Search code examples
c#asp.netgridviewnavigateurl

Set custom NavigateUrl value from gridview c#


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>

Solution

  • <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>