C# 从codebehind获取多用户控件中的GridView

C# 从codebehind获取多用户控件中的GridView,c#,asp.net,html,code-behind,C#,Asp.net,Html,Code Behind,i接口UC用户控件: <div id="dvChannel" runat="server" style="height: 205px; width: 550px; overflow: auto; margin-left: 5px;"> <asp:GridView ID="gvChannelUC"> </div> Html显示 <div id="dvChannel"> <div id="device_192.1

i接口UC用户控件:

<div id="dvChannel" runat="server" style="height: 205px; width: 550px; overflow: auto;
        margin-left: 5px;">
        <asp:GridView ID="gvChannelUC">
</div>
Html显示

<div id="dvChannel">
<div id="device_192.168.2.19_3331_0_pnlChannelUC">
  <div id="device_192.168.2.19_3331_0_dvChannel">
    <table id="device_192.168.2.19_3331_0_gvChannelUC">
    </table>
  </div>
</div>
<div id="dvChannel">
<div id="device_192.168.2.19_3331_1_pnlChannelUC">
  <div id="device_192.168.2.19_3331_1_dvChannel">
    <table id="device_192.168.2.19_3331_1_gvChannelUC">
    </table>
  </div>
</div>

问题
如何从多用户控件获取gridview?

虽然可以递归使用FindControl来查找gridview,但更好的方法是让用户控件
I接口UC
决定如何将数据绑定到其中的控件

您可以向UserControl添加公共方法
ShowData
,并将要显示的数据传递给它。然后可以将其分配给
gvChannelUC

int indexInterface=0;
foreach (DataRow row in dtDevicesListByRole.Rows)
{
    var ctrIpInterfaceUC = (Test2.SetupGroup.Ipservice.IpInterfaceUC)LoadControl("IpInterfaceUC.ascx");
    ctrIpInterfaceUC.ShowData(myRows);
    ctrIpInterfaceUC.ID = "device_"+ip+"_"+port+"$"+indexInterface;
    phDevices.Controls.Add(ctrIpInterfaceUC);//PlaceHolder for add many UserControl
}

通过UserControl上的公共属性公开gridview:

public GridView Grid
{
  get { return gvChannelUC; }
}
然后

List Grids=new List();//
...
ctrIpInterfaceUC=(Test2.SetupGroup.Ipservice.IpInterfaceUC)加载控制(“IpInterfaceUC.ascx”);
string Id=“设备”+ip+“\u”+端口+“$”+索引接口;
GridView ctrGridView=CtriInterfaceUC.Grid;
添加(Id,ctrGridView.ClientID);
控制ctr=(控制)CTRIInterfaceUC;
ctr.ID=ID
phDevices.Controls.Add(ctr)//添加多个用户控件的占位符
...

我试过:GridView gvChannelUC=(GridView)FindControl(“device_192.168.2.19_3331_0_gvChannelUC”);;但是gvChannelUC是空的,这不起作用。您必须在
CtriInterfaceUC
的控件集合中递归搜索。尽管我知道你的想法,但我还是非常鼓励在你的UserControl中调用一个方法,在我尝试的时候,我们会讨论一些东西吗;。它不会获得用户控件的单个GridView。我一直在尝试递归地查找控件函数。很快就会得到结果。你说的“动态用户控件”是什么意思?即使使用LoadControl,它也是类型转换到用户控件类。这允许你调用该类的任何公共方法很高兴见到你,Nunesspascal。提前谢谢你。我需要和你讨论一些事情,以便更清楚地了解。啊,“动态用户控件”,我的意思是当我们使用“foreach(DTDeviceListByRole.Rows中的DataRow行)”时,我们只是在占位符中添加了许多用户控件,而不关心这些控件。因此,我们将很难在用户控件上获取网格属性。是吗?你的建议很有用。非常感谢。我还有另一个问题,我们可以讨论一下吗?我想在另一个问题中,你指的是同一段代码?您应该将事件处理程序添加到访问网格属性的同一节中的控件中。i、 e.
ctrIpInterfaceUC.RaiseSelectedIndexChanged+=新事件处理程序(OnRaiseSelectedIndexChanged)如果您使用双制表符技巧,它将为您删除该方法。单个UserControl有一个ID,因此,我注册事件以分离UserControl。没有引用相同的代码块。您的想法是我只向UserControl添加一个事件。无法将事件添加到下一个UserControl?我们可以通过stackover上的聊天转到讨论吗?我想知道您是否与我讨论此问题
public GridView Grid
{
  get { return gvChannelUC; }
}
List<string, string> Grids = new List<string, string>(); // <UCId, GridId>
...
ctrIpInterfaceUC = (Test2.SetupGroup.Ipservice.IpInterfaceUC)LoadControl("IpInterfaceUC.ascx");
string Id = "device_"+ip+"_"+port+"$"+indexInterface;

GridView ctrGridView = ctrIpInterfaceUC.Grid;
Grids.Add(Id, ctrGridView.ClientID);

Control ctr = (Control)ctrIpInterfaceUC;
ctr.ID = Id
phDevices.Controls.Add(ctr);//PlaceHolder for add many UserControl
...