Sus*_*san 6 c# asp.net selectedindex drop-down-menu
我从PageLoad上的ArrayList动态填充所有50个状态的下拉列表.当用户选择SUBMIT按钮(btnSubmit_Click事件)时,尽管用户选择了什么选择,但下拉列表控件的SelectedIndex属性始终为0.
表单中的HTML代码:
<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True"
onselectedindexchanged="ddlState_SelectedIndexChanged" >
</asp:DropDownList>
Run Code Online (Sandbox Code Playgroud)
代码背后:
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;
}
Run Code Online (Sandbox Code Playgroud)
小智 4
当我在 ViewState 上遇到问题时(这就是我对你的情况的怀疑),我用它来将数据恢复到动态填充的下拉对象中
public void Page_Load(object sender, EventArgs e){
if (!IsPostBack)
{
Databind();
}
else {
LoadAllViewStates();
}
}
private void Databind()
{
DataTable questionnaireDT = null;
DataTable questionsDT = null;
DataTable indicatorDT = null;
DataView tempView = QuestionnaireDS.Select(DataSourceSelectArguments.Empty) as DataView;
questionnaireDT = tempView.Table;
ViewState["QuestionnaireDL"] = questionnaireDT;
QuestionnaireDL.DataSource = ViewState["QuestionnaireDL"];
QuestionnaireDL.DataBind();
tempView = QuestionDS.Select(DataSourceSelectArguments.Empty) as DataView;
questionsDT = tempView.Table;
ViewState["QuestionList"] = questionsDT;
QuestionList.DataSource = ViewState["QuestionList"];
QuestionList.DataBind();
tempView = IndicatorDS.Select(DataSourceSelectArguments.Empty) as DataView;
indicatorDT = tempView.Table;
ViewState["IndicatorLst"] = indicatorDT;
IndicatorLst.DataSource = ViewState["IndicatorLst"];
IndicatorLst.DataBind();
}
private void LoadAllViewStates()
{
QuestionnaireDL.DataSource = ViewState["QuestionnaireDL"];
QuestionnaireDL.DataBind();
QuestionList.DataSource = ViewState["QuestionList"];
QuestionList.DataBind();
IndicatorLst.DataSource = ViewState["IndicatorLst"];
IndicatorLst.DataBind();
}
Run Code Online (Sandbox Code Playgroud)
为了恢复所选索引,我将 selectedIndex 传递到隐藏字段中。
希望这可以帮助?
对了,为什么要传入DropDownList对象作为参数呢?相反,调用无参数函数并在函数内填充 DropDownList 对象。
另外,请确保 ViewState 未关闭。
归档时间: |
|
查看次数: |
12920 次 |
最近记录: |