本文介绍了使用按钮单击查找子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.
推荐答案
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;
}
这篇关于使用按钮单击查找子GridView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!