如何强制刷新GridView?

时间:2012-12-31 19:28:46

标签: asp.net gridview checkbox

我想要的刷新类型本身并不处理数据。

以下是该方案: 我有一个有3列A,B和C的网格视图。 用户可以单击复选框a,b和c以使相应的列可见或不可见。 [要清楚,这些复选框在gridview外部。]

现在我希望立即刷新gridview,只显示每个复选框后的选定列。我该怎么做?

1 个答案:

答案 0 :(得分:1)

假设你有一个表(Grid)中每一列的复选框,你可以在jQuery中写这样的东西:

$(function() {
    $('input:checkbox').change(function() {
        //get a reference to the corresponding table column
        var $col = $('<#%=grid.ClientID %>' + ' td:nth-child(' + $(this).val() + ')');
        if (this.checked) $col.hide();
        else $col.show();
    });
});​

相应的复选框的值设置为表中的列索引。例如:

<input type="checkbox" value="1" /><-- hides first column in grid-->
<input type="checkbox" value="2" /><-- hides second column in grid-->
<input type="checkbox" value="3" /><-- hides third column in grid-->

可能对您有用的更短/更简单的版本是:

$(function() {
    $('input:checkbox').change(function() {
        var $col = $('table ' + 'td:nth-child(' + $(this).val() + ')');
        $col.toggle();
    });
});​

jsfiddle.

更新 - C#版本

protected void Check_Clicked(Object sender, EventArgs e) 
  {
     CheckBox theOneClicked= (sender as CheckBox);
     if(theOneClicked.ID=="checkbox1")
         myGrid.Columns[0].Visible=false;
     else if(theOneClicked.ID=="checkbox2")
         myGrid.Columns[1].Visible=false;
     else if(theOneClicked.ID=="checkbox3")
         myGrid.Columns[2].Visible=false;
  }
相关问题