1

我有一个带有以下数据的 ASP.NET GridView:

在此处输入图像描述

行将根据 column3 上的值禁用 OnRowDataBound。

网格视图 :

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
        onrowdatabound="GridView1_RowDataBound1">
        <Columns>
            <asp:TemplateField HeaderText="Column1">
                <ItemTemplate>
                    <asp:HyperLink ID="hyperlink" runat="server" Text='<% #Eval("Dosage") %>' NavigateUrl='<% #Eval("Dosage") %>'></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Column2">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<% #Eval("Drug") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Column3">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<% #Eval("Patient") %>' ></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Column4">
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<% #Eval("Date") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

行数据绑定:

protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label a = e.Row.FindControl("Label3") as Label;
        if (a.Text == "Sam")
        {
            e.Row.Enabled = false;
            e.Row.Cells[0].Enabled = true;
        }
    }
}

但是,我希望 column1 始终启用,column1 中的超链接应始终可点击。

我试过获取单元格并启用它,但它不起作用。

请告知上述问题的解决方法是什么。

4

1 回答 1

2

您可以通过启用/禁用特定单元格来做到这一点。

  protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label a = e.Row.FindControl("Label3") as Label;
        if (a.Text == "Sam")
        {

            e.Row.Cells[0].Enabled = true;
            e.Row.Cells[1].Enabled = false;
            e.Row.Cells[2].Enabled = false;
            e.Row.Cells[3].Enabled = false;

        }
    }
}
于 2013-03-26T18:34:56.000 回答