ASP.NET - data from the checkbox selected from the grid to be in another page.

Asked By Karthikeyan H on 01-Nov-11 01:16 AM
I want to add checkboxes to the grid. Once the checkboxes are selected, the submit button should redirect another page(or open a new page) with the data selected from the grid. Please help me with the C# coding(ASP.net).

Thanks
H.Karthikeyan
Jitendra Faye replied to Karthikeyan H on 01-Nov-11 01:21 AM
First you need to Add Template Column to your GridView and Put LinkButton on it where you can click and New Page Display the Detail .

<

asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">

<Columns>

<asp:TemplateField>

<EditItemTemplate>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

</EditItemTemplate>

<ItemTemplate>

<asp:LinkButton ID="LinkButton1" runat="server"

CommandName="ViewDetail">LinkButton</asp:LinkButton>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</

asp:GridView>



Now write code in GridView Row Command Event like below

protected void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)

{


if (e.CommandName == "ViewDetail")

{


GridViewRow r = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

string col1Value = r.Cells[0].Text;

string col2value = r.Cells[1].Text;

string col3Value = r.Cells[2].Text;

/* Pass it to Page 2 */

string url = "Page2.aspx?val1=" + col1Value + "&val2=" + col2value + "&val3=" + col3Value;

Response.Redirect(url);

}

}


In Page2.Aspx-


protected

void Page_Load(object sender, EventArgs e)

{


string val1 = Request.QueryString["val1"].ToString();

string val2 = Request.QueryString["val2"].ToString();

string val3 = Request.QueryString["val3"].ToString();


/* Display it in TextBoxes */

TextBox1.Text = val1;

TextBox2.Text = val2;

TextBox3.Text = val3;

}



Try this code and let me know.


Reena Jain replied to Karthikeyan H on 01-Nov-11 01:25 AM
hi,

you can do it like below Store the  row in Session and Get it on the other page
foreach (GridViewRow row in GridView1.Rows)
  {
    /* assum that generate column is on 0 Index */
    if (row.Cells[0].Text == "your value to match")
    {
    /* Store the Data in Session */
    Session["TransferedRow"] = row;
    break;
    }
     
  }

on second page.aspx

/* retrive it in Other WebForm */
  GridViewRow row = (GridViewRow)Session["TransferedRow"];
  
  /* Now you can Use it Like Normal row */
Riley K replied to Karthikeyan H on 01-Nov-11 01:30 AM


After selecting the items you need to code for the
Button server control which will send the selected values to the new
page.


protected void Button1_Click(object sender, EventArgs e)
 
{
 
ArrayList selectedValues = new ArrayList();
 
// This will select multiple items
 
foreach (GridViewRow row in GridView1.Rows)
 
{
 
bool result = ((CheckBox) row.FindControl("CheckBox1")).Checked;
 
if (result)
 
{
 
int categoryID = Convert.ToInt32(((Label)row.FindControl("lblCategoryID")).Text);
 
selectedValues.Add(categoryID);
 
}
 
}
 
// Put the selected values in the Session to be used in a different page
 
Session["SELECTEDVALUES"] = selectedValues;
 
// transfer to a new page
 
Response.Redirect("NewPage.aspx");
 
}

And here is the code for NewPage.aspx:

if (!Page.IsPostBack)
 
{
 
// Check that if the session object contains anything or not
 
if (Session["SELECTEDVALUES"] != null)
 
{
 
ArrayList selectedValues = (ArrayList)Session["SELECTEDVALUES"];
 
// print out the values
 
foreach (int intValue in selectedValues)
 
Response.Write(intValue);
 
}
 
}


Regards
Kirtan Patel replied to Karthikeyan H on 01-Nov-11 01:31 AM
Untitled document
Step 1 : Add Check Box Columns and "View Page" link Column to gridView
 
<asp:GridView ID="GridView1" runat="server">
  <Columns>
    <asp:CheckBoxField HeaderText="Select" />
    <asp:ButtonField CommandName="ViewAnotherPage" Text="View Page" />
  </Columns>
</asp:GridView>
 
 
Step 2 : Write code in GridViewRow Command Event
 
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName == "ViewAnotherPage")
  {
    /* Get Index of the Row */
    int index = (((Button)sender).NamingContainer as GridViewRow).RowIndex;
    string col3val = GridView1.Rows[index].Cells[3].Text;
    Response.Redirect("Page2.aspx?col3=" + col3val);
  }
}
 
Done !
dipa ahuja replied to Karthikeyan H on 01-Nov-11 01:32 PM
Markup To Add A button with CommandNAme :
<ItemTemplate>
  <asp:CheckBox ID="CheckBox2" runat="server" />
   <asp:Label ID="lblID" runat="Server" Text='<%# Eval("ID") %>' />
  <asp:Button ID="btnSubmit" CommandName="Submit" runat="server" Text="Refirect" /> 
</ItemTemplate>

CodeBehind To implement RowCommand Event:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName == "Submit")
  {
    GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
 
    CheckBox chk2 = (CheckBox)row.FindControl("CheckBox1");
    if (chk2.Checked)
    {
      Label lbl = (Label)row.FindControl("lblID");
      Response.Redirect("~/Default.aspx?id=" + lbl.Text);
    }
  }
}
Karthikeyan H replied to dipa ahuja on 02-Nov-11 08:17 AM
Thanks everyone for your fantastic replies :)