0

我已经创建了一个datagrid并将其链接到我的数据库中的一个表,然后我想添加一个hyperlink应该绑定到column来自table

 <asp:DataGrid ID="DataGrid1" runat="server" DataSourceID="SqlDataSource1">
        </asp:DataGrid>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:holidaysConnectionString %>" 
            SelectCommand="SELECT [Name], [External_Link] FROM [Person]">
        </asp:SqlDataSource>
        <asp:HyperLink ID="hyperlink" runat="server" NavigateUrl='http://www.google.com/<%# Bind("External_Link")%>' Target="_blank">Visit Google</asp:HyperLink>

这不起作用有人可以告诉我我做错了什么吗?

我的 , 中有2并且,每一行列内)都包含对 a 的扩展名,这取决于单击了哪一行我会得到,或者tableNameExternal_Hyperlinkexternal_hyerplinkurlwww.google.com/extension1www.google.com/extension2 etc.

但不要认为我的方向是正确的。请给我一些想法来解决我的问题。

4

1 回答 1

2

请尝试以下示例:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">           
            <Columns>
                <asp:BoundField DataField="ProblemID" />
                <asp:HyperLinkField DataNavigateUrlFields="ProblemID" DataNavigateUrlFormatString="SmallWindow.aspx?id={0}"
                    DataTextField="Click here" NavigateUrl="SmallWindow.aspx" />
                <asp:BoundField DataField="Solution" />
            </Columns>
        </asp:GridView>

或者

//This event should fire on Row Data Bound

protected void yourGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
        HyperLink hlControl = new HyperLink();
        hlControl.Text = e.Row.Cells[2].Text; //Take back the text (let say you want it in cell of index 2)
        hlControl.NavigateUrl = "http://www.stackoverflow.com";
        e.Row.Cells[2].Controls.Add(hlControl);//index 2 for the example

编辑

尝试这样的事情:

<asp:HyperLink ID="HyperLink2" runat=server NavigateUrl='<%#Eval("Company_ID", "CompanyProfile.aspx?ID={0}")%>'><%#Eval("Name")%></asp:HyperLink>
于 2013-01-10T19:35:54.207 回答