I have two DropDownList which are being populated from a database(SQL) on Page_Load. Now I want to take the Text/Value selected from each DropDownList and insert them into a database. But when I c开发者_如何学运维lick on the button and the OnClick Event is action the DropDownLists go back to the original state and not the selected values get inserted, instead I get the first Text/Value of the DropDownList all the time. How can I prevent this from happening? Any help will really appreciate it.
I'm using C# for this.
Thanks
In page load, load up the dropdowns like this
protected void Page_load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadDropDowns();
}
}
Basically button click causes postback and if you are populating controls in Load event without check for postback, it will repopulate your controls resetting it's state.
Your DDL is getting rebuilt on every page load. You need to wrap your ddl data source calculation in an
if (!IsPostBack)
or put it in a part of the lifecycle that only loads once, like the OnLoad()
精彩评论