Click here to Skip to main content
15,947,957 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In gridview, I want to open another form when user clicks on particular row and in new form I want to show the record as selected on particular row by user by passing primary key for one page to new page... Can anyone tell me on how to do this.. Is there any gridview event to accomplish this on click of row.. i donot prefer using item template and link button in it.. Any help???
Posted

HI
Follow these steps.

ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%"
       OnRowDataBound="GridView1_RowDataBound">
       <Columns>
           <asp:BoundField HeaderText="SomeColumn" DataField="SomeColumn" />

       </Columns>
   </asp:GridView>


1) create a OnRowDataBound event to the grid view as above.

2) in the server event add onclick event to the rows as follows

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               string primaryKey = DataBinder.Eval(e.Row.DataItem, "PrimaryKeyID") + "";
               e.Row.Attributes.Add("onclick", "opennewwindow('" + primaryKey + "')");

           }

       }


3) add javascript to open a new window , by passing the primary key ID as parameter.

XML
<script type="text/javascript">
        var opennewwindow = function (primarykey) {
            alert(primarykey);
            window.location = "Job.aspx?primarykey=" + primarykey;
        }
    </script>



sample input to run the above:

C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {
               DataTable dt = new DataTable();
               dt.Columns.Add("PrimaryKeyID", typeof(int));
               dt.Columns.Add("SomeColumn", typeof(string));
               dt.Rows.Add(1,"aaaaaaaa");
               dt.Rows.Add(2,"bbbbbbbbb");


               GridView1.DataSource = dt;
               GridView1.DataBind();
           }
       }


read the query string values in next page as

C#
public partial class Job : System.Web.UI.Page
   {
       protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {

            int primarykey = Convert.ToInt32(Request.QueryString["primarykey"]);      
               // your code....
           }
       }
 
Share this answer
 
v2
Comments
Codes DeCodes 2-Jan-14 1:02am    
thanks for the code karthik.. my javascript code is
<script type="text/javascript">
var opennewwindow = function (primarykey) {
Session["Entry_No"] = Entry_No;
Response.Redirect("Job.aspx");
}
</script>
this code gave me the entry no as desired butpage is not directed to Job.aspx.. any help..
Karthik_Mahalingam 2-Jan-14 1:04am    
you cannot use session or server code in javascript ...
Codes DeCodes 2-Jan-14 1:07am    
then i should use querystring??? does javascript allows Response.Redirect("Job.aspx");??
Karthik_Mahalingam 2-Jan-14 1:06am    
you want to create a popup window or redirect to new page ??
Codes DeCodes 2-Jan-14 1:08am    
i want to redirect to new page... how can i do it?
Hi You should assign primarykey column as DataKeynames and create selectedindexchanged event for that gridview.
Then in that event youu can write
int id = GridView1.SelectedDataKey.Value;

by using this you will get id of that selected row and then using querystring you will post that id to new page using response.redirect o/w storing that id value in session variable.

And next page you can use that id and fetch data according to that id from Database and display on page.
 
Share this answer
 
Comments
Codes DeCodes 2-Jan-14 0:24am    
BK 4 code. thanks for your solution. I did it in your way but selectedindexchanged is not fired when i click on the particular row in gridview.. did i missed something??? is there some other features like autopostback = true that i missed out...???
BK 4 code 2-Jan-14 0:28am    
Hi did you use <asp:CommandField ShowSelectButton="True" /> as select button ?

Use this command field if you did not use it.
Codes DeCodes 2-Jan-14 0:54am    
this code created a select button as a column.. bt I dont want this to be created.. what i wanted is to select the row and not to add any other item and use it as a means to pass to other page.
BK 4 code 2-Jan-14 1:13am    
Then you should go for runtime binding javascript to created row.
Add following script to page header
<script type="text/javascript">
function rowSelection(PK) {
location.href='http://localhost/Nextpage.aspx?id='+PK;
}
</script>
Create onrowdatabound event and put following code in that event
where card_type_id is your primarykey column name
if (e.Row.RowType == DataControlRowType.DataRow)
{
string PK = DataBinder.Eval(e.Row.DataItem, "card_type_id") + "";
e.Row.Attributes.Add("onclick", "rowSelection('" + PK + "')");

}
BK 4 code. thanks for your solution. I did it in your way but selectedindexchanged is not fired when i click on the particular row in gridview.. did i missed something??? is there some other features like autopostback = true that i missed out...???
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900