0

I have a gridview in which I want to load a users details. In this gridview I want to make the users phone number a hyperlink. This is so they can click on the link and it automatically rings the number using the phone software stored on the their pc. This works fine if you use the below syntax in html:

   <a href = "tel:07123456789">07123456789</a>

My issue is that I want to do this in a gridview which populattes the phone number. The html has to have the 'tel:' bit in front of it first. I have tried everything please help! I want essentially above but to render in gridview with the loaded HomeNo where the phone number should be...HElp! Gridview:

   <asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="ds">
           <Columns>

               <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
               <asp:HyperLinkField DataTextField="HomeNo" HeaderText="HomeNo" NavigateUrl="tel:"  />
            </Columns>
       </asp:GridView>

2 Answers 2

3
<asp:GridView ID="GridView1" runat="server"  OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="ds">
   <Columns>
      <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
      <asp:TemplateField>
         <ItemTemplate>
           <asp:HyperLink ID="HyperLink1" runat="server" 
                NavigateUrl='<%# Eval("HomeNo", "tel:{0}") %>' 
                       Text='<%# Eval("NomeNo") %>'></asp:HyperLink>
         </ItemTemplate>
      </asp:TemplateField>
   </Columns>
</asp:GridView>
Sign up to request clarification or add additional context in comments.

1 Comment

Yey thanks very much! Thats the solution I was looking for. Turns out I wasnt far off!
2

you would need to do this inside of an also if you have data it's reading from you need to do some code inside of the DataBound Event of the DataGrid

for example I have names and email addresses as a link in my current datagrid here is how I do it

<asp:TemplateColumn HeaderText="Scheduler" HeaderStyle-Font-Bold="true" HeaderStyle-Width="145">
    <ItemTemplate>
        <a href='<%#Eval("Email_Address") %>' ><%# Eval("Scheduler") %></a>
    </ItemTemplate>
    <HeaderStyle Font-Bold="True" />
</asp:TemplateColumn>

protected void dgShippers_DataBinding(object sender, EventArgs e)
{
    foreach (DataRow r in dtShippers.Rows)
    {
        if (!System.Uri.IsWellFormedUriString(r.ItemArray[3].ToString(), UriKind.Absolute))
        {
            var tempHref = "<a href=mailto:" + r.ItemArray[4].ToString() + " />" + r.ItemArray[3].ToString()+ "</a>";
            r.Table.Rows[0]["Scheduler"] = tempHref;
        }
    }
}

4 Comments

This is what he would need if he were using a DataGrid, but he is using a Gridview. So it should be a TemplateField
That's correct.. @DaneEdw I did this using a DataGrid
if his original code I think he could just cast the field in reference as (HyperLink) for example ( (HyperLink)GridView1.Rows[e.RowIndex].Cells[0].Controls[0] ).Text
string.Format() vs fooObject.SomeProperty.ToString() which is better ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.