Thursday, March 29, 2012

MAILTO: Datagrid Column

Hi I am using the following code to fill the datagrid in my ASP.NET app with VB.NET code behind. It fills the grid with name and email address. What I want to do is to show email addresses as a hyperlink to email address so upon clicking it default mail client will open up and a new mail is created which is possible to do with mailto:emailaddress

I hope I am making this clear enough to understand.

Private Sub FILL_GRID(ByVal SQL_String As String, ByVal GName As DataGrid)
Dim objConnection As OleDbConnection
objConnection = New OleDbConnection(strMain)
objConnection.Open()

Dim objCommand As OleDbCommand
objCommand = New OleDbCommand(SQL_String, objConnection)

Dim objDataReader As OleDbDataReader
objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)

GName.DataSource = objDataReader
GName.DataBind()

objDataReader.Close()
objDataReader = Nothing

objCommand = Nothing

objConnection.Close()
objConnection = Nothing
Cheers.Hello,

you need to achieve this with the ItemDataBound event from the datagrid.

What you can do then is the following (untested). You have to count the columns with an index (starting on 0):

Private Sub dgdSearch_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgdSearch.ItemDataBound

If e.Item.ItemType = ListItemType.Item Then
If e.Item.Cells(2).Text <> "" Then
e.Item.Cells(2).Text = "<a href= mailto:' " & e.Item.Cells(2).Text & "'>" & e.Item.Cells & "</a>
End If
Thanks for the code but I have tried that and it doesn't work and I get alternate email addresses and each alternate one is missing with no hyperlinks in remaining ones.

Cheers.
bind "DataNavigateUrlField" of your hyperlink column to your e-mail address field aswell as the "DataTextField". Of course, this won't work if you are AutoGenerating your columns .
Originally posted by MasterBlaster
bind "DataNavigateUrlField" of your hyperlink column to your e-mail address field aswell as the "DataTextField". Of course, this won't work if you are AutoGenerating your columns . Yes I am autogenerating the columns since I am new to .NET and still learning.

If you can pass me the code on how to manually add columns and then attach them to the data I have.

My recordset will return 2 fields. Name, EMail.

Cheers.
Ok, this is pretty easy once you learn about template columns and databinding expressions.

Here is my load event code to bind the grid to a simple datatable that is created in code.

Dim dt As New DataTable("MyTable")
Dim dr As DataRow

dt.Columns.Add("Name")
dt.Columns.Add("Email")

dr = dt.NewRow()
dr("Name") = "Tom Somebody"
dr("Email") = "tom@.sombody.com"

dt.Rows.Add(dr)

DataGrid1.DataSource = dt
DataGrid1.DataBind()

Now, this is the HTML of the datagrid control:

<asp:DataGrid id="DataGrid1"
runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="Name" HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Email">
<ItemTemplate>
<asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "Email", "MAILTO:{0}") %>' Text='<%# DataBinder.Eval(Container.DataItem, "Email") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

Ensure you turn off AutoGenerateColumns by setting it to false as shown above. Good luck.
Thanks. I got it now.

Unfortunately I actually changed the EMail in Database itself in the MAILTO format and now it all nice looking hyperlinks in Grid even with AutoGenerateColumn = True.

<a href="http://links.10026.com/?link=mailto:test@.test.com">test@.test.com</a>

Cheers.
That was probably the worst way you could go about it. You shouldn't change your database because of the way you want to display something. I would suggest you change your DB back to holding just the email address. Here is why.

Lets say you want to send an email to the user from your asp.net page via code, now, you have to strip out the MAILTO: everytime because you wanted it to be easier earlier. Now everytime you want to use that email address column, you will have to do a data conversion to it to have a usable column.

A MCSD should know this...

Have you looked at my ealier post? It does EXACTLY what you want to do.
Yes I know that it's the worst way yo do so but I needed urgently just for the weekend and so rather than wasting my time on that for 2 days of purpose I thought it would be a good idea to do it now and then at later stage correct it.

Cheers.

0 comments:

Post a Comment