Solved

ASP.NET C#: Dynamic text in GridView HyperLinkField

Posted on 2012-03-13
6
894 Views
Last Modified: 2012-03-13
Hello All

I have a GridView on an ASP.NET C# website that contains data from a database. In the GridView I have a HyperLinkField.

What I want to do is change the text of the HyperLinkField depending on the value of one of the GridView Columns (if the column is 0 the HyperLinkField text should be "Activate" and if the value is 1 the text should be "Block").

Does anyone know how this can be done?

Regards
Søren Augustesen
0
Comment
Question by:poultarp
  • 3
  • 2
6 Comments
 
LVL 7

Accepted Solution

by:
markmiddlemist earned 500 total points
ID: 37713648
What you will need to do is convert your hyperlinkfield to a templatefield (containing a HyperLink) and handle the onRowDataBound event of the grid, an

so your markup would contain something like:

<ItemTemplate>
    <asp:HyperLink ID="HyperLinkBlockActivate" runat="server" NavigateUrl='<%# Eval("ID", "~/DoSomething.aspx?ID={0}") %>' />
</ItemTemplate>

and your code for the eventhandler would be:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
     //Get the data row that is the source for this row
      DataRowView drv = ((DataRowView)e.Row.DataItem);

   //Get a reference to the hyperlink
   HyperLink HyperLinkBlockActivate = e.Row.FindControl("HyperLinkBlockActivate")

    //Check your trigger value and set the text
    if ((bool)drv["BitField"])
    {
        HyperLinkBlockActivate.Text = "Block";
    }
    else
    {
        HyperLinkBlockActivate.Text = "Activate";
    }
  }
}

There's also some good explanations and examples at http://www.simple-talk.com/dotnet/asp.net/take-row-level-control-of-your-gridview/ 

Let me know if you do have any issues

Mark (@delradie)
0
 
LVL 11

Expert Comment

by:rajvja
ID: 37713650
Hi

you can use this

Text='<%# Eval("colname")%> == "1" ? "Yes" : "No"

this is just example
0
 
LVL 1

Author Comment

by:poultarp
ID: 37713793
Hello Mark

Thank you for your answer.

I have one quick question:

In the <%Eval %> line, how do I pass more than one value to the URL parameters?

Regards
Søren Augustesen
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 7

Assisted Solution

by:markmiddlemist
markmiddlemist earned 500 total points
ID: 37713811
You could modify the NavigateUrl property in the rowdatabound as well as the text, or you could go for something like the following in the markup:

NavigateUrl= '<%# String.Format"~/DoSomething.aspx?ID1={0}&ID2={1}", HttpUtility.UrlEncode(Eval("ID")), HttpUtility.UrlEncode(Eval("SomethingElse"))) %>'
0
 
LVL 1

Author Closing Comment

by:poultarp
ID: 37714065
Thank you Mark - it works just as I want it to!

Regards
Søren Augsutesen
0
 
LVL 7

Expert Comment

by:markmiddlemist
ID: 37717868
Glad to help. It's a very useful technique, and one I use almost daily.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Deploying to Azure 3 29
Question about JQuery and asp.net 3 27
VB.Net WebRequest Code from JSON curl 9 13
Convert datetime to time string 10 21
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question