gpt4 book ai didi

c# ASP.Net 动态填充的 DropDownList 在提交按钮上丢失选定的索引

转载 作者:太空狗 更新时间:2023-10-29 21:35:34 25 4
gpt4 key购买 nike

我从 PageLoad 上的 ArrayList 动态填充所有 50 个状态的下拉列表。当用户选择提交按钮(btnSubmit_Click 事件)时,下拉列表控件的 SelectedIndex 属性始终为 0,无论用户选择了什么。


添加了更多代码以帮助解决问题。从 session 变量 (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;
}

最佳答案

当我遇到 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();
}

为了恢复选定的索引,我将 selectedIndex 传递到一个隐藏字段中。

希望这有帮助吗?

对了,为什么要传入DropDownList对象作为参数呢?而是调用无参数函数并在函数内填充 DropDownList 对象。

另外,确保 ViewState 没有关闭。

关于c# ASP.Net 动态填充的 DropDownList 在提交按钮上丢失选定的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9619111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com