Open popup window on click of gridview linkbutton


In this article i am opening popup on click of gridview linkbutton.

I placed link button inside the gridview using its templatefield. On the gridview rowdatabound event i am checking whether the rowtype is datarow or not. If rowtype is datarow,  i get the link button control reference and adding the onclick attributes to that link button. The onclick attributes value is set to the javascript function which opens the popup window.

.aspx page

        <asp:GridView DataSourceID="sqlds_prod" OnRowDataBound="grdvw_products_RowDataBound" AllowPaging="true" AllowSorting="true" PageSize="10" AutoGenerateColumns="false" ID="grdvw_products" runat="server">
                <asp:TemplateField HeaderText="Product ID">
                        <asp:LinkButton ID="lnk_productid" Text='<%#Eval("ProductID") %>' runat="server" />
               <asp:BoundField DataField="ProductName"  HeaderText="ProductName" />
               <asp:BoundField DataField="SupplierID"  HeaderText="SupplierID" />
               <asp:BoundField DataField="CategoryID"  HeaderText="CategoryID" />

         <asp:SqlDataSource ID="sqlds_prod" runat="server" ConnectionString="<%$ ConnectionStrings:Northwindconstring %>"
            SelectCommand="SELECT * FROM Products" />

ProductPopup.aspx page

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
   <asp:Label ID="lbl_productid" runat="server" />

Codebehind file:-

                if (e.Row.RowType == DataControlRowType.DataRow)

                    LinkButton lnk = (LinkButton)e.Row.FindControl("lnk_productid");

                    string url="ProductPopup.aspx?ProductID="+lnk.Text;

                    lnk.Attributes.Add("onClick", "JavaScript:'"+url+"','','_blank','width=500,height=245,left=350,top=400')");
            catch (Exception)
            { }



Average Rating: 0.00 by 0 users
Giri Prasad
Download PDF