簡體   English   中英

從C#的復選框列表中的下拉列表中獲取價值

[英]Getting value from Dropdownlist in Checkboxlist in C#

好。 我做了瘋狂的事。 這實際上是正確的,但是您如何使用C#從服務器端的下拉列表中獲取選定的值?

我嘗試獲取下拉列表代碼

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

但是現在有了下拉列表,如何從中獲取所選值? EST 5/15/15 5:39 PM EST我想如果我編寫有關如何創建此代碼的代碼,它將是:

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

我正在使用服務器端代碼動態創建此代碼。

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

如果您看到我正在嘗試做的事情並且知道更好的方法,歡迎提出建議。

如果您要獲取的是所選值,請更改此值

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

為了這

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

並獲取選定的值,執行此操作

string selectedValue = Letter.SelectedValue;

您還可以使用SELECT元素的id從Form values集合中獲取值。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM