0

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.