Friday, 4 July 2014

OnClick event upon changing the item in a DropDown

This posts says about triggering an event upon selecting a item in a DropDown.

First add the DropDownList into the page, update the asp snippet of the dropdown control in .aspx file ash shown as below.

<asp:DropDownList ID="DropDownList1" runat="server"  OnDataBound = "DropDown1_OnDataBound" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="100px" AutoPostBack="True" BackColor="#666666">      </asp:DropDownList>



Below functions are to be used for the dropdown

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
             //Write the code which you want to trigger upon selecting an item in the dropdown list
        }


protected void DropDown1_OnDataBound(object sender, EventArgs e)
        {
            if (this.DropDownList1.Items.Count > 0)
            {               
                this.DropDownList1.Items.Insert(0, new ListItem("--Select--", "-1"));                
            }                        
        }


No comments:

Post a Comment