I’m try making a dynamic DropDownList this way:
<form id="form1" runat="server">
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="True"
onselectedindexchanged="CategoryDropList_SelectedIndexChanged" />
</form>
void DropListInit()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("1","apple");
dic.Add("2","banana");
ddlCategory.DataSource = dic;
ddlCategory.DataTextField = "value";
ddlCategory.DataValueField = "key";
ddlCategory.DataBind();
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
DropListInit();
}
protected void CategoryDropList_SelectedIndexChanged(object sender, EventArgs e)
{
ddlCategory.SelectedValue = ddlCategory.SelectedValue;
}
And i find out that in doesn’t work without this strange expression ddlCategory.SelectedValue = ddlCategory.SelectedValue; So, what this expression means? Or am I doing something wrong?
Move the DropListInit to
Page_loadas the following;Remove this;
Should be fine.