使用按钮单击查找子GridView控件 [英] Find child gridview control using button click

查看:68
本文介绍了使用按钮单击查找子GridView控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,


我有两个网格,一个是父级,第二个是子级.现在,我想在单击按钮时尝试访问子gridview,但尝试显示为空.这是我尝试过的


Hello friends,


i have two grid one is parent and second is child. now i want to access child gridview when button click i try something but it display null. here what i tried


protected void BtnSet_Click(object sender, EventArgs e)
    {
        GridView GrdRights = (GridView)this.GrdParent.FindControl("GrdRightDetail");
    }



它显示为null ..谁能告诉我如何在单击按钮时获取子网格视图.



It display null ..can anyone tell me how to get child grid view on button click.

推荐答案

以下是您可以使用的一些代码,在此代码中,我比较了按控件类型进行控制,但如果要更改,可以按控件名称进行比较,这样可以有效地发挥作用.
Here is some code you can use, in this code I compare the control by the control type but if you want you can change is to compare by the control name and it works effectively.
private void button1_Click(object sender, EventArgs e)
       {
           object childgrid;

           childgrid =  getControlByType(this, typeof(DataGridView).ToString());
           childgrid =  getControlByName(this, dataGridView1.Name);
       }

       private object getControlByType(Control Main, string controlType)
       {
           if (Main.HasChildren)
           {
               foreach (var control in Main.Controls)
               {
                   if (control.GetType().ToString() == controlType) return control;
               }
           }

           return null;
       }

       private object getControlByName(Control Main, string controlName)
       {
           if (Main.HasChildren)
           {
               foreach (var control in Main.Controls)
               {
                   var ctl = (Control) control;
                   object ctl2 = null;

                   if (ctl.GetType().ToString() == typeof (DataGridView).ToString())
                   {
                       ctl2 = ctl;
                       if (ctl.Name == controlName)
                       {return ctl2;}
                       ctl2 = null;
                   }

                   if (ctl.GetType().ToString() == typeof(Button).ToString())
                   {
                       ctl2 = ctl;
                       if (ctl.Name == controlName)
                       { return ctl2; }
                       ctl2 = null;
                   }

                   if (ctl.HasChildren)
                   {
                     ctl2 =  getControlByName(ctl, controlName);
                   }

                   if (ctl2 != null)
                       return ctl2;

               }
           }

           return null;
       }


使用这个

int SomeRowIndex = 0;
GridView GrdRights = this.GrdParent.Rows(SomeRowIndex).FindControl("GrdRightDetail")as GridView;

就是了
Use this one

int SomeRowIndex=0;
GridView GrdRights = this.GrdParent.Rows(SomeRowIndex).FindControl("GrdRightDetail") as GridView;

Thats it


这篇关于使用按钮单击查找子GridView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆