开发者

Makinkg dynamic DropDownList

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_load as the following;

protected void Page_LoadComplete(object sender, EventArgs e) 
{
    if (!IsPostBack)    
    {
        DropListInit();
    }
}

Remove this;

ddlCategory.SelectedValue = ddlCategory.SelectedValue;

Should be fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜