?
Solved

How to create a hyperlink in gridview in C# code behind?

Posted on 2014-02-28
6
Medium Priority
?
6,204 Views
Last Modified: 2014-03-01
I am a total newbie and I am trying to modify someone else's code.
The .aspx page has a gridview cotnrol on it. In the c# code behind, several columns for the gridview are defined, and rows are created ... all dynamically. That all works fine.

There is a column for "Email". I want the email to link to a page and pass an id that is unique to the email (which I know what it should be).
So the link would be, for example:
<a href="/mypage.aspx?id=123">test@test.com</a>

Open in new window


The column is defined in the code behind:
                dcField = new DataColumn();
                dcField.DataType = System.Type.GetType("System.String");
                dcField.ColumnName = "Email";
                dcField.ReadOnly = false;
                dcField.Unique = false;
                dtRequests.Columns.Add(dcField);

Open in new window


In the code behind, the column is filled ... I tried this but of course all I see in the grid view is the HTML, not a link:
string emailLink = "<a href=\"/mypage.aspx?id=" + formDataId.ToString() + "\">" + htFormData["Email"] + "</a>";
                drRequest["Email"] = emailLink;

Open in new window

               

I know this is a common question on the web but I am such a newbie that I don't understand what is being said. I need to know exactly what code I need to add/change in the code behind so that the link will be displayed. I'm not looking for examples or links to other solutions. I need the exact code.

Please let me know if you need any other info.
Thanks.
0
Comment
Question by:alicia1234
[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
  • 3
6 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 39896667
In your case the following would probably be the easiest solution.

There should be a line somewhere that binds the data to the grid, something like:
GridView1.DataBind();

Open in new window

Just before that, add this line (substituting the name of your grid for GridView1):
GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);

Open in new window

The handler that is used by the line above, is defined as follows:
        void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
            int intEmailCol = 1; // set to correct column, starting at 0
            if (e.Row.RowType == DataControlRowType.DataRow && // skip header/footer
                e.Row.Cells[intEmailCol].Text.StartsWith("&lt;a href")) { // don't just decode all text in all columns, in this case we know what we want to show: <a href...
                e.Row.Cells[intEmailCol].Text = Server.HtmlDecode(e.Row.Cells[intEmailCol].Text);
            }
        }

Open in new window

The first line in there holds a constant, in my small test datatable "Email" was the second column so I set the column index to 1. For you this will probably be a different value, depending on your other columns. If it's the last column you could use:
int intEmailCol = e.Row.Cells.Count - 1

Open in new window

Point is, this will only be done for the Email column. Because if you start doing this for all columns (as some examples on the web suggest) it could be a security risk, writing out a script tag in your html for example.
0
 

Author Comment

by:alicia1234
ID: 39897184
You are my hero! ;-) That worked perfectly. Just one little tweak I am wondering about. Is there a way for me to actually pass the number of the email column to the event handler?
And if I did want more than one column to be a link (which I don't but good to know), if I could pass the column number, then would I just "call" the event handler once for each column? And FYI ... this is only used by registered, paying, logged in, "naive" users ... and the ID is not a user ID or anything like that. If someone were to try messing with the ID number, they really couldn't do any harm. ;-)
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39897237
Nice!

Well no not really an argument to the handler but perhaps just make it a class variable (move the declaration of int intEmailCol outside the function), if you want a number of columns that class variable could be for example
List<int> mEmailCols = new List<int>();

Open in new window

and you could fill it with
mEmailCols.Add(dtRequests.Columns.IndexOf(dcField));

Open in new window

after your code where you add the fields to the dtRequests.Columns, but before the DataBind call again.

The event handler could then process all column indices in the List:
        void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
            foreach (int intEmailCol in mEmailCols) {
                if (e.Row.RowType == DataControlRowType.DataRow && // skip header/footer
                    e.Row.Cells[intEmailCol].Text.StartsWith("&lt;a href")) { // don't just decode all columns, in your case we know what we want to show: <a href...
                    e.Row.Cells[intEmailCol].Text = Server.HtmlDecode(e.Row.Cells[intEmailCol].Text);
                }
            }
        }

Open in new window

If you do it for multiple columns, remember that the check for "<a href" is there, if you need other content to be shown as well, take out that check.

Then, at some point, if you want to take it to the next level (because this is a bit of a 'quick & dirty hack'), look into defining TemplateFields on your DataGrid: http://msdn.microsoft.com/en-us/library/aa479353.aspx (I haven't tried it for this type of data yet but it should be easy to do, if you want I'll have a look later on).
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39897248
Well. why wait... I had a look and you could define your grid as follows:
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" />
                <asp:BoundField DataField="Email" HeaderText="Email Address" HtmlEncode="false" />
            </Columns>
        </asp:GridView>

Open in new window

The actual columns would differ for you but the important part is AutoGenerateColumns="false" and HtmlEncode="false" because from what I read, auto generate is what 'switches on' the encoding by default so this is the way to prevent that.

After making sure you have a backup of your project I would say lose the event handler and this is the way to go even though it's a bit more work.
0
 

Author Comment

by:alicia1234
ID: 39897251
Great. Thank you SO much! ;-)
0
 

Author Closing Comment

by:alicia1234
ID: 39897252
Exactly what I needed to know!
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

743 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