I have 7 items in my dropdown list like
<asp:DropDownList ID="DdlSortBy" runat="server" OnSelectedIndexChanged="DdlSortBy_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem Value="0">Case 1</asp:ListItem>
<asp:ListItem Value="1">Case 2</asp:ListItem>
<asp:ListItem Value="2">Case 3</asp:ListItem>
<asp:ListItem Value="3">Case 4</asp:ListItem>
<asp:ListItem Value="4">Case 5</asp:ListItem>
<asp:ListItem Value="5">Case 6</asp:ListItem>
<asp:ListItem Value="6">Case 7</asp:ListItem>
</asp:DropDownList>
all items except Case 1 value 0 initiate selected index change event.
Any idea how to fix it?
If it is working for one then it should be working for each of them; an instance in which it won't postback upon selection would be if that item was already selected, say, by default - then you would need to select something else, then re-select said "default" value.
Otherwise, I can't see that any single item would be discriminated against.
The reason might be that the first item is selected by default. What you could try is to add a new item and set it to be the first:
<asp:ListItem Value="-1">please select</asp:ListItem>
That way, when you select Case 1, it will fire the event.
Related
I have a dropdownlist and want for the dropdownlist to keep firing the action whenever I selected a value, regardless of the selected value being changing or not.
dropdownlist having value of (a, b, c)
dropdownlist_SelectedIndexChanged(object sender, EventArgs e)
{
//action adding a new row in gridview
}
In this case, if I select the dropdownlist value (a, b, c, a, b,...) respectively this still works.
However, if I select the dropdownlist value (a, a, b, c, c, a,...) respectively, (a,a) and (c,c) doesn't fire the action.
This clearly does not work as I wanted, as it only works whenever I select a different value.
How do i play around this? Thank you
The SelectedIndexChanged event only fires when the Value is different. In this first snippet, there are duplicate Value items, so selecting the first or second A makes no difference and the event is not fired.
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="A" Value="A"></asp:ListItem>
<asp:ListItem Text="A" Value="A"></asp:ListItem>
<asp:ListItem Text="B" Value="B"></asp:ListItem>
<asp:ListItem Text="B" Value="B"></asp:ListItem>
<asp:ListItem Text="C" Value="C"></asp:ListItem>
<asp:ListItem Text="C" Value="C"></asp:ListItem>
</asp:DropDownList>
So make sure you have unique values, something like this and it will work.
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="A" Value="A_1"></asp:ListItem>
<asp:ListItem Text="A" Value="A_2"></asp:ListItem>
<asp:ListItem Text="B" Value="B_1"></asp:ListItem>
<asp:ListItem Text="B" Value="B_2"></asp:ListItem>
<asp:ListItem Text="C" Value="C_1"></asp:ListItem>
<asp:ListItem Text="C" Value="C_2"></asp:ListItem>
</asp:DropDownList>
im editing one gridview. while clicking edit im redirecting to one page and binding all the fields. but unble to bind month dropdown . i want to bind april month in dropdown but not binding
<asp:DropDownList ID="DDLMonth" runat="server">
<asp:ListItem>Month</asp:ListItem>
<asp:ListItem Text="January" Value="1"></asp:ListItem>
<asp:ListItem Text="February" Value="2"></asp:ListItem>
<asp:ListItem Text="March" Value="3"></asp:ListItem>
<asp:ListItem Text="April" Value="4"></asp:ListItem>
<asp:ListItem Text="May" Value="5"></asp:ListItem>
<asp:ListItem Text="June" Value="6"></asp:ListItem>
<asp:ListItem Text="July" Value="7"></asp:ListItem>
<asp:ListItem Text="August" Value="8"></asp:ListItem>
<asp:ListItem Text="September" Value="9"></asp:ListItem>
<asp:ListItem Text="October" Value="10"></asp:ListItem>
<asp:ListItem Text="November" Value="11"></asp:ListItem>
<asp:ListItem Text="December" Value="12"></asp:ListItem>
code behind
string DDLMonthstr = date.Month.ToString();//4
DDLMonth.SelectedIndex = Convert.ToInt32(DDLMonthstr);
Try
DDLMonth.Items.FindByValue(string val).Selected = true;
You probably have already selected item and need to clear selection using ListControl.ClearSelection before making new selection through code.
string DDLMonthstr = date.Month.ToString();//4
DDLMonth.ClearSelection();
DDLMonth.SelectedIndex = Convert.ToInt32(DDLMonthstr);
Also check if you are getting any exception by using try-catch block if you do not already have, this could help in fixing.
I have a dropdown list in design page as shown below:
<asp:DropDownList ID="ddlArtList" runat="server">
<asp:ListItem Value="95">Select</asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
</asp:DropDownList>
This items above are sometimes overridden by some other values in C#, according to the requirement.
But at the end i want to bind the above default items with the help of C# to get the above listitems.
I want to know is there any In-Built method or attribute to bind the dropdownlist(.aspx) in C#.
Without using this: ddlArtList.Items.Add ("1); etc etc.
Thanks in advance.
Use AppendDataBoundItems
.aspx Code:
<asp:DropDownList ID="ddlArtList" AppendDataBoundItems="True" runat="server">
<asp:ListItem Value="95">Select</asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
</asp:DropDownList>
ServerSide:
ddlArtList.AppendDataBoundItems="True"
You can keep the default list in Session during first Page Load -
if(!isPostback)
{
ListItem[] items = new ListItem[ddlArtList.Items.Count];
ddlArtList.Items.CopyTo(items, 0);
Session["ddlArtList"] = items;
}
Now when you want to reset the list -
if(Session["ddlArtList"] != null)
{
ListItem[] items = Session["ddlArtList"] as ListItem[];
ddlArtList.Items.Clear();
ddlArtList.Items.AddRange(items);
}
If you want to append to a default list, use the markup as you provided to set the default list. You will want to set AppendDataBoundItems to true, as already mentioned.
<asp:DropDownList ID="ddlArtList" runat="server" AppendDataBoundItems="true">
<asp:ListItem Value="95">Select</asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
</asp:DropDownList>
In the code-behind, you can set the DataSource to the appended items and simply call DataBind. That will append those items to your dropdownlist.
ddlArtList.DataSource = new List<int>{ 10, 11, 12 }; // replace with actual data source you are using
ddlArtList.DataBind();
Depending on all of your needs, you can add those appended items on the page's Load event or inside another event handler, such as a button click or selecting from another dropdownlist or whatever.
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 web form in which there are some controls along with dropdown list as below:
<asp:DropDownList ID="ddlrange" CssClass="dropdownstate" style="position:absolute;top:224px; left:200px;" runat="server" >
<asp:ListItem >Select Range</asp:ListItem>
<asp:ListItem Value="A" >A</asp:ListItem>
<asp:ListItem Value="B" >B</asp:ListItem>
<asp:ListItem Value="C" >C</asp:ListItem>
<asp:ListItem Value="D" >D</asp:ListItem>
</asp:DropDownList>
and theres a gridview in which data is shown on btninsert click.
and I have a link button EDIT. on clicking edit the corresponding values are displayed in their respective controls.
Now, coming to the point,insert works fine, but when i click on Edit link every thing is fine except the value in dropdown list.
it should be like as shown in screenshot below:
but what I get is shown below:
how can I overcome this???
Try giving value to the Select Range list item and then chcek, may be that helps.
Maybe this is helpful for you
<asp:DropDownList ID="ddlrange" CssClass="dropdownstate" style="position:absolute;top:224px; left:200px;" runat="server" >
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="" >Select Range</asp:ListItem>
<asp:ListItem Value="A" >A</asp:ListItem>
<asp:ListItem Value="B" >B</asp:ListItem>
<asp:ListItem Value="C" >C</asp:ListItem>
<asp:ListItem Value="D" >D</asp:ListItem>
</asp:DropDownList>
In edit click event add a "Select Range" value into the drop down.
ddlRange.Items.Insert (0,"Select Range");
Add <asp:ListItem Value=null>Select Range</asp:ListItem> line.
Try Below:
<asp:DropDownList ID="ddlrange" CssClass="dropdownstate" style="position:absolute;top:224px; left:200px;" runat="server" >
<asp:ListItem Value="Select Range">Select Range</asp:ListItem>
<asp:ListItem Value="A" >A</asp:ListItem>
<asp:ListItem Value="B" >B</asp:ListItem>
<asp:ListItem Value="C" >C</asp:ListItem>
<asp:ListItem Value="D" >D</asp:ListItem>
</asp:DropDownList>
I tried using ddlrange.SelectedValue instead of ddlrange.SelectedItem.Text, and issue is solved.
Thanks everyone for your efforts, I appreciate.