Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selected Index doesn't change

Tags:

html

c#

select

I have an ASP.NET DropDownList control, with a onSelectedIndexChanged event. I also have the AutoPostBack="true" that many have said would fix the problem. However I don't think that is where the problem lays... My Html code and C# code are below for reference. The thing is the code works, but only when I press the enter key while editing the drop down box. If I simply click on an object in the drop down then the event will not fire. If I change the selected item so the "selected" text in the drop down says "ASP" and I then inspect the element using the browser I see that the Selected="True" part of the ListItem is still on the first item... It doesn't change in there. It changes with an enter key but not with a mouse click. Any help is welcome and much appreciated.

HTML:

<div class="ui-widget">
    <asp:DropDownList id="Select1" OnSelectedIndexChanged="Select1_SomethingChange" runat="server" AutoPostBack="true">
        <asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
        <asp:ListItem Value="Select one...">Select one...</asp:ListItem>
        <asp:ListItem Value="ActionScript">ActionScript</asp:ListItem>
        <asp:ListItem Value="AppleScript">AppleScript</asp:ListItem>
        <asp:ListItem Value="Asp">Asp</asp:ListItem>
        <asp:ListItem Value="BASIC">BASIC</asp:ListItem>
    </asp:DropDownList>
</div>

C#:

protected void Select1_SomethingChange(object sender, EventArgs e)
{
    //something is meant to happen here
}
like image 935
5tar-Kaster Avatar asked Nov 19 '25 20:11

5tar-Kaster


1 Answers

It may be caused by data binding your dropdownlist in Page_Load method. Please, surround it (data binding) with

if(!IsPostBack){
// data binding.
}

Hope, it help!

like image 76
Egor Romanko Avatar answered Nov 21 '25 09:11

Egor Romanko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!