根据列值显示行 asp repeater / gridview

Display rows based on column values asp repeater / gridview

我正在使用 VB.NET

我得到一个数据table,其中包含以下列

ID   DESC  Hyperlink COUNT 

计数为 01

如果计数是 0 我需要做一个

NavigateUrl='<%#"~/Create.aspx?ID=" + Eval("ID")%>' Text="Create"

在超链接栏

我没有对数据绑定事件进行任何显式操作。 我填充中继器的方式是:

rptrTask.DataSource = PpltDefGrid(Trim(v))

此时函数刚刚获得 sql table

rptrTask.DataBind()

有人可以给我有关如何执行此操作的内联代码吗?如果不知道如何通过后面的代码来做到这一点(请举个例子)?我尝试了一些 / Container.data 方法.. 但没有成功。非常感谢任何帮助。

您可以在代码隐藏中使用gridview的RowDatBound()事件。

 protected void rptrTask_RowDataBound(object sender, GridViewRowEventArgs e) {
        if (e.Row.RowType == DataControlRowType.DataRow) {
            HyperLink hl = (HyperLink)e.Row.FindControl("hyperlinkID");
            if(DataBinder.Eval(e.Row.DataItem, "COUNT") == "0")
            {
            hl.NavigateUrl = "~/Create.aspx?ID=" + DataBinder.Eval(e.Row.DataItem, "ID");
            }
        }
    }

我将逻辑提取到变量(bShowURl),以增强可维护性。还进行了额外检查以确保超链接存在。

Protected Sub yourGridview_RowDataBound(sender As Object, e As GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim currentDataItem As yourDataItemDataType = e.Row.DataItem
            Dim bShowUrl = IIF(currentDataItem.ID = 0, True, False)

            If bShowUrl Then
                Dim hyperlink As HyperLink = CType(e.Row.FindControl("yourHyperlinkControlId"), HyperLink) 
                If hyperlink IsNot Nothing Then
                    hyperlink.NavigateUrl = "~/Create.aspx?ID=" + currentDataItem.ID.ToString()
                End If
            End If
        End If

End Sub

我正在编辑我的答案,那么在这种情况下 RowDataBound 对您来说是一个简单的选择。

像这样修改你的 GridView:-

 <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">

我已经添加了"OnRowDataBound="GridView1_RowDataBound"

在您的 .aspx 页面中,您可以这样写 hyperlink

<asp:TemplateField HeaderText="Count">
    <ItemTemplate>
      <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="#" Text='<%#Eval("ID") %>'></asp:HyperLink>
   </ItemTemplate>
</asp:TemplateField>

在你的.aspx.vb页面中这样写:-

Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim HyperLink1 As HyperLink = DirectCast(e.Row.FindControl("HyperLink1"), HyperLink)
            If DataBinder.Eval(e.Row.DataItem, "COUNT") = "0" Then
                HyperLink1.NavigateUrl = "~/Create.aspx?ID=" + DataBinder.Eval(e.Row.DataItem, "ID")
            End If
        End If
    End Sub

希望对您有所帮助。