简体   繁体   中英

Bind to a hyperlinkfield in a gridview

I have a gridview which pulls data from an xml file. One of the columns of the gridview is a hyperlinkfield. I want to bind a URL field contained in my XML file to this column. I think I have the right idea with the code below but cant figure out how to finish it. The URL is the datakey of the gridview by the way.

protected void grdContents_RowCreated(object sender, GridViewRowEventArgs e)
{
    ((HyperLinkField)grdContents.Columns[1]).NavigateUrl = 






}

you can bind hyperlink on gridView_RowDataBound event like this

    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType.Equals(DataControlRowType.DataRow))
            {
                HyperLinkField lnkHyper = (HyperLinkField)e.Row.FindControl("HyperLinkField1");
                lnkHyper.NavigateUrl="";
            }
        }

try this.

or you can also bind url using DataBinder.Eval at the time of binding source to grid like

 <Columns>
       <asp:TemplateField>                                           
        <ItemTemplate>
             <asp:HyperLink runat="server" 
NavigateUrl="<%# DataBinder.Eval(Container.DataItem, "url") %>"></asp:HyperLink>
         </ItemTemplate>
        </asp:TemplateField>
</Columns>

You can use this as well, as you also providing data source to Grid view.

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