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

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.
alicia1234Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robert SchuttSoftware EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alicia1234Author Commented:
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
Robert SchuttSoftware EngineerCommented:
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
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Robert SchuttSoftware EngineerCommented:
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
alicia1234Author Commented:
Great. Thank you SO much! ;-)
0
alicia1234Author Commented:
Exactly what I needed to know!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.