0

IpInterfaceUC 用户控件:

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

初始化的代码隐藏

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

显示

<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>

问题 如何从多个 UserControl 中获取 gridview?

4

2 回答 2

1

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

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
...
于 2013-01-04T04:08:25.587 回答
0

虽然您可以递归地使用 FindControl 来查找它,但更好的方法是让 UserControlIpInterfaceUC决定如何将数据绑定到其中的控件。

您可以向 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
}
于 2013-01-04T04:00:17.943 回答