提交后从下拉列表中获取选定的值?

时间:2010-05-17 06:36:34

标签: asp.net webforms drop-down-menu

我在ASP.NET webforms中填充DropDownList:

<asp:DropDownList runat="server" ID="salesman"></asp:DropDownList>


users= Buslayer.GetSalesRep();

foreach (userentity user in users)
{         
      salesman.Items.Add(new ListItem(user.FirstName + " " + user.LastName,
                                      user.UserID.ToString())); 
}

提交后,我仍然选择index = 0,

我尝试了所有这些但失败了:

Response.Write("" + salesman.SelectedValue);
Response.Write("" + salesman.SelectedItem.Value);
Response.Write("" + salesman.SelectedIndex);

1 个答案:

答案 0 :(得分:0)

您是否正在检查与您进行数据绑定的页面回复?

您的网页加载应如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        BindData();
    }

}

protected void BindData()
 {
    // Get your data
    ddl.DataSource = yourData;
    ddl.DataBind();

}