我从 PageLoad 上的 ArrayList 动态填充所有 50 个状态的下拉列表。当用户选择 SUBMIT 按钮(btnSubmit_Click 事件)时,无论用户选择什么选项,下拉列表控件的 SelectedIndex 属性始终为 0。
添加了更多代码以帮助进行故障排除。从会话变量 (bbb) 和 ddlState.selectedindex (bbb) 中获取 -1。
表单中的 HTML 代码:
<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True"
onselectedindexchanged="ddlState_SelectedIndexChanged" >
</asp:DropDownList>
代码背后:
protected void Page_Load(object sender, EventArgs e)
{
//------------------------------------------------
// Populates state dropdownlists
//------------------------------------------------
if (!IsPostBack)
{
GetAllStatesForDdl(ddlDLState);
GetAllStatesForDdl(ddlOldState);
GetStatesForDdl(ddlState);
}
}
private void GetAllStatesForDdl(DropDownList ddlStateList)
{
AppInputFormProcessor getStates = new AppInputFormProcessor();
ArrayList States = new ArrayList();
States = getStates.GetAllStates();
ddlStateList.DataSource = States;
ddlStateList.DataBind();
}
private void GetStatesForDdl(DropDownList ddlStateList)
{
AppInputFormProcessor getStates = new AppInputFormProcessor();
ArrayList States = new ArrayList();
States = getStates.GetStates();
ddlStateList.DataSource = States;
ddlStateList.DataBind();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
int aaa = ddlState.SelectedIndex;
int bbb = Convert.ToInt32(Session["ddlState"]);
}
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
Session["ddlState"] = ddlState.SelectedIndex;
}