Monday, April 22, 2013

how to get selected items from checkboxlist in asp.net c#


In previous post i have given how to get the id of checkbox using jqury ,check all checkbox in gridview.In this post i will show how to get the selected values of check box list in asp.net.I below piece of code i have looping thorough the check box list items on button click event .Then the resultant value has assigned to string
<html>
<head>
</head>
<body>
<form>
<asp:CheckBoxList ID="ChkOrdrList" runat="server">
<asp:ListItem>COmputers</asp:ListItem>
<asp:ListItem>Books</asp:ListItem>
<asp:ListItem>Laptop</asp:ListItem>
<asp:ListItem>Ipad</asp:ListItem>
</asp:CheckBoxList>
<div>   
<asp:Button ID="btngetOrdr" Text="Ok" OnClick="btlgetOrder_Click" runat="server" />
</div>
<asp:TextBox ID="txtOrdername" runat="server" />
</form>
</body>
</html>

public void btlgetOrder_Click(object Source, EventArgs e)
{
String strOrdr = "Checkboxlist selected Item is:";
for (int i = 0; i < ChkOrdrList.Items.Count; i++)
{
if (ChkOrdrList.Items[i].Selected)
{
strOrdr = strOrdr +","+ ChkOrdrList.Items[i].Text;
}
}
txtOrdername.Text = strOrdr;
}

No comments:

Bel