6

I have an ASP.NET Web Forms application. In my application I have a GridView that works smoothly. I have several text fields and the last one is a <asp:hyperlinkfield>.

Now I would like to programmatically change the field by placing a simple link instead of the hyperlinkfield if a specific condition is fulfilled. Therefore I catch the onRowDataBound event:

Sub myGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles myGridView.RowDataBound

    If (condition) Then
           Dim link = New HyperLink()
           link.Text = "login"
           link.NavigateUrl = "login.aspx"
           e.Row.Cells(3).Controls.Add(link)
        End If
    End If
End Sub

where n is the cell where the hyperlinkfield is placed. With this code it just adds to the hyperlinkfield the new link. How can I replace it?

PS: The code is in VB6 but I am a C# programmer, answers with both languages are accepted

Taz
  • 3,718
  • 2
  • 37
  • 59
CiccioMiami
  • 8,028
  • 32
  • 90
  • 151

5 Answers5

7

Remove the control you want to replace from the collection before adding the new one:

protected void TestGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    HyperLink newHyperLink = new HyperLink();
    newHyperLink.Text = "New";
    e.Row.Cells[3].Controls.RemoveAt(0);
    e.Row.Cells[3].Controls.Add(newHyperLink);
  }
}

But I agree with the others, just change the existing link's properties:

protected void TestGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    HyperLink link = e.Row.Cells[0].Controls[0] as HyperLink;
    if (link != null)
    {
      link.Text = "New";
      link.NavigateUrl = "New";
    }
  }
}
dugas
  • 12,025
  • 3
  • 45
  • 51
  • I give you the answer but the first one is the one I was looking for. The main reason is that my `HyperLinkField` has other attributes linked to data and therefore I cannot just replace Text and NavigateUrl. The surprising thing is that none noticed that the control I had to remove was an `HyperLinkField` which differs from `HyperLink`. I guess the first solution works smooth unless there is some JavaScript connected to the data refresh of the `GridView` but it is not my case. Many Thanks! – CiccioMiami Feb 29 '12 at 16:46
7

In situations like that I typically convert the bound field to a templated field.

 <asp:TemplateField HeaderText="Title" SortExpression="Title">
    <ItemTemplate>
       <asp:HyperLink ID="TitleHyperLink" runat="server" ></asp:HyperLink>
    </ItemTemplate>
 </asp:TemplateField>

And do the rest of the work in the codebehind.

protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var link = (HyperLink)e.Row.FindControl("TitleHyperLink");

        if (link != null)
        {
            if (condition)
            {
               link.Text = "login";
               link.NavigateUrl = "login.aspx";
            }
            else 
            {
               link.Text = "default text";
               link.NavigateUrl = "default.aspx";
            }
        }
    }
}
lincolnk
  • 11,218
  • 4
  • 40
  • 61
  • 2
    thanks this solution is nice but the annoying thing is that hyperlinkfield does not have an Id property – CiccioMiami Feb 29 '12 at 16:17
  • What @lincolnk is saying is use a TemplateField, then you can use a HyperLink instead of a HyperLinkField, which will have an Id. – dugas Feb 29 '12 at 16:23
1

Instead of creating a new link at this point, grab the link that's already generated as part of the field.

If (e.Row.RowType = DataControlRowType.DataRow) Then
    Dim link = e.Row.Cells(3).Controls(0)
    link.Text = "login"
    link.NavigateUrl = "login.aspx"
End If

EDIT: Added If block to avoid action outside item rows.

Mike Guthrie
  • 4,029
  • 2
  • 25
  • 48
1

You could do this in your aspx file :

<asp:HyperLink Text='<%# condition ? "TextIfTrue" : "TextIfFalse" %>' NavigateUrl='<%# condition ? "UrlIfTrue" : "UrlIfFalse" %>' />

or cast your

e.Row.Cells(3).Controls(0)

into a hyperlink and change its values.

JoRouss
  • 2,864
  • 2
  • 22
  • 40
  • thanks. I already thought about that but the conditions are complex. Moreover I always try to put code in the code behind, do not like "dirty" aspx – CiccioMiami Feb 29 '12 at 15:51
0

You can use in the aspx:

<asp:HyperLink ID="HyperLink1" CssClass="exercise" runat="server" NavigateUrl="#">Search ¡here!</asp:HyperLink>

In the codebehind: You can use also a method:

public string SharePoint(string x)
{

   string page1, page2;

                if (x== "1")
                {
                    string page1="http://nwpage/files.zip";

                    return page1;
                }

                else
                { 


                    string page2="http://example2.aspx";
                    return page2;
                }


        }

If I call the control in the other method or page load, you can add HyperLink1 with the path

 string path= SharePoint(x);
 HyperLink1.NavigateUrl = path;