如何在按钮上更改FooterRow和Columns的可见性 - C#

时间:2015-06-03 17:06:33

标签: c# asp.net gridview visibility

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

页面加载:

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。我一直在玩Footer Row的可见性设置为false,将列可见性设置为true等等,但似乎没有任何东西能给我我想要的结果。

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

1 个答案:

答案 0 :(得分:0)

问题

由于您的#!/usr/bin/perl use strict; use warnings; my %dictionary; open my $dic_fh, '<', 'dictionary.txt' or die "Can't open file: $!"; while (my $ref = <$dic_fh>) { chomp $ref; my @columns = split(/\t/, $ref); my $res_name = $columns[0]; my $ref_nuc = $columns[1]; $dictionary{$res_name} = {$ref_nuc}; open my $in_fh, '<', 'input.txt' or die "Can't open file: $!"; while (my $line = <$in_fh>) { chomp $line; my @columns = split(/\t/, $line); my @name = $columns[3]; if (my $name eq $res_name) { my $line = $_; foreach my $res_name (keys %dictionary) { $line =~ s/$name/$dictionary{$ref_nuc}/; } print $line; } } } 方法中没有if (!this.IsPostBack)阻止,因此Page_Load方法中的所有代码都会在执行Page_Load方法之前执行点击Add_Button_Click

第二次点击,这是Add_Button中发生的事情:

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