PostBack on Gridview Row Click


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CellClick.aspx.cs" Inherits="CellClick" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
   <title>Untitled Page</title>

   <script language="javascript" type="text/javascript">
       function cellClicked(row, column) {
           myForm.inhAction.value = "cellClicked";
           myForm.inhRow.value = row;
           myForm.inhColumn.value = column;
           document.forms['myForm'].submit();


       }
   </script>

</head>
<body>
   <form id="myForm" runat="server">
       <div>
           <input runat="server" type="hidden" id="inhAction" />
           <input runat="server" type="hidden" id="inhRow" />
           <input runat="server" type="hidden" id="inhColumn" />
           <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
           </asp:GridView>
       </div>
   </form>
</body>
</html>
CodeBehind.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CellClick : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           if (Session["s"] != null)
           {
               GridView1.DataSource = Session["s"] as DataTable;
               GridView1.DataBind();


           }
           else
           {
               GridView1.DataSource = GetCustomMadeDataTable();
               GridView1.DataBind();
           }
       }


   }

   public DataTable GetCustomMadeDataTable()
   {

       //Create a new DataTable object

       System.Data.DataTable objDataTable = new System.Data.DataTable();

       //Create three columns with string as their type

       objDataTable.Columns.Add("Id", typeof(string));
       objDataTable.Columns.Add("FirstName", typeof(string));
       objDataTable.Columns.Add("LastName", typeof(string));
       objDataTable.Columns.Add("Address", typeof(string));
       objDataTable.Columns.Add("Email", typeof(string));
       DataRow dr;
       //Adding some data in the rows of this DataTable
       for (int i = 0; i <= 50; i++)
       {
           dr = objDataTable.NewRow();
           dr[0] = i.ToString();
           dr[1] = "FirstName" + i.ToString();
           dr[2] = "LastName" + i.ToString();
           dr[3] = "Address" + i.ToString();
           dr[4] = "Email" + i.ToString();
           objDataTable.Rows.Add(dr);
       }
       DataColumn[] dcPk = new DataColumn[1];
       dcPk[0] = objDataTable.Columns["Id"];
       objDataTable.PrimaryKey = dcPk;
       Session["dtTemp"] = objDataTable;
       return objDataTable;
   }
   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {

           e.Row.Cells[0].Attributes["onclick"] = String.Format("cellClicked({0},{1})",
e.Row.RowIndex, e.Row.RowIndex);



       }
   }
}
Reactions

Post a Comment

0 Comments

Close Menu