How to select more than one checkbox in gridview and show the details in another Gridview in asp.net with c#
In my project i have a need to select some check boxes in a GridView and show the details in another gridview. I use an arraylist to k开发者_开发知识库eep all the values of the checkbox using the foreach loop, but i can't able to display the records in another Grid view. Any help will be highly appreciated.
Try this sample and use as per your requirement
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
</asp:CheckBoxList>
<asp:Button ID="Button1" runat="server" Text="Navigate" OnClick="Button1_Click" />
</form>
</body>
</html>
Code
private void PassValues()
{
int counter = 0;
ArrayList arr = new ArrayList();
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
if (ViewState["CountLimit"] != null)
{
counter++;
ViewState["CountLimit"] = counter;
}
else
{
counter++;
ViewState["CountLimit"] = counter;
}
if (counter < 2)
{
//Display message if user selects only 1 item
Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowBox", "alert('You should select at least 2 items!');", true);
//Save the selected items in the Array
arr.Add(CheckBoxList1.Items[i].Text);
}
else
{
//Save the selected items in the Array
arr.Add(CheckBoxList1.Items[i].Text);
//Store the values in Session so that we can get the values on the page
Session["Values"] = arr;
//Navigate to the next page
Response.Redirect("Default4.aspx");
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
PassValues();
}
On Page2 write the following code
protected void Page_Load( object sender, EventArgs e )
{
if (Session["Values"] != null)
{
ArrayList arr = (ArrayList)Session["Values"];
foreach (string item in arr)
{
Response.Write(item + "<BR/>");
}
}
}
精彩评论