如何在按钮上改变FooterRow和Columns的可见性,点击c#

本文关键字:Columns 可见性 点击 FooterRow 按钮 改变 | 更新日期: 2023-09-27 17:53:12

我有一个大约有16列的GridView,出于美观的原因,我只希望显示某些列,但是如果用户想要插入一行(使用页脚行),我需要使其余列可见。

On Page Load:

protected void Page_Load(object sender, EventArgs e)
{
    if (Add_Button.Text != "Cancel")
    {
        GridView1.FooterRow.Visible = false;
        GridView1.Columns[10].Visible = false;
        GridView1.Columns[11].Visible = false;
        GridView1.Columns[12].Visible = false;
        GridView1.Columns[13].Visible = false;
        GridView1.Columns[14].Visible = false;
        GridView1.Columns[15].Visible = false;
        GridView1.Columns[16].Visible = false;
    }
    else
    {
        GridView1.FooterRow.Visible = true;
    }
}

点击按钮:

protected void Add_Button_Click(object sender, EventArgs e)
{
    if (GridView1.FooterRow.Visible == false)
    {
        GridView1.Columns[8].Visible = true;
        GridView1.Columns[9].Visible = true;
        GridView1.Columns[10].Visible = true;
        GridView1.Columns[11].Visible = true;
        GridView1.Columns[12].Visible = true;
        GridView1.Columns[13].Visible = true;
        GridView1.Columns[14].Visible = true;
        GridView1.FooterRow.Visible = true;
        Add_Button.Text = "Cancel";
        Panel2.Visible = false;
    }
    else
    {                    
        Add_Button.Text = "Add New Record"; 
        GridView1.FooterRow.Visible = false;
    }
}

客户端页脚行可见性设置为true,额外列可见性设置为false。我一直在玩设置页脚行可见性为假,设置列可见性为真等等,但似乎没有给我我想要的结果。

目前,因为它是在第一个按钮上单击额外的行和页脚行是可见的,在第二次单击,这应该使它们再次不可见,它只使页脚行不可见,而不是列。有什么解决办法吗?

如何在按钮上改变FooterRow和Columns的可见性,点击c#

问题

由于您的Page_Load方法中没有if (!this.IsPostBack)块,那么当您单击Add_Button时,Page_Load方法中的所有代码将在Add_Button_Click方法执行之前执行。

第二次点击时,Page_Load的情况如下:

protected void Page_Load(object sender, EventArgs e)
{
    if (Add_Button.Text != "Cancel")
    {
        GridView1.FooterRow.Visible = false;
        GridView1.Columns[10].Visible = false;
        GridView1.Columns[11].Visible = false;
        GridView1.Columns[12].Visible = false;
        GridView1.Columns[13].Visible = false;
        GridView1.Columns[14].Visible = false;
        GridView1.Columns[15].Visible = false;
        GridView1.Columns[16].Visible = false;
    }
    else
    {
        // this block is executed because Add_Button.Text is "Cancel",
        // which was the result of the first click
        GridView1.FooterRow.Visible = true;
    }
}

下面是Add_Button_Click:

protected void Add_Button_Click(object sender, EventArgs e)
{
    if (GridView1.FooterRow.Visible == false)
    {
        GridView1.Columns[8].Visible = true;
        GridView1.Columns[9].Visible = true;
        GridView1.Columns[10].Visible = true;
        GridView1.Columns[11].Visible = true;
        GridView1.Columns[12].Visible = true;
        GridView1.Columns[13].Visible = true;
        GridView1.Columns[14].Visible = true;
        GridView1.FooterRow.Visible = true;
        Add_Button.Text = "Cancel";
        Panel2.Visible = false;
    }
    else
    {
        // this block is executed because GridView1.FooterRow.Visible is true,
        // which was the result of Page_Load execution on the second click
        Add_Button.Text = "Add New Record"; 
        GridView1.FooterRow.Visible = false;
    }
}

这就是为什么列在第二次点击时不可见的原因。

的解决方案

我假设你目前在Page_Load方法应该在第一次加载页面时执行,所以你需要添加if (!this.IsPostBack)块,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        if (Add_Button.Text != "Cancel")
        {
            GridView1.FooterRow.Visible = false;
            GridView1.Columns[10].Visible = false;
            GridView1.Columns[11].Visible = false;
            GridView1.Columns[12].Visible = false;
            GridView1.Columns[13].Visible = false;
            GridView1.Columns[14].Visible = false;
            GridView1.Columns[15].Visible = false;
            GridView1.Columns[16].Visible = false;
        }
        else
        {
            GridView1.FooterRow.Visible = true;
        }
    }
}

和更改Add_Button_Click方法如下:

protected void Add_Button_Click(object sender, EventArgs e)
{
    if (GridView1.FooterRow.Visible == false)
    {
        GridView1.Columns[8].Visible = true;
        GridView1.Columns[9].Visible = true;
        GridView1.Columns[10].Visible = true;
        GridView1.Columns[11].Visible = true;
        GridView1.Columns[12].Visible = true;
        GridView1.Columns[13].Visible = true;
        GridView1.Columns[14].Visible = true;
        GridView1.FooterRow.Visible = true;
        Add_Button.Text = "Cancel";
        Panel2.Visible = false;
    }
    else
    {
        GridView1.Columns[8].Visible = false;
        GridView1.Columns[9].Visible = false;
        GridView1.Columns[10].Visible = false;
        GridView1.Columns[11].Visible = false;
        GridView1.Columns[12].Visible = false;
        GridView1.Columns[13].Visible = false;
        GridView1.Columns[14].Visible = false;
        GridView1.FooterRow.Visible = false;
        Add_Button.Text = "Add New Record"; 
    }
}