Help: Working with hyperlinks in C#

B

Bill

Say I want to pull a bunch of records from a contacts database into a
gridview and one of the fields contains an email address. Now suppose
I want to make that email address clickable by putting it in a string
(strEmail) and wrapping it in a hyperlink like this ASP Classic
example...
"<a href='mailto:" & strEmail & "'>" & strEmail & "</a>"

How do I pull that off in a gridview? Step by step or tutorial link
would be great if available.

I am using VS 2005, ASP.Net 2.0 with C# code behind.

Thanks for your help in advance,

Bill
 
J

John

you could also use a template field and put your <a href...> in that.
currently thats what I am doing and passing params to the page in the link
and works well.
 
G

Guest

Sure, example that should show you how to accomplish this task:

-- begin code --

<asp:GridView runat="server" ID="gridView" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>No.</HeaderTemplate>
<ItemTemplate>
<%# Container.DataItemIndex.ToString() %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Name" DataField="Name"/>
<asp:BoundField HeaderText="Email" DataField="Email"
DataFormatString="<a href='mailto:{0}'>{0}</a>"/>
</Columns>
</asp:GridView>

<script runat="server">

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gridView.DataSource = GetExampleData(10);
gridView.DataBind();
}
}

/// <summary>
///
/// </summary>
/// <returns></returns>
private DataTable GetExampleData(int rowCount)
{
DataTable table = new DataTable();
DataRow row = null;

table.Columns.Add("Id", typeof(Guid));
table.Columns.Add("Name", typeof(String));
table.Columns.Add("Email", typeof(String));

string str;

for (int i = 0; i < rowCount; i++)
{
str = i.ToString();

row = table.NewRow();
row[0] = Guid.NewGuid();
row[1] = "Name" + str;
row[2] = String.Format("emailaddress{0}@hotmail.com", str);

table.Rows.Add(row);
}

return table;
}


</script>
-- end code --

hope this helps

Milosz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top