【发布时间】:2019-09-03 14:38:57
【问题描述】:
我正在使用带有母版页的网络表单。
在网络表单上,我有一个更新面板。里面是一个由数据表加载的列表框。我让列表执行自动回发,并且在 SelectedIndexChanged 上,无论我选择什么,它都会返回第一项。
我注意到如果我在 .aspx 页面上列出了项目,它会返回正确的值。
我不知道发生了什么。
<asp:Content ID="SearchBodyContent" ContentPlaceHolderID="MainContent" runat="server" EnableViewState="true">
<asp:UpdatePanel ID="parentup" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:UpdatePanel ID="childup1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:ListBox ID="list" runat="server" AutoPostBack="true" OnSelectedIndexChanged="list_SelectedIndexChanged" DataTextField="title" DataValueField="type">
</asp:ListBox>
<asp:TextBox runat="server" ID="text" Visible="false"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="list" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</ContentTemplate>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using (SecureData sd = new SecureData(true, UserManager.GetLoginToken(true)))
{
_dt = sd.GetData("web_searchcriteria_item_listKK", "", 0, 0).Tables["web_searchcriteria_item_listKK"];
list.DataSource = _dt;
list.DataBind();
list.Items.Insert(0, "Select One");
Session["criteria"] = _dt;
}
}
}
protected void list_SelectedIndexChanged(object sender, EventArgs e)
{
string title = list.SelectedItem.Text;
text.Visible = true;
}
我真的需要得到实际选择的项目。有人可以帮忙吗?
【问题讨论】:
-
ListBox 在子更新面板中,可能是它触发了父更新面板并且更改的值没有正确回传,请尝试将 ChildrenAsTriggers="False" 添加到父更新面板。跨度>
-
试过了。没用。
-
我能够毫无问题地运行您的代码,您能否显示您的网络表单的整个 Html?
-
我没有显示的 html 的唯一部分是 site.master 当我从数据库加载时,它在第一次单击列表框时起作用,但第二次和后续似乎几乎是随机的。如果我在 c# 中创建表,它工作正常。我开始认为,几年前某人所做的大量代码背后有一些东西可能会弄乱这个。感谢您的帮助,但我已经放弃了这种方法。
标签: c# listbox updatepanel