提问人:Henry B 提问时间:4/9/2021 最后编辑:Henry B 更新时间:4/10/2021 访问量:69
缺少嵌套在 FindControl 中的内容,但总是得到 Null
Missing something nested in FindControl but always get Null
问:
从字面上看,已经阅读并尝试了 Stackoverflow 中的所有解决方案,但无济于事。
我正在尝试获取从中继器生成的不同下拉列表的值,以及另一个刚刚在旅途中生成的下拉列表的值,简单明了。
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
我正在尝试获取通过 Repeater 生成的所有 dropdownLists 的所有 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;
答:
好的,所以第二个或其他一些中继器问题 - 我们暂时将其分开。
最后一个答案显示了我们如何获取这些值 - 它实际上是相同的代码。
所以,你有一个中继器。它可能有 1 或 15 行。因此,我们希望从中继器的每一行中获取/抓取下拉列表。
所以,假设我们在中继器中有这个标记(我包括了你上面的下拉列表)。
因此,我们有这个简单的标记:
<asp:Repeater ID="Repeater1" runat="server" >
<ItemTemplate>
<div style="border-style:solid;color:black;width:250px">
<div style="padding:5px;text-align:right">
First Name: <asp:TextBox ID="txtFirst" runat="server" Text ='<%# Eval("FirstName") %>' Width="130px" />
<br />
Last Name: <asp:TextBox ID="txtLast" runat="server" Text ='<%# Eval("LastName") %>' Width="130px" />
<br />
<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>
</asp:DropDownList>
<br />
</div>
</div>
</ItemTemplate>
好的,现在这有 2 行还是 30 行都没关系了。假设它有 3 行数据。所以上面现在显示了这一点:
所以说当我们点击上面的按钮时,我们需要代码(与最后一个问题中的代码相同)。因此,对于该按钮单击,处理每一行,获取值(包括下拉列表)的代码可能如下所示:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (RepeaterItem rRow in Repeater1.Items)
{
// get First Name.
Debug.Print(rRow.FindControl("txtFirst") as TextBox.Text);
// get LastName
Debug.Print(rRow.FindControl("txtLast") as TextBox.Text);
// get value of drop down box
string strDropDownChoice = rRow.FindControl("reasonFamily") as DropDownList.Text;
Debug.Print("Drop Down option picked = " + strDropDownChoice);
}
}
上面循环代码的输出是这样的:
Output:
Albert
Kallal
Drop Down option picked = 1
Darcy
Caroll
Drop Down option picked = 2
Don
Grant
Drop Down option picked = 2
因此,再一次,我没有看到任何真正的问题或问题,即遍历数据转发器中的行,然后从文本框、复选框或下拉列表中提取值 - 它们都非常相似。
评论