0

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;
Henry B
  • 29
  • 8
  • Having difficulty grasping which row you want from repeater? Or you want all rows? It is better to think of each repeater set as a row of information. So which particular row we want out of the "n" rows of that data? I don't see this issue as much different as grabbing say one text box, one check box or one value from a drop down list from that "set" of rows that repeats to create your display. so the dropdown list or text box should not make any difference. Very rare that we need to resort to parsing or adding the ctrl100$ content stuff - it should not be required. This is rows of data. – Albert D. Kallal Apr 09 '21 at 06:06
  • The repeater is just creating "n" amount of rows that each contains a dropdownlist and what I need is to get the value of each dropdownlist generated in each repeater. Say, there are 3 items in my repeater and there are 3 dropdown generated "repStudents_reasonStd_0", "repStudents_reasonStd_1", "repStudents_reasonStd_2". I tried getting them with the findControl explained in the edit, but all I am getting are just null. Apparently, the findControl is not finding them anyhow. Starting to think I should just use other approach :( – Henry B Apr 09 '21 at 13:57

1 Answers1

0

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.

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51