0

I have a gridview that's defined like so:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="columnid" DataSourceID="SqlDataSource1"
           <Columns>
        <asp:TemplateField HeaderText="Title">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1"  ForeColor="red"  runat="server" onclick="LinkButton1_Click" 
                   Text="Click Me!"></asp:LinkButton>                  
            </ItemTemplate>
        </asp:TemplateField>

And the codebehind for LinkButton1_click looks like this:

protected void LinkButton1_click(object sender, EventArgs e)
{
    string stringQuery = methodToGetQueryString;
    Response.Redirect(queryString, true);
}

This works awesomely except when I try to open the link as a new tab or a new window, it gives me a about:blank page. I've looked around quite a bit and it seems the general consensus seems that instead of using a linkbutton, I have to use a hyperlink? I've tried to change the code inside my ItemTemplate to look like the following:

 <ItemTemplate>
        <asp:HyperLink ID = "HyperLink1" runat="server" Text="click me!" NavigateURL = "~/setTheUrlHere.aspx" />

 </ItemTemplate>

The problem with this is for some strange reason, none of my hyperlinks are actually clickable. I hover over them and when I right click I don't get the option to open in a new tab or window. It looks like it's just a dead link. Do I need to set the href or something? I thought that's what the NavigateURL would do? Or is there a easier way to do what I need using a LinkButton?

Thanks!

Kevin
  • 3,209
  • 9
  • 39
  • 53
  • Yes that is what NavigateURL does -- are you setting the value on Page_Load? – MikeSmithDev Jan 30 '13 at 14:09
  • I actually figured it out setting the NavigateURL in the .aspx page. How do I set it on Page_load? When I try to type in the ID of my hyperlink into the .aspx.cs page it can't find it? – Kevin Jan 31 '13 at 01:34
  • You have it in a template of a gridview, so you would need to set it on the databound event for that gridview. – MikeSmithDev Jan 31 '13 at 01:45
  • So this is what my code looks like in the .ASPX.CS but it tells me that HyperLink1 does not exist in the current context?: protected void GridView1_DataBound(object sender, EventArgs e) { HyperLink1.NavigateURL = "url" } – Kevin Jan 31 '13 at 01:50
  • How are you determining the URL? Is it based on the datasource results? If so, what column? – MikeSmithDev Jan 31 '13 at 02:06
  • It's been a long time since I've used GridView. But I think its RowDataBound and you'd need to do something like `HyperLink h = (HyperLink)e.Row.FindControl("_h");` and if `h` exists, you can set the NavigateUrl... though I suspect there is a better way to do it, like a `HyperLinkField` instead of a `TemplateField`. – MikeSmithDev Jan 31 '13 at 02:09
  • Yea, it's based on datasource results. Ah ok cool I think I got it. Quick question. If I set the NavigateURL in the c#, do I just not have a NavigateURL = in the .aspx of the Hyperlink? – Kevin Jan 31 '13 at 02:23

1 Answers1

0

If it's interesting I used asp:HyperLinkField colummn like this:

<asp:HyperLinkField DataTextField="Nome_Da_Agencia" DataTextFormatString="Detalhe {0}" DataNavigateUrlFields="Nome_Da_Agencia, Codigo_Da_Agencia" HeaderText="Detalhes"
                     DataNavigateUrlFormatString="Agencia.aspx?codigoAgencia={1}" Target="_blank"  />

atention to Target="_blank" where it tells to open in a new tab