Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
function popwin()
    {
     var nw=window.open("","window","width=200,height=300,scrollbar=yes");

nw.location.href="default3.aspx?id=3" ;
  nw=null;  
      }


Here I use id= 3 as static but

I want to pass id from my gridview datakeyname--where I put datakeyname=id....
in default 3 I reterive as

SqlCommand cmd = new SqlCommand("select * from student where id=@id"  ,con);
cmd.Parameters.AddWithValue("@id", SqlDbType.Int).Value = Request.QueryString["id"];
Posted
Updated 25-Aug-10 22:21pm
v2
Comments
Dalek Dave 26-Aug-10 4:21am    
Edited for Code Block and Remove BIG tabs (ugly).

Write javascript to find the data you want and pass it through. Your best bet is to emit the SQL you want, using the data you want in each row on the query string.
 
Share this answer
 
Here is an example implementation. This binds some sample data in the GridView and each row has a "Detail" link. When clicked, the "Detail" link opens a popup window with the Id as parameter.

Basically, the "onclick" javascript attribute is bound with the javascript method popupwin() with the Id parameter in the
RowDataBound event of the GridView (See CodeBehind code)

The Markup:

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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></title>
    <script language="javascript">
        function popwin(id) {
            var nw = window.open("", "window", "width=200,height=300,scrollbar=yes");
            nw.location.href = "default3.aspx?id " + id;
            nw = null;
            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                Width="215px" onrowdatabound="GridView1_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="Name" />
                    <asp:TemplateField ShowHeader="False">
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
                                Text="Detail"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    </div>
    </form>
</body>
</html>


The CodeBehind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    public class Person
    {
        public int Id
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public Person(int Id, string Name)
        {
            this.Id = Id;
            this.Name = Name;
        }
    }
    //Binding some sample data to the GridView
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Populating some dummy data. In reality, you should populate these
            //from the database
            IList items = new ArrayList();

            items.Add(new Person(1, "Peter"));
            items.Add(new Person(2, "John"));
            items.Add(new Person(3, "Shubho"));

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

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton link = e.Row.FindControl("LinkButton1") as LinkButton;
            link.Attributes["onclick"] = "return popwin(" + ((Person)e.Row.DataItem).Id + ")";
        }
    }
}


Hope this will help you implementing your desired functionality.
 
Share this answer
 
Comments
balongi 20-Aug-10 2:55am    
i get this error--Unable to cast object of type 'System.Data.DataRowView' to type 'PropertyLayer.cls_student'.

why writing --
if (e.Row.RowType == DataControlRowType.DataRow)
{

LinkButton link = e.Row.FindControl("View") as LinkButton;
link.Attributes["onclick"] = "return popwin(" + ((cls_student)e.Row.DataItem).id + ")";


}
Al-Farooque Shubho 20-Aug-10 3:14am    
As I can understand, you have a class named as "cls_student"

So, to run the above code correctly, you need to bind the object list of "cls_student" to the GridView.

I bound some objects of "Person" class in my example:

IList items = new ArrayList();

items.Add(new Person(1, "Peter"));
items.Add(new Person(2, "John"));
items.Add(new Person(3, "Shubho"));

GridView1.DataSource = items;
GridView1.DataBind();

Note that, as I am binding the "Person" objects, in the RowDataBound, I am retrieving the object as a "Person" object. So, to retrieve an object as "cls_student", you need to make sure that, you are binding the list of "cls_student" objects to the GridView.
balongi 20-Aug-10 4:12am    
ok thax..... Its work!
u can also do as:
// after giveing datakey name in gridview you can do like:


//in row command:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "View")
{
Response.Redirect("Default3.aspx?id=";+e.CommandArgument);
}


// in rowdatabound:

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
                ((LinkButton)(e.Row.FindControl("view"))).CommandArgument = GridView1.DataKeys[e.Row.RowIndex][0].ToString();
        }
        catch (Exception ex)
        {
        }
    }
 
Share this answer
 
v2

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