I'm not sure what to do here, I might have to use viewstates, but I need help.
I have a dropdown list, I am not databinding. I would know if I was I should do a Page.IsPostBack and not databind.
<asp:DropDownList ID="ddlWeeklyWeightIn" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:DropDownList>
Now in my code behind I have this:
protected void Button1_Click(object sender, EventArgs e)
{
string wwin = "";
wwin = ddlWeeklyWeightIn.SelectedItem.Text;
}
On the button click is always "1", never the selected item.
Thank you
Aspx side:
add autopostback=true to dropdown property and and use listitem as following:
ListItem Text="4" Value="4"> etc
Code Behind: string wwin = ""; wwin = ddlWeeklyWeightIn.SelectedValue;
try adding EnableViewState="True" to your dropdown control.
A post back is causing to reset your selected value. either use if(!IsPostBack) or use update panel to prevent post back.
This is just to make sure but could you try using:
<asp:DropDownList ID="ddlWeeklyWeightIn" runat="server">
<asp:ListItem Text="1"></asp:ListItem>
<asp:ListItem Text="2"></asp:ListItem>
<asp:ListItem Text="3"></asp:ListItem>
<asp:ListItem Text="4"></asp:ListItem>
</asp:DropDownList>
Could you check you don't just SET the selected item during page's initialization?
Related
I have the following asp dropdown:
<asp:DropDownList ID="ArticleCategories" runat="server" AppendDataBoundItems="true" CssClass="ArticleCategoriesDropDown" onselectedindexchanged="mCategoryItemSelected" AutoPostBack="True">
<asp:ListItem Text="Select Category" Value="" />
</asp:DropDownList>
Once a selection has been made, and the data loaded, I want to set the value in the dropdown to display the value chosen by the user.
I am doing this by:
ArticleCategories.SelectedValue = CategoryID.ToString();
This works fine the first time, but from then the page is stuck on that selection, as the selected value gets loaded before the new value can load.
I have tried disabling the itemchangelistener by:
ArticleCategories.SelectedIndexChanged += new EventHandler(doNothing);
But that does not work.
I have also tried to disable postback which also does not work.
Any ideas?
You should only set the selection when loading the page the first time. In ASP.NET, you can achieve this by checking the IsPostBack property:
if(!IsPostBack)
{
ArticleCategories.SelectedValue = CategoryID.ToString();
}
You can do Autopostback= false
<asp:DropDownList ID="ArticleCategories" runat="server" AppendDataBoundItems="true" CssClass="ArticleCategoriesDropDown" onselectedindexchanged="mCategoryItemSelected" AutoPostBack="False">
<asp:ListItem Text="Select Category" Value="" />
</asp:DropDownList>
Hey before selecting another item from Dropdown you should clear previous selection then selected another one.
ArticleCategories.ClearSelection();
Hope it helps you.
I have an aspx page (C# code page). I have some hard coded dropdownlists and for some reason they are not displaying the top list item (value). I added an extra top list item (value) and now the correct values display for the values already in it, but that extra one does not display.
The only functionality I do with my dropdownlists in my C# code is to hide or show them. And then do validation or not based on the selected value.
My aspx code:
<asp:DropDownList ID="ddlAction" runat="server" Visible="True"
AppendDataBoundItems="true" Height="25px" Width="149px">
<asp:ListItem Value="Select">Please Select</asp:ListItem>
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
C# Code:
ddlAction.Visible = false;
ddlAction.Visible = true;
I use dropdownlist's regularly and have never had this problem before. Does anyone have any ideas what the issue could be?
UPDATE TO THIS ISSUE:
I added my items in my C# code as per Rahul. Did a quick test and it worked.
Now this morning, I am once again getting blanks for the first item ("Please Select").
Aspx code:
<asp:DropDownList ID="ddlAction" runat="server"
AppendDataBoundItems="True" Height="27px" Width="159px">
</asp:DropDownList>
C# code:
ddlAction.Visible = true;
ddlAction.AppendDataBoundItems = true;
ddlAction.Items.Insert(0, new ListItem("Please Select","Select"));
ddlAction.Items.Insert(1, new ListItem("Yes", "Yes"));
ddlAction.Items.Insert(2, new ListItem("No", "No"));
ddlAction.DataBind();
Rendered source code:
<select name="ctl00$ContentPlaceHolder1$ddlAction" id="ContentPlaceHolder1_ddlAction" style="height:27px;width:159px;">
<option selected="selected" value="Select"></option>
<option value="Yes">Yes</option>
<option value="No">No</option>
Try to use AppendDataBoundItems = true property of DropSownList into your .aspx page.
you may also assign value from code behind as well like
ddlAction.Items.Insert(0, new ListItem(String.Empty, String.Empty));
use AppendDataBound = true in your aspx coe.
<asp:DropDownList ID="ddlAction" AppendDataBound = true runat="server" Visible="True" Height="25px"
Width="149px">
<asp:ListItem Value="Select">Please Select</asp:ListItem>
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
Edit 1
More detail about List Item
<asp:ListItem Value="-2" Text="Please Select"></asp:ListItem>
<asp:ListItem Value="0" Text="Yes"></asp:ListItem>
<asp:ListItem Value="-1" Text="No"></asp:ListItem>
I suggest you declare your DropDownList ListItems using its internal properties and defining what ListItem must be the selected one:
<asp:DropDownList ID="ddlAction" runat="server" Visible="True" AppendDataBoundItems="true" Height="25px" Width="149px">
<asp:ListItem Text="Please Select" Value="Select" Selected="True"></asp:ListItem>
<asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
<asp:ListItem Text="No" Value="No"</asp:ListItem>
</asp:DropDownList>
It's the way ASP.NET uses to work and will return you the right selected value on the server side on postbacks.
I think that you don't have to use nor the DataBind() method neither set the AppendDataBoundItems, because, you already inserted the ListItems and you aren't loading options from a database!
I think you need to tell what ListItemIndex is the selected one by seting a value to the DropDownList.SelectedIndex property.
EDIT
Also, try to read to MSDN documentation about the AppendDataBoundItems property and the enter link description here method.
I have a radiobuttonlist, a label, and a dropdown as follows:
<asp:RadioButtonList id="rbList" runat="server" AutoPostBack="true" EnableViewState="false"
OnSelectedIndexChanged="rbList_SelectedIndexChanged"
RepeatLayout="Table" RepeatDirection="Horizontal" RepeatColumns="3">
<asp:ListItem Selected="True"> Radio 1 </asp:ListItem>
<asp:ListItem> Radio 2 </asp:ListItem>
<asp:ListItem> Radio 3 </asp:ListItem>
</asp:RadioButtonList>
<asp:Label runat="server" ID="lbl" text="1,2" EnableViewState="false"></asp:Label>
<asp:DropDownList runat="server" ID="ddl" Visible="false">
</asp:DropDownList>
My rbList_SelectedIndexChanged is as follows:
protected void rbList_SelectedIndexChanged(object sender, EventArgs e)
{
if (rbList.SelectedIndex == 0 | rbList.SelectedIndex==1)
{
lbl.Text = "1,2";
ddl.Visible = false;
//ddl.Attributes.Add("style", "display:none");
}
else if (rbList.SelectedIndex == 2)
{
lbl.Text = "3";
ddl.Visible = true;
//ddl.Attributes.Add("style", "");
}
}
Now when I change from radio3 to radio2, the event is getting fired as usual and everything looks good. But when I change from radio3 to radio1, I don't see the event getting fired (I inserted a breakpoint) the ddl stays visible but the value of lbl changes to 1,2.
My 2 questions are as follows:
1) Why is the event not getting fired on changing from radio3 to radio1?
2) How is the label value getting changed when the event is not firing?
Any help or comments are much appreciated..Thanks in advance!
I am not sure this is a bug or not, but...
When EnableViewState="false" with DDL or RBL and user trying to pick the first list item (index 0), the SelectedIndexChanged will NOT get fired.
If you set EnableViewState="true", then the DDL or RBL should work properly when user pick the first list item while non-first item was selected...
Preselecting a radio button in your markup is causing your problems. going from any other option back to option 1 will not trigger the changed event.
this line is your culprit.
<asp:ListItem Selected="True"> Radio 1 </asp:ListItem>
if you remove the Selected attribute the event should register properly
<asp:ListItem> Radio 1 </asp:ListItem>
you could handle the preselection in your code behind.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
rbList.SelectedIndex = 0;
}
}
As far as I can tell, programmatically setting SelectedIndex (even on first post) results in the same behaviour as setting Selected="True" on the markup.
The only sure-fire way seems to be using an UpdatePanel with the RadioButtonList as an async trigger, making sure the markup changes with every change.
That is, unless you want to go the jQuery route..
I had a similar problem, but it had to do with the "ChildrenAsTriggers" property of the update panel being set to false. All other radio button indexes worked fine this way, except index 0.
How can i set each list item value dynamically in radio button list?
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:RadioButtonList>
In above radio button list the list items i want to set from database.
your help will be highly appreciated.
You must find ListItem by text and change it. Let's think, you have control :
<asp:RadioButtonList ID="yourrdblist" runat="server" RepeatDirection="Horizontal" AutoPostBack="true" CssClass="tarpiukai">
<asp:ListItem Selected="True" Value="0" Text="0"></asp:ListItem>
<asp:ListItem Value="0" Text="1"></asp:ListItem>
<asp:ListItem Value="1" Text="2"></asp:ListItem>
</asp:RadioButtonList>
In this case you can easy change value of ListItem with text "1" to text "bambargija" :
yourrdblist.Items.FindByText("1").Text = "bambargija";
That's easy. Have a look at the basiscs of the RadioButtonList-Control.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobuttonlist.aspx
If you wanna have access to the inner controls, the ListItems then acces them by using the controls property Items.
Greetings,
Like this:
<asp:RadioButtonList ID="inputs" runat="server" Width="50px">
</asp:RadioButtonList>
..and...
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
inputs.Items.Add("input1");
inputs.Items.Add("input2");
inputs.Items.Add("input3");
inputs.Items.Add("input4");
}
}
this is my radio button list:
<asp:RadioButtonList runat="server" ID="rdlTest" OnSelectedIndexChanged="rdlTst_selectedChange">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
</asp:RadioButtonList
this is the code behind:
protected void rdlTst_selectedChange(object sender, EventArgs e)
{
Response.Write("hello");
}
it's not firing the event for some reason
You need to add AutoPostBack = "true" onto the definition of the RadioButtonList.
<asp:RadioButtonList runat="server" ID="rdlTest" AutoPostBack="true" OnSelectedIndexChanged="rdlTst_selectedChange">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
</asp:RadioButtonList>