【问题标题】:Edit visibility of a cell in GridView based on a value in the row根据行中的值编辑 GridView 中单元格的可见性
【发布时间】:2013-11-11 02:45:01
【问题描述】:

当行中最后一个单元格的值等于用户 ID 的会话值时,我正在尝试将我的删除按钮可见性设置为 true

Girdview 更新:

<Columns>
<asp:TemplateField>
    <ItemTemplate>
        <%# Container.DataItemIndex + 1 %>
    </ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="attachID" HeaderText="Attachment ID" SortExpression="atID" HeaderStyle-CssClass="hidcol" ItemStyle-CssClass="hidcol"/>
<asp:BoundField DataField="attachmentTitle" HeaderText="Attachment Title" SortExpression="attachmentTitle" />
<asp:BoundField DataField="attachmentType" HeaderText="Attachment Type" SortExpression="attachmentType" />
<asp:BoundField DataField="attachmentDescription" HeaderText="Attachment Description" SortExpression="attachmentDescription" />
<asp:BoundField DataField="attachmentDate" HeaderText="Attachment Date" SortExpression="attachmentDate" />
<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="download"  CommandArgument='<%# Eval("attachmentFile") %>'><img src="CSS/images/download.png" alt="Download File" /></asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
    <ItemTemplate>
       <asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False" CommandArgument='<%# Eval("userID") %>' CommandName="Delete" ImageUrl="~/CSS/images/delete_page.png" Text="Delete" />
    </ItemTemplate>
</asp:TemplateField>
</Columns>

如果userIDsession["username"] 匹配,我正在尝试设置删除按钮属性可见

我不确定这是否正确。

更新后的代码:

String searchValue = "" + Session["username"];

    foreach (GridViewRow row in GridView1.Rows)
    {
        // Only look in data rows
        if (row.RowType == DataControlRowType.DataRow ||
            row.RowType == DataControlRowType.EmptyDataRow)
        {
            // Find the delete button by ID
            ImageButton theDeleteButton = row.FindControl("ImageButton1") as ImageButton;

            // Verify the button was found before we try to use it
            if (theDeleteButton != null)
            {
                if (theDeleteButton.CommandArgument != searchValue)
                {
                    // Make the button invisible
                    theDeleteButton.Visible = false;
                }
            }

        }
    }

我怎样才能做到这一点?

提前致谢!

代码已更新

【问题讨论】:

    标签: c# asp.net gridview code-behind


    【解决方案1】:

    我建议对带有CommandName 属性的删除按钮使用TemplateField(类似于您对下载链接按钮所做的操作)而不是CommandField,因为它会使您的代码隐藏删除按钮清洁器,像这样:

    标记:

    <asp:TemplateField>
        <ItemTemplate>
            <asp:Button ID="ButtonDelete" runat="server" CommandName="delete"  
                        CommandArgument='<%# Eval("id") %>' Text="Delete" />
        </ItemTemplate>
    </asp:TemplateField>
    

    代码隐藏:

    foreach (GridViewRow row in GridView1.Rows)
    {
        // Only look in data rows
        if (row.RowType == DataControlRowType.DataRow || 
            row.RowType == DataControlRowType.EmptyDataRow)
        {
            // Find the delete button by ID
            Button theDeleteButton = row.FindControl("ButtonDelete") as Button;
    
            // Verify the button was found before we try to use it
            if(theDeleteButton != null)
            {
                // Make the button invisible
                theDeleteButton.Visible = false;
            }
        }
    }
    

    注意:如果尝试的转换无法成功完成,as 运算符将返回 null;因此检查 nulltheDeleteButton 变量。

    【讨论】:

    • 我应该如何检查userID 值与Session["username"] 的对比?因为我想在值不匹配时隐藏删除按钮!
    • @pouria - 同样的事情,将userID 设为TemplateField 而不是BoundField,这样您就可以摆脱容易出错的row.Cells[x] 语法。 FindControl 并不完美,但它不依赖于网格中列的顺序。
    • 谢谢!正如您在示例中所示,我使用了CommandArgument='&lt;%# Eval("id") %&gt;'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    相关资源
    最近更新 更多