【问题标题】:Exclude column in certain rows - SQL / Asp.Net排除某些行中的列 - SQL / Asp.Net
【发布时间】:2017-05-25 07:09:13
【问题描述】:

我想更改一些为我编写的代码。我想省略显示 tblResults.videoLink=''

的超链接

超链接在表格中显示为“视频链接”,因此人们可以点击它。我只希望单词/超链接“视频链接”出现在人们输入的行中。

代码是 Microsoft SQL,但写在 ASPX 文件中。后端是 VB,但 VB 几乎是空的。ASPX 文件处理所有内容...

    <h1>Recent Results</h1>
    <asp:Panel ID="pnlMyWishes" runat="server">

        <asp:SqlDataSource ID="DSWishes" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" SelectCommand="SELECT     TOP (100) PERCENT tblResults.wishID, tblResults.Player1AccountID, tblResults.Player2AccountID, tblResults.Player1Result, tblResults.Player2Result, tblResults.venue, tblResults.potSize, tblResults.player1Name, tblResults.player2Name, tblResults.videoLink, tblResults.date FROM tblResults ORDER BY tblResults.date DESC">
        </asp:SqlDataSource>
        <asp:GridView ID="gdvWishes" width="100%" runat="server" AllowPaging="True" AutoGenerateColumns="False" CssClass="mGrid" DataKeyNames="wishID" DataSourceID="DSWishes" PageSize="20" AllowSorting="True">
            <AlternatingRowStyle CssClass="alt" />
            <Columns>

                <asp:BoundField DataField="player1Name" HeaderText="Player 1" />      
                <asp:TemplateField HeaderText="Result">
                    <ItemTemplate>
                        <asp:Label ID="lblP1" Text='<%# Eval("player1Result").ToString %>' runat="server" Visible="true">
                        </asp:Label><asp:Label ID="lblVs" Text=" - " runat="server" Visible="true"></asp:Label>
                        <asp:Label ID="lblP2" Text='<%# Eval("player2Result").ToString %>' runat="server" Visible="true"></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Width="17%" />
                </asp:TemplateField>
                <asp:BoundField DataField="player2Name" HeaderText="Player 2" />

                <asp:BoundField DataField="potSize" HeaderText="Pot" />
                <asp:BoundField DataField="venue" HeaderText="Venue" />
                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:HyperLink ID="hypVideoLink" runat="server" NavigateUrl='<%# Eval("videoLink").ToString %>'>Video Link</asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </asp:Panel>

【问题讨论】:

    标签: sql asp.net vb.net


    【解决方案1】:

    像下面这样创建一个子,在用数据填充GridView 的代码之后调用它。

    Private Sub HideBlankURLs()
        For Each r As GridViewRow in gdvWishes.Rows
            If r.RowType = DataControlRowType.DataRow Then //Execute the code only for datarow, excluding footer and header
                Dim hypURL As HyperLink
                hypURL = r.Cells(5).FindControl("hypVideoLink") //Goes to the column of VideoURL, index 5 is counting from 0 to 5
                If hypURL.NavigateURL = "" Then //Checks if the URL is blank, if it is then hide the hyperlink control
                    hypURL.Visible = False
                End If
            End If
        Next r
    End Sub
    

    这将遍历所有行并在该列中查找控件,检查导航 URL 是否为空白,然后隐藏该控件。

    注意:将 (//) 替换为单引号 ('),因为 SO 将其读取为文本封闭字符而不是注释。

    【讨论】:

    猜你喜欢
    • 2018-02-24
    • 2021-12-24
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 2022-06-28
    • 1970-01-01
    相关资源
    最近更新 更多