简体   繁体   中英

Getting value from Dropdownlist in Checkboxlist in C#

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.

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;

您还可以使用SELECT元素的id从Form values集合中获取值。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM