简体   繁体   中英

ASP.Net GridView TemplateField Inserting HTML

I'm relatively new to ASP.Net so this may be a simple question. I'm using Visual Studio Express 2012 for Web and I have a GridView setup (which works fine) that I am trying to get HTML wrapped round one of the columns. My code at the moment:

            <asp:TemplateField HeaderText="" SortExpression="teamviewer">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("teamviewer") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <a href="javascript:teamviewerclientconnect('999999999')">
                        <img src="Images/teamviewer_icon.png" /><asp:Label ID="Label1" runat="server" Text='<%# Bind("teamviewer") %>'></asp:Label>
                    </a>
                </ItemTemplate>
            </asp:TemplateField>

At the moment this puts my image in as the hyperlink and then outputs the SQL resulting field as text next to it. What I need to do is replace where I have put 999999999 with the actual SQL result. I don't want it to print the text at all, it should just be in the hyperlink. Thanks in advance.

Use a code-behind method to build the href string, like this:

protected string BuildHref(string clientId)
{
    return "javascript:teamviewerclientconnect('" + clientId + "')";
}

<ItemTemplate>
    <a href='<%# BuildHref(Eval("DATABASE_COLUMN_NAME_HERE")) %>'>
        <img src="Images/teamviewer_icon.png" />
        <asp:Label ID="Label1" runat="server" 
                   Text='<%# Bind("teamviewer") %>'>
        </asp:Label>
    </a>
</ItemTemplate>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM