0

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

格维更新:

<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>

我正在尝试设置删除按钮属性可见,如果userIDmachatessession["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;
                }
            }

        }
    }

我怎样才能让它发生?

提前致谢!

代码已更新

4

1 回答 1

1

我建议将 aTemplateField用于具有CommandName属性的删除按钮(类似于您对下载链接按钮所做的操作)而不是 a 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

于 2013-11-10T17:23:54.710 回答