hkguile
hkguile

Reputation: 4359

Add dropdownlist value to database which items add from page load functions

protected void Page_Load(object sender, EventArgs e)
{
    //skipped
    foreach(pair item in al)  
    {           
        pid.Items.Add(new ListItem(item.getTitle(), item.getId()));

    }  

}

protected void Button1_Click(object sender, EventArgs e) 
{
            insertUser.Parameters.AddWithValue("@parent_id", Convert.ToInt32(pid.SelectedValue));

}

From the above code there is a dropdownlist that all items are added from Page_Load function, and the bug is that, whatever items i choose from dropdownlist, only the first item is added to database.

anyone know what's the problem?

thanks!

Upvotes: 1

Views: 976

Answers (1)

giftcv
giftcv

Reputation: 1662

You should not add items during postbacks

try

if (!IsPostBack)
{
    foreach(pair item in al)  
    {           
        pid.Items.Add(new ListItem(item.getTitle(), item.getId()));

    }  
}

The IsPostBack property checks whether the page is being rendered for the first time or is responding to a postback.

Upvotes: 2

Related Questions