Missing something nested in FindControl but always get Null

86 views Asked by At

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

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

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;

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>

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

There are 1 answers

0
Albert D. Kallal On

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.

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>

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:

enter image description here

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.