【问题标题】:Hidden edit button in GridView using C#使用 C# 在 GridView 中隐藏编辑按钮
【发布时间】:2021-06-17 03:32:59
【问题描述】:

我已经在我的数据集中输入了这个条件

  1. 当 message 的值等于"1" 时,无法继续在此网页上工作...
  2. 当 message 的值等于 "0" 时,可以继续,但无法编辑 gridview 数据...

下面是我的代码

if (message == 1)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('test msg 1');window.location='Default.aspx';", true);
    return null;
}
else if (message == 0)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('test msg 2');", true);
    btrila.Visible = false;
    btnreset.Visible = true;
    return dsProducts;
}
else
{
    return dsProducts;
}

当消息的值等于"0" 时,我需要隐藏或禁用edit 下方的按钮gridview 数据

<ItemTemplate>
    <asp:ImageButton ID="btnedit" runat="server"
        CommandName="Edit"
        ImageUrl="/aspnet/img/edit_icon.gif"
        ToolTip="Edit" />
</ItemTemplate>

我在RowDataBound 中尝试过这个解决方案但没有成功,因为"btnedit" 总是被隐藏...

任何帮助将不胜感激...谢谢。

protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.DataItem != null)
        {
            if (btnreset.Visible == true)
            {
                gvProducts.Columns[2].Visible = false;
            }
            else
            {
                gvProducts.Columns[2].Visible = true;
            }
        }
    }
}

【问题讨论】:

    标签: c# asp.net gridview edit


    【解决方案1】:

    当 message 的值等于“0”时,我需要隐藏或禁用 下面的按钮用于编辑 gridview 数据

    在 DataBound 函数上,您需要循环单元格并找到您的控件 - 方法如下

    protected void onRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // loop all data rows
            foreach (DataControlFieldCell cell in e.Row.Cells)
            {
                // check all cells in one row
                foreach (Control control in cell.Controls)
                {
                    // go to find this button
                    Button button = control as Button;
                    if (button != null && button.CommandName == "Edit")                        
                          button.Enable = false; // or true depend on your contitions.
                }
            }
        }
    }
    

    【讨论】:

    • 感谢您的帮助。我已经尝试过你的建议,“btnedit”总是可见的......
    • @EdwardSheriffCurtis 检查你的代码,这就是方法。您也可以尝试在按钮上添加一些 CSS。
    猜你喜欢
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 2021-07-25
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    相关资源
    最近更新 更多