将navigateURL绑定到datagrid

时间:2013-01-10 19:23:18

标签: asp.net data-binding datagrid hyperlink webforms

我创建了一个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>

这不行,有人可以告诉我做错了吗?

我的table NameExternal_Hyperlink 中有 2列,每行(位于{{} {1}}列)包含external_hyerplink的扩展名,因此根据点击的行,我会获得 urlwww.google.com/extension1 < / p>

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

1 个答案:

答案 0 :(得分: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>