【问题标题】:Get GridView in Multiple UserControl from codebehind从代码隐藏中获取多个用户控件中的 GridView
【发布时间】:2013-01-04 03:52:04
【问题描述】:

IpInterfaceUC 用户控件:

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

Init 的代码隐藏

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
}

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>

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

【问题讨论】:

    标签: c# asp.net html code-behind


    【解决方案1】:

    通过 UserControl 上的公共属性公开网格视图:

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

    【讨论】:

    • 您的建议很有用。非常感谢。我还有一个问题,可以讨论一下。stackoverflow.com/questions/14189851/…
    • 我假设在另一个问题中您指的​​是同一代码块?您应该将事件处理程序添加到您访问 Grid 属性的同一部分中的控件。即ctrIpInterfaceUC.RaiseSelectedIndexChanged += new EventHandler&lt;EventArgs&gt;(OnRaiseSelectedIndexChanged); 如果您使用双标签技巧,它将为您排除方法。
    • 个别UserControl有一个ID,所以,我注册Event来分开UserControl。没有引用同一个代码块。
    • 你的意思是我只是将一个事件添加到 UserControl。不能将事件添加到下一个 UserControl?
    • 我们可以通过stackover上的聊天进行讨论吗?我想知道你是否与我讨论过这个问题
    【解决方案2】:

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

    您可以向您的 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
    }
    

    【讨论】:

    • 我试过了:GridView gvChannelUC = (GridView)FindControl("device_192.168.2.19_3331_0_gvChannelUC");;但是gvChannelUC是空的。
    • 那行不通。您将不得不在ctrIpInterfaceUC 的控件集合中递归搜索。我强烈鼓励在你的 UserControl 中调用一个方法
    • 我知道你的想法,当我尝试的时候,我们会讨论一些事情吗?因为我使用动态用户控件,所以,我们不能使用 ctrIPinterfaceUC.getGridView();。它不会得到个人一个UserControl的GridView。我一直在尝试递归查找控件函数。结果很快。
    • “动态用户控件”是什么意思?即使您使用 LoadControl,它也会被强制转换为您的用户控件类。这允许您调用该类的任何公共方法
    • 很高兴见到你,nunespascal。提前谢谢你。我需要和你讨论清楚一些事情。;)。啊,“动态用户控件”我的意思是当我们使用“foreach(DataRow 行在 dtDevicesListByRole.Rows)" 中,我们只是在占位符中添加了许多用户控件,并且不关心这些控件。这样,我们将很难在用户控件上获取 Grid 属性。是吗?
    猜你喜欢
    • 2012-12-21
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    相关资源
    最近更新 更多