【发布时间】:2021-07-04 20:54:35
【问题描述】:
实际上已经阅读并尝试了 Stackoverflow 中的所有解决方案,但均无济于事。
我正在尝试获取从中继器生成的不同下拉列表的值,以及在旅途中生成的另一个下拉列表的值,简单明了。
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
我正在尝试获取通过 Repeater 生成的所有下拉列表的所有 selectedValue,以及一个简单明了的下拉列表。
<asp:DropDownList ID="reasonFamily" runat="server">
<asp:ListItem Enabled="true" Text="--------" Value="-1"></asp:ListItem>
<asp:ListItem Text="Option 1" Value="1"></asp:ListItem>
<asp:ListItem Text="Option 2" Value="2"></asp:ListItem>
从前端看,我得到的是……
<select name="ctl00$ContentPlaceHolder1$reasonFamily" id="ContentPlaceHolder1_reasonFamily">
这看起来不错,因为我做了一个按钮并试图获得这个值......
DropDownList ctr = Page.FindControl("Content2").FindControl("reasonFamily") as DropDownList;
TextBox.Text = ctr.SelectedValue;
我可以获取 reasonFamily.SelectedValue 的值并完成...但问题是,还有一个带有转发器的部分,它创建了几个 DropDownList,我需要找到所有这些并获取它们的所有值和将它们发送到数据库。 DropDownList 全部嵌套在...
<asp:Repeater ID="repStudents" runat="server">
<asp:DropDownList ID="reasonStd" runat="server">
<asp:ListItem Enabled="true" Text="--------" Value="-1"></asp:ListItem>
<asp:ListItem Text="Option 1" Value="1"></asp:ListItem>
<asp:ListItem Text="Option 2" Value="2"></asp:ListItem>
我尝试查找所有下拉列表,但我得到的都是 NULL。
更新:虽然我几乎尝试了所有可能的选择,但我仍然能够获得不在中继器中的那个。
<select name="ctl00$ContentPlaceHolder1$reasonFamily" id="ContentPlaceHolder1_reasonFamily"> // This I was able to access with:
DropDownList ctr = this.Master.FindControl("ContentPlaceHolder1").FindControl("reasonFamily") as DropDownList;
<select name="ctl00$ContentPlaceHolder1$repStudents$ctl01$reasonStd" id="ContentPlaceHolder1_repStudents_reasonStd_1"> //This I could not. Tried all of this:
DropDownList ctr = this.Master.FindControl("ContentPlaceHolder1").FindControl("reasonStd_1") as DropDownList;
DropDownList ctr = this.Master.FindControl("ContentPlaceHolder1").FindControl("repStudents").FindControl("reasonStd_1") as DropDownList;
DropDownList ctr = this.Master.FindControl("ContentPlaceHolder1").FindControl("repStudents")FindControl("reasonStd").FinControl("1") as DropDownList;
【问题讨论】:
-
难以从中继器中掌握您想要的哪一行?或者你想要所有的行?最好将每个中继器集视为一行信息。那么我们想要从该数据的“n”行中找出哪一行?我认为这个问题与从重复创建显示的行“集”中的下拉列表中抓取一个文本框、一个复选框或一个值没有太大不同。所以下拉列表或文本框不应该有任何区别。我们需要求助于解析或添加 ctrl100$ 内容的情况非常少见——它不应该是必需的。这是数据行。
-
转发器只是创建“n”行,每行包含一个下拉列表,我需要的是获取每个转发器中生成的每个下拉列表的值。比如说,我的中继器中有 3 个项目,并且生成了 3 个下拉列表“repStudents_reasonStd_0”、“repStudents_reasonStd_1”、“repStudents_reasonStd_2”。我尝试使用编辑中解释的 findControl 来获取它们,但我得到的只是空值。显然, findControl 无论如何都找不到它们。开始认为我应该使用其他方法:(
标签: asp.net repeater findcontrol