Click here to Skip to main content
16,018,797 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, how to pass GridView cell value to popup window ?

I am trying something like this but it is not successfull...

ASP.NET
       <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function popwin(id) {
            var nw = window.open("", "window", "width=200,height=300,scrollbar=yes");
            nw.location.href = "Default.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="UserName" 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>






C#
     using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class _Default : System.Web.UI.Page
{    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                using (SqlCommand cmd = new SqlCommand())
                {
                    string cmdText = "Select * from login";
                    DataTable dt = new DataTable();
                    cmd.CommandText = cmdText;
                    cmd.Connection = conn;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    conn.Open();
                    da.Fill(dt);

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

                     conn.Close();
                }
            }
            
        }
    }

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

1 solution

No change to the aspx UI page. I just modified the code behind page. Please have look in the RowDataBound method.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Demo : System.Web.UI.Page
{
    List<user> userList = new List<user>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            userList.Add(new User() { UserName = "Alex" });
            userList.Add(new User() { UserName = "Bob" });
            userList.Add(new User() { UserName = "Jeniffer" });
            GridView1.DataSource = userList;
            GridView1.DataBind();
        }

    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton link = e.Row.FindControl("LinkButton1") as LinkButton;
            string name = e.Row.Cells[0].Text;
            link.Attributes["onclick"] = "return popwin('" + name + "')";
        }
    }
}

public class User
{
    public string UserName  { get; set; }
}
 
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