开发者

how to retrieve multiple selected value from asp:checkbox .net c#

Does anyone know how can I retrieved the multiple selected value from asp:checkbox .net c#?

Example: I'm new in .net c#, I have the following code, but I have no idea how can I retrieved the multiple selected value from .net c#

<tr>   
    <th class="graytext r">Add Test:</th>
    <td>
        <asp:CheckBoxList ID="Test" runat="server" DataSourceID="dsTest" CssClass=""
            DataValueField="employeeid" DataTextField=开发者_如何学JAVA"fullname" 
            AppendDataBoundItems="false" >
            <asp:ListItem></asp:ListItem>
        </asp:CheckBoxList>  
        <asp:SqlDataSource ID="dsTest" runat="server" 
            ConnectionString="<%$ ConnectionStrings:SmartStaffConnectionString %>"
            SelectCommand="app_dsTest_select" SelectCommandType="StoredProcedure">
        </asp:SqlDataSource>
    </td>
</tr>  


Use the following:

for (int i=0; i<checkboxlist1.Items.Count; i++)
{
    if (checkboxlist1.Items[i].Selected)
    {
        Message.Text += checkboxlist1.Items[i].Text + "<br />";
    }
}

Refer to CheckBoxList Class.


Propably the simplest approach is this:

foreach (ListItem item in myCheckboxList.Items)
{
  if (item.Selected)
  {
    // do something with this item
  }
}


This is an old thread, but using .NET 4.5 (not sure if previous versions work), you can use LINQ to do this:

IEnumerable<ListItem> selectedItems = myCheckboxList.Items.Cast<ListItem>().Where(x => x.Selected);


try listitem.Selected property as i did below

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    Label1.Text = string.Empty;

    foreach (ListItem listitem in CheckBoxList1.Items)
    {
        if (listitem.Selected)
            Label1.Text += listitem.Text + "<br />";
    }
}


foreach (ListItem item in myCheckboxList.Items)
{
  if (item.Selected)
  {
    //Your code goes here
  }
}


You must iterate through the Items.

Refer to CheckBoxList Class.

To determine which items are checked, iterate through the collection and test the Selected property of each item in the list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜