缺少嵌套在FindControl中的东西,但始终为Null [英] Missing something nested in FindControl but always get Null

查看:67
本文介绍了缺少嵌套在FindControl中的东西,但始终为Null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从字面上看,都没有读过并尝试过Stackoverflow中的所有解决方案.

Literally have read and tried all solutions in Stackoverflow to no avail.

我正在尝试从中继器中获得不同的Dropdownlists的价值,而在旅途中,简单而又简单地生成另一个Dropdownlists.

I am trying to get the value of different Dropdownlists genereated from a Repeater and another one just generated on the go, plain and simple.

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

我正在尝试通过Repeater生成所有dropdownLists的所有selectedValue's,而这些都是简单而简单的.

I am trying to get all the selectedValue's of all the dropdownLists genereated thru Repeater and the one that is plain and simply there.

                    <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>

在前端看,我得到类似...

which looking in the front end, I am getting something like...

<select name="ctl00$ContentPlaceHolder1$reasonFamily" id="ContentPlaceHolder1_reasonFamily">

看起来不错,因为我做了一个按钮并试图获得此值...

Which looks good, since I did a button and tried to get this value...

DropDownList ctr = Page.FindControl("Content2").FindControl("reasonFamily") as DropDownList;
TextBox.Text = ctr.SelectedValue;

我可以只获取reasonFamily.SelectedValue的值并完成...但是问题是,还有一个带有重复器的部分,该重复器创建了多个DropDownList,我需要找到所有它们并获取它们的所有值,将它们发送到数据库.DropDownList全部嵌套在...

I could just get the value of reasonFamily.SelectedValue and get finished with... But the problem is, there is another section with a repeater that creates several DropDownList and I need to find all of them and get all their values and Send them to the DB. The DropDownList is all nested in...

<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.

I tried getting finding all of the dropdownlist, but all I get is NULL.

更新:我能够得到不在中继器中的一个,但是其他仍然困扰着我,即使我几乎已经尝试了所有可能的选择.

UPDATE: I was able to get the one that is not in the Repeater, but other still eludes me, even tho I have almost tried all the possible choices.

<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;

推荐答案

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

Ok, so the 2nd or some other repeater issue - we leave that separate for now.

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

that last answer SHOWS how we can grab those values - it really the same code.

因此,您有一个中继器.它可能有1或15行.因此,我们要从转发器的每一行获取/获取下拉列表.

So, you have a repeater. It has maybe 1 or 15 rows. So, we want to get/grab the drop down list from each row of the repeater.

因此,假设我们在转发器中有此标记(我已在上面的下拉列表中添加了此标记).

So, say we have this markup in the repeater (I included your above dropdown list).

因此,我们有一个简单的标记:

So, we have this simple markup:

<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行数据.所以上面的内容显示了这一点:

Ok, now it don't matter if this has 2, or 30 rows. Lets assume it has 3 rows of data. so the above now shows this:

所以说当我们单击上面的按钮时,我们需要代码(最后一个问题中的SAME代码).因此,用于处理每一行,获取值(包括下拉列表)的代码对于该按钮单击而言可能是这样的:

So say when we click on the above button, then we need code (the SAME code in the last question). so the code to process each row, get the values (including the dropdown) could be like this for that button click:

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);
  }
}

上面的循环代码的输出是这样的:

And output from above loop code would be this:

Output:
Albert
Kallal
Drop Down option picked = 1

Darcy
Caroll
Drop Down option picked = 2

Don
Grant
Drop Down option picked = 2

因此,再次遍历数据转发器中的行,然后从文本框,复选框或下拉列表中拉出值,我看不到任何真正的问题或问题,仅此而已差不多.

So, once again, I don't see any real issue or problem with looping over the rows in the data repeater, and then pulling out the values from text box, check box, or a drop down list - it is all quite much the same.

这篇关于缺少嵌套在FindControl中的东西,但始终为Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆