0

Here is my Data Grid:

<asp:DataGrid id="dataGrid1" runat="server">
    <Columns>
        <asp:HyperLinkColumn DataNavigateUrlField="Valid" 
         DataTextField="Valid" 
          HeaderText="Enable / Disable"
         DataNavigateUrlFormatString="~\examfilemanager\{0}" />
      </Columns>
</asp:DataGrid>

I would like to have custom DataTextField, display Enable is the value is true, and Disable if the value is false, how can I add this logic in the HyperLinkColumn? Thanks.

4

2 回答 2

1

请更改 ItemDataBound 事件中的文本

protected void dataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    if (e.Item.Cells[0].Controls.Count > 0)
    {
        HyperLink hypLnk = (HyperLink)e.Item.Cells[0].Controls[0];
        if (hypLnk != null)
        {
            if (hypLnk.Text == "False")
                hypLnk.Text = "Enable";
            else
                hypLnk.Text = "Disable";
        }
    }
}

数据网格 html

<asp:DataGrid id="dataGrid1" runat="server" OnItemDataBound="dataGrid1_ItemDataBound">
    <Columns>
        <asp:HyperLinkColumn DataNavigateUrlField="Valid" 
         DataTextField="Valid" 
          HeaderText="Enable / Disable"
         DataNavigateUrlFormatString="~\examfilemanager\{0}" />
      </Columns>
</asp:DataGrid>
于 2013-04-29T05:22:02.117 回答
0
<asp:HyperLink ID="hl1" NavigateUrl='<%# String.Format("uri.aspx?id={0}",Eval("id")) %>'
runat="server">Edit</asp:HyperLink>

Used this Below if you prefer HyperLink Column

    <Columns>
        <asp:HyperLinkColumn
             HeaderText="Enable /Disable"
             DataNavigateUrlField="IntegerValue"
             DataNavigateUrlFormatString="examfilemanager.aspx?id={0}"
             DataTextField="Id"
             DataTextFormatString="{0:c}"
             Target="_blank"/>
     </Columns>
于 2013-04-29T05:05:13.300 回答