Open popup window on click of gridview linkbutton

(ASP.NET)


Hi,
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

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

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

ProductPopup.aspx page

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   <asp:Label ID="lbl_productid" runat="server" />
    </div>
    </form>
</body>
</html>



Codebehind file:-

        {
            try
            {
                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: window.open('"+url+"','','_blank','width=500,height=245,left=350,top=400')");
                }
            }
            catch (Exception)
            { }
        }

Image

Ratings


Average Rating: 0.00 by 0 users
Giri Prasad
6/10/2013
6/10/2013
Download PDF

Comments