0

Ok. I did something crazy. This actually renders correct but how would you get the selected value from the dropdown from the server-side using C#?

I tried getting the dropdownlist code

CheckBoxList.Items[0].Text.Substring(CheckBoxList.Items[0].Text.indexOf("<select>")); 

But now that I have the dropdown, how do I get the selected value from it? EDIT 5/15/15 5:39PM EST I think it would if I wrote the code as to how I am creating this:

CheckBoxList chkBoxLst = new CheckBoxList();
chkBoxLst.Items.Add("Grade");
chkBoxLst.Items.Add("2");
chkBoxLst.Items.Add("3");

chkBoxLst.Items[0].Text += "<select id='Letter' runat='server'>
            <option>A</option>
            <option>B</option>
            <option>C</option>
            </select>"

I am creating this dynamically with server-side code.

<asp:CheckBoxList ID="CheckBoxList1" runat="server">
        <asp:ListItem>Grade <select id="Letter" runat="server">
            <option>A</option>
            <option>B</option>
            <option>C</option>
            </select>
        </asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
    </asp:CheckBoxList>

If you see what I am trying to do and know a better way, suggestions welcome.

Kyle Johnson
  • 763
  • 1
  • 13
  • 31
  • Did you try ? http://stackoverflow.com/questions/9523263/how-can-i-get-the-checkboxlist-selected-values-what-i-have-doesnt-seem-to-work – kanchirk May 14 '15 at 20:39

2 Answers2

3

If what you are trying to accomplish is get the selected value, change this

<select id="Letter" runat="server">
    <option>A</option>
    <option>B</option>
    <option>C</option>
</select>

for this

<asp:DropDownList ID="Letter" runat="server" >
    <asp:ListItem Text="A" Value="A"></asp:ListItem>
    <asp:ListItem Text="B" Value="B"></asp:ListItem>
    <asp:ListItem Text="C" Value="C"></asp:ListItem>
</asp:DropDownList>

and to get the selected value do this

string selectedValue = Letter.SelectedValue;
Enrique Zavaleta
  • 2,098
  • 3
  • 21
  • 29
2

You can also get the value from the Form values collection using the id for the SELECT element.

var val = Request.Form["Letter"];
T McKeown
  • 12,971
  • 1
  • 25
  • 32