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.

1

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

Comments

1

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

var val = Request.Form["Letter"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.