简体   繁体   中英

Events not firing on the first postback, but will fire after that

I'm having quite a strange issue where my generated control will not fire its' SelectedIndexChanged event on the first click, but will dutifully work after that.

The code is as follows:

//Generate list
ddl.Items.Add(new ListItem(" ", " "));
for(int i =1 ; i < 13 ; i ++)
{
    ddl.Items.Add(new ListItem(i.ToString(),i.ToString()));
}
//ddl.SelectedIndex = -1;
tc2.Controls.Add(ddl);
tr.Cells.Add(tc2);

//Keep position after postback
for (int i = 1; i < 13; i++)
{
    if (i.ToString() == sNoOfPreviousMonths )
    {
        ddl.SelectedIndex = i;
    }
}

And the event:

void ddlNoOfPreviousMonths_SelectedIndexChanged(object obj, EventArgs e)
{
    DropDownList x = obj as DropDownList;
    sNoOfPreviousMonths = x.SelectedValue;

}

I found out the answer, turns out that on the first click the control's ID is different, and gets changed on the second click. It's explained better here

i think u dont use Autopostback for dropdownlist. u need set to be true value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM