Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在网格中找到控件列?_C#_Asp.net_Gridview - Fatal编程技术网

C# 如何在网格中找到控件列?

C# 如何在网格中找到控件列?,c#,asp.net,gridview,C#,Asp.net,Gridview,在我的asp网格中,很少有列是模板字段,也很少有列是从DataTable绑定的。 我将Autogenearte设置为true。这么多 列=静态列+数据表列 。我正在对行创建事件上的列重新排序 我的问题是,在静态列中,我需要根据某些情况禁用某些列 条件我需要识别静态模板字段中控件的列号。这样我就可以 e、 Row.Cells[7].CssClass=hiddencol; 但是我怎样才能得到这个专栏呢 我试过了 int colNo=0; for(int count=0;count<e.R

在我的asp网格中,很少有列是模板字段,也很少有列是从DataTable绑定的。 我将Autogenearte设置为true。这么多 列=静态列+数据表列 。我正在对行创建事件上的列重新排序

我的问题是,在静态列中,我需要根据某些情况禁用某些列 条件我需要识别静态模板字段中控件的列号。这样我就可以 e、 Row.Cells[7].CssClass=hiddencol; 但是我怎样才能得到这个专栏呢

我试过了

int colNo=0;   
for(int count=0;count<e.Row.Cells.count;count++)
{ 
   Button btn=(Button) e.Row.Cells[Count].FindControl("txtCol");
   if(btn!=null)
   colNo=count;
} 

但是我没有得到专栏号。所有专栏都很满意。

如果我理解你的意图,那么下面的内容怎么样。它对我的测试有效

网格

代码隐藏

    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = GetData();
        GridView1.DataBind();
    }



    private DataTable GetData()
    {
        // get some data and return it
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (HideCell(e.Row.RowIndex))
            {
                var cell = e.Row.Cells.Cast<TableCell>().FirstOrDefault(c => c.FindControl("btnTest") != null);
                if (cell != null)
                {
                    cell.CssClass = "hidden";
                }
            }
        }
    }

    private bool HideCell(int rowNum)
    {
        return rowNum%2 == 0;
    }

最好用javascript控制CSS。@在这种情况下,决定是否隐藏单元格的信息可能在服务器上。与加载页面后运行代码隐藏内容相比,按原样将标记发送到浏览器更有效。
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = GetData();
        GridView1.DataBind();
    }



    private DataTable GetData()
    {
        // get some data and return it
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (HideCell(e.Row.RowIndex))
            {
                var cell = e.Row.Cells.Cast<TableCell>().FirstOrDefault(c => c.FindControl("btnTest") != null);
                if (cell != null)
                {
                    cell.CssClass = "hidden";
                }
            }
        }
    }

    private bool HideCell(int rowNum)
    {
        return rowNum%2 == 0;
    }