【发布时间】:2011-04-06 08:57:38
【问题描述】:
我知道这不是第一次在此处发布有关此问题的问题,但我还没有设法找到我的问题的答案。
我的页面上有许多列表框:
<tr>
<td class="loqhArea2Area">
<asp:ListBox ID="ListBox1Val1" class="InputItem" runat="server" AutoPostBack="true"></asp:ListBox>
</td>
<td class="loqhArea3Area">
<asp:ListBox ID="ListBox2Val1" class="InputItem" runat="server" AutoPostBack="true"></asp:ListBox>
</td>
<td class="loqhArea4Area">
<asp:ListBox ID="ListBox3Val1" class="InputItem" runat="server"></asp:ListBox>
</td>
</tr>
这些框在某种意义上是链接在一起的,第一个框中的选择用于填充第二个框和第四个框。为了从他们那里获取信息,我使用以下代码 sn-p:
protected override void OnInit(EventArgs e)
{
// Do some other stuff ...
if (!IsPostBack)
{
// Fill the boxes on initial load
}
else
{
// INeedTheData takes an ID-string (in this case "Val1")
// and the selected indexes as ints
INeedTheData("Val1",
ListBox1Val1.SelectedIndex,
ListBox2Val1.SelectedIndex,
ListBox3Val1.SelectedIndex);
}
// Some error handling
}
问题是 SelectedIndexes 都返回-1,这显然不是我需要的。
我一直在疯狂搜索这个问题的解决方案。欢迎提供所有线索或线索。提前致谢!
更新: 也许这对任何人都有任何线索,我的前任(不幸的是,我无法联系到他)实现了这个相当奇怪的代码,它确实有效。或者我应该说某种作品。问题是我们想要一些更可靠的代码,所以我开始重写它。
INeedTheData("Val1"
, Request.Form.AllKeys.Contains("ctl01$ListBox1Val1") ? Request.Form["ctl01$ListBox1Val1"] == string.Empty ? 0 : int.Parse(Request.Form["ctl01$ListBox1Val1"]) : 0
, Request.Form.AllKeys.Contains("ctl01$ListBox2Val1") ? Request.Form["ctl01$ListBox2Val1"] == string.Empty ? 0 : int.Parse(Request.Form["ctl01$ListBox2Val1"]) : 0
, Request.Form.AllKeys.Contains("ctl01$ListBox3Val1") ? Request.Form["ctl01$ListBox3Val1"] == string.Empty ? 0 : int.Parse(Request.Form["ctl01$ListBox3Val1"]) : 0);
此解决方案不可取,因为它使用硬编码的 html id 获取数据,将来在重新构建和重新组织页面上的内容时可能会发生变化。无论如何,我认为它应该在这里输入,因为这是我重写它的原因。
如上所述,欢迎所有 cmets!谢谢!
更新 ii(对@Deeptechtons 的回答):期望的行为
我有一组三个 ListBox,用于从树形图中导航和做出选择。第一个框 (ListBox1Val1) 直接从数据库中填充。第二个 (ListBox2Val1) 是空的,直到用户在第一个中选择了他的选项。这样做会导致第一个列表框中所选节点的子节点加载到第二个列表框中。第三个列表框 (ListBox3Val1) 也是如此。在第二个框中选择一个节点,然后填充第三个。
【问题讨论】:
-
你为什么在 OnInit 而不是 PageLoad 中这样做?
-
其实我也不知道。公平地说,我是该项目的新手,也是 asp.net 开发的新手。我会尝试移动代码!
-
@Lazarus:它给出了相同的行为。 :(
-
@Lazarus @dotmartin 你们能否在这些列表框上启用视图状态,然后重建项目并进行测试。
<asp:ListBox ID="ListBox1Val1" class="InputItem" runat="server" EnableViewState="True" AutoPostBack="true"></asp:ListBox> -
@Deeptechtons:遗憾的是问题仍然存在。
标签: c# asp.net listbox autopostback