缺少嵌套在 FindControl 中但始终为 Null 的内容

时间:2021-04-08 23:50:15

标签: asp.net repeater findcontrol

实际上已经阅读并尝试了 Stackoverflow 中的所有解决方案都无济于事。

我正在尝试获取从中继器生成的不同下拉列表的值,以及在旅途中生成的另一个下拉列表的值,简单明了。

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

我正在尝试获取通过中继器生成的所有下拉列表的所有 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 个答案:

答案 0 :(得分:0)

好的,那么第二个或其他一些中继器问题 - 我们暂时将其分开。

最后一个答案展示了我们如何获取这些值 - 这确实是相同的代码。

所以,你有一个中继器。它可能有 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 行数据。所以上面现在显示了这一点:

enter image description here

所以说当我们点击上面的按钮时,我们需要代码(最后一个问题中的相同代码)。所以处理每一行的代码,获取值(包括下拉列表)对于那个按钮点击可能是这样的:

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

所以,再一次,我没有看到任何真正的问题或问题,即循环遍历数据转发器中的行,然后从文本框、复选框或下拉列表中提取值 - 就是这样大同小异。

相关问题