?
Solved

ASP.NET C#: Dynamic text in GridView HyperLinkField

Posted on 2012-03-13
6
Medium Priority
?
927 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 7

Accepted Solution

by:
markmiddlemist earned 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Assisted Solution

by:markmiddlemist
markmiddlemist earned 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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.…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

719 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