【问题标题】:Missing something nested in FindControl but always get Null缺少嵌套在 FindControl 中的东西,但总是得到 Null
【发布时间】: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


【解决方案1】:

好的,所以第二个或其他中继器问题 - 我们暂时将其分开。

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

所以,你有一个中继器。它可能有 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

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

【讨论】:

    猜你喜欢
    • 2012-07-20
    • 2012-08-18
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    相关资源
    最近更新 更多