我知道这不是第一次在这里发布有关此问题的问题,但我没有设法找到我的问题的答案。
我的页面上有很多ListBox:
<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>
这些框在某种意义上是链接在一起的,第一个框中的选项用于填充第二个框,因此第四个框。为了从中获取信息,我使用此代码段:
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 all返回-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获取数据,这可能会在将来重建和重新组织页面上的内容时发生变化。无论如何我认为应该在这里输入,因为这是我重写它的原因。
如上所述,欢迎所有评论!谢谢!
更新ii(回答@Deeptechtons):所需行为
我有一组三个ListBox用于导航和从树形图中进行选择。第一个框(ListBox1Val1
)直接从数据库填充。第二个(ListBox2Val1
)为空,直到用户在第一个中选择了他的选择。这样做会导致第一个列表框中所选节点的子节点加载到第二个列表框中。第三个列表框(ListBox3Val1
)也是如此。在第二个框中选择一个节点,然后填充第三个节点。
答案 0 :(得分:2)
@dotmartin以下是您在Cs文件中需要的代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListBox1.DataSource = GetList();
ListBox1.DataBind();
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox2.DataSource = GetSecondList(ListBox1.SelectedIndex);
ListBox2.DataBind();
}
protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox3.Items.Add(new ListItem(ListBox1.SelectedValue + "-" + ListBox2.SelectedValue, "Wippie"));
}
private ListItemCollection GetList()
{
ListItemCollection lstNumbers = new ListItemCollection();
lstNumbers.Add(new ListItem("1", "One"));
lstNumbers.Add(new ListItem("2", "Two"));
lstNumbers.Add(new ListItem("3", "Three"));
lstNumbers.Add(new ListItem("4", "Four"));
lstNumbers.Add(new ListItem("5", "Five"));
return lstNumbers;
}
private ListItemCollection GetSecondList(int iSelectedIndex)
{
ListItemCollection lstRandom = new ListItemCollection();
System.Random RandNum = new System.Random();
for (int i = 0; i < 10; i++)
{
lstRandom.Add(new ListItem(RandNum.Next(ListBox1.SelectedIndex, i + 1).ToString(), "random"));
}
return lstRandom;
}
我刚刚生成了一些随机数字要绑定到列表框。
下面是aspx文件代码,
<form id="form1" runat="server">
<asp:ScriptManager id="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel id="UpdatePanel1" runat="server" updatemode="Conditional" childrenastriggers="true">
<ContentTemplate>
<div>
<asp:ListBox id="ListBox1" autopostback="true" runat="server" onselectedindexchanged="ListBox1_SelectedIndexChanged"
width="200"></asp:ListBox></div>
<div>
<asp:ListBox id="ListBox2" autopostback="true" runat="server" onselectedindexchanged="ListBox2_SelectedIndexChanged"
width="200"></asp:ListBox></div>
<div>
<asp:ListBox id="ListBox3" autopostback="true" runat="server" width="200"></asp:ListBox>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
答案 1 :(得分:0)
实际上,在ASP.NET中,控制事件发生在页面生命周期中的页面加载之后:ASP.NET Page Life Cycle Overview。
我猜(但不确定确切名称)下拉列表应该有SelectedIndexChanged
个事件,您应该考虑采用新的选择。
我希望这有帮助!