jorame
jorame

Reputation: 2207

DropDownList not issues in asp.net

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 click 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

Upvotes: 0

Views: 70

Answers (2)

Rikon
Rikon

Reputation: 2696

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()

Upvotes: 0

Bala R
Bala R

Reputation: 108937

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.

Upvotes: 2

Related Questions