Solved

ASP.NET C#: Dynamic text in GridView HyperLinkField

Posted on 2012-03-13
6
884 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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.…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now