Solved

ASP.NET C#: Dynamic text in GridView HyperLinkField

Posted on 2012-03-13
6
907 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 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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