i have a hyperlink called companyname in gridview .when i click it has to open popup window and provides details of that company.can someone explain how to call popup window in hyperlink

Dani AI

Generated

If a simple new tab works for you, bind a HyperLinkField in the GridView so you can pass the CompanyID without any JavaScript. This keeps things accessible and avoids popup blockers.

<asp:HyperLinkField
    DataTextField="CompanyName"
    DataNavigateUrlFields="CompanyID"
    DataNavigateUrlFormatString="CompanyDetails.aspx?id={0}"
    Target="_blank" />

If you really need a popup with size/position, avoid inline JS and Response.Write. Use a TemplateField with a LinkButton and set its client script in RowDataBound so the click is a direct user action (not blocked by browsers), URL-encode the ID, and reuse a named window per company.

<asp:TemplateField HeaderText="Company">
  <ItemTemplate>
    <asp:LinkButton ID="lnkCompany" runat="server"
        Text='<%# Eval("CompanyName") %>' />
  </ItemTemplate>
</asp:TemplateField>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType != DataControlRowType.DataRow) return;

    var idObj = DataBinder.Eval(e.Row.DataItem, "CompanyID");
    var id = idObj == null ? string.Empty : idObj.ToString();

    var link = (LinkButton)e.Row.FindControl("lnkCompany");
    var url = ResolveUrl("~/CompanyDetails.aspx?id=" +
                         HttpUtility.UrlEncode(id));
    var winName = "company_" + id; // reuse one window per company

    link.OnClientClick =
        "window.open('" + url + "', '" + winName + "', " +
        "'popup,width=600,height=450,scrollbars=1,noopener,noreferrer'); return false;";
}

Notes:

Recommended Answers

All 5 Replies

in the target property of the field, set it to _blank

Hi... I would suggest passing the CompanyID in Session -- Session.Add("CompanyID") -- and retrieving the companyid -- Session.Item("ComanyID") from the popup.

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles GridView1.RowCommand

        If GridView1.Rows.Count - 1 > 0 Then

            If e.CommandName = "WHATEVERYOURBUTTONCOMMANDNAME" Then

 Dim CompanyID As String = GridView1.DataKeys(Convert.ToString(e.CommandArgument)).Item(0).ToString

Session.Add("CompanyID", CompanyID)

               Response.Write("<script>")
                Response.Write("window.open(YOURDETAILSWINDOW.aspx','_blank','toolbar=0,location=0,menubar=0,resizable=0,width=400,height=320')")
                Response.Write("</script>")

end if

end if

<asp:TemplateField HeaderText="Header">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
<ItemTemplate>

<a onclick="javascript:window.open('PricePage.aspx?id=<%#DataBinder.Eval(Container.DataItem,"Columnname")%>&show=<%#DataBinder.Eval(Container.DataItem,"Columnname")%>');" href="#" id="a6" ><%#DataBinder.Eval(Container.DataItem, "Columnname")%></a>


</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
<ItemTemplate>

<a onclick="javascript:window.open('PricePage.aspx?id=<%#DataBinder.Eval(Container.DataItem,"Columnname")%>&show=<%#DataBinder.Eval(Container.DataItem,"Columnname")%>');" href="#" id="a11" ><%#DataBinder.Eval(Container.DataItem, "Columnname")%></a>

</ItemTemplate>
</asp:TemplateField>
hi u can use this code and pass query string

Hi You can do that by using JavaScript's OpenWindow() method and set the attribute values properly so that you get what exactly you want..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.