Open a Popup window from a gridview asp:hyperlink control in C#

In this asp.net tutorial you will learn how to Popup a window from a gridview asp:hyperlink control. Sometimes suring development you may need to Popup a window from a gridview asp:hyperlink control. Let's have a look over how to do such.

Yourpage.aspx


<asp:GridView ID="GridView1" runat="server" EnableTheming="false" GridLines="None" AutoGenerateColumns="false" Width="100%" OnRowDataBound="GridView1_RowDataBound">
<Columns>

<asp:TemplateField>
<ItemTemplate>
<asp:Label id="lblFormID" runat="server" Text="<%# Bind('ID') %>" Visible="false">

</asp:Label>
<asp:HyperLink ID="hypComment" runat="server" Text="<%# Bind('Comments') %>" style="cursor:pointer;">
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


YourPage.aspx.cs

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Now Want to open Popup window from hyper link
string formid = ((Label)e.Row.FindControl("lblFormID")).Text;
urlWithParams = "
yourpage.aspx?id="+formid;
((HyperLink)e.Row.FindControl("hypComment")).Add("OnClick", "window.open('" + urlWithParams + "','PopupWindow','width=400,height=200')");
//Ends Here
}
}

So this is the way to Popup a window from gridview asp:hyperlink control.

0 comments: