我在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);
答案 0 :(得分:0)
您是否正在检查与您进行数据绑定的页面回复?
您的网页加载应如下所示:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
// Get your data
ddl.DataSource = yourData;
ddl.DataBind();
}