0

IpInterfaceUC UserControl:

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

CodeBehind for 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 Show

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

Question How do I get gridview from multiple UserControl?

Brian
  • 163
  • 2
  • 18

2 Answers2

1

Expose the gridview through a public property on your UserControl:

public GridView Grid
{
  get { return gvChannelUC; }
}

Then

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
...
Malk
  • 11,855
  • 4
  • 33
  • 32
  • Your suggestion is useful.Thanks so much.I have another issues,May we disccuss about it.http://stackoverflow.com/questions/14189851/cant-access-gridview-event-at-usercontrol-by-codebehind/14190246#comment19706841_14190246 – Brian Jan 09 '13 at 04:26
  • I assume in the other question you are referring to this same block of code? You should add the event handler to the control in the same section that you access the Grid property. i.e. `ctrIpInterfaceUC.RaiseSelectedIndexChanged += new EventHandler(OnRaiseSelectedIndexChanged);` If you use the double tab trick it will stub out the method for you. – Malk Jan 09 '13 at 17:09
  • Individual UserControl has a ID,So that,I register Event to seperate UserControl.Didn't refer to same block of code. – Brian Jan 10 '13 at 03:09
  • Your idea mean that I just add a event to a UserControl.Can't add event to next UserControl? – Brian Jan 10 '13 at 03:09
  • May we move to a discussion by chat on stackover?I wondered if you discuss with me about this issues – Brian Jan 10 '13 at 03:10
0

While you can recursively use FindControl to find it, a much better approach is to let the UserControl IpInterfaceUC decide how to bind data to the controls within it.

You could add a public method ShowData to you UserControl and pass the data to be displayed to it. It can then assign it to 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
}
nunespascal
  • 17,584
  • 2
  • 43
  • 46
  • I tried: **GridView gvChannelUC = (GridView)FindControl("device_192.168.2.19_3331_0_gvChannelUC");**;But gvChannelUC is null. – Brian Jan 04 '13 at 04:07
  • That won't work. You will have to search recursively in the controls collection of `ctrIpInterfaceUC`. I would highly encourage calling a method within your UserControl though – nunespascal Jan 04 '13 at 04:10
  • I know your idea,While I tried it,Will we disccus something.Bcoz I use dynamic usercontrol,So that,We can't use ctrIPinterfaceUC.getGridView();.It'll not get individual GridView of a UserControl.I've been trying recursively find control function.Result soon. – Brian Jan 04 '13 at 04:14
  • What do you mean by "dynamic usercontrol"? Even if you use LoadControl, it is type casted to your user control class. This allows you to call any public methods of that class – nunespascal Jan 04 '13 at 04:36
  • Nice to meet you,nunespascal.Thanks you in advance.I need discuss with you something for clearly.;).Ah,"Dynamic usercontrol" which I mean that when we use "foreach (DataRow row in dtDevicesListByRole.Rows)" , we just add many usercontrol to placeholder,And don't care about these control.So that,We will get difficult for get Grid propertities on usercontrol.Yep? – Brian Jan 04 '13 at 05:03
  • It worked fine now.May we disscuss about another issues.http://stackoverflow.com/questions/14189851/cant-access-gridview-event-at-usercontrol-by-codebehind/14190246#comment19706841_14190246.Thanks – Brian Jan 09 '13 at 04:27
  • It didn't work when I press SelectIndexCchanged at these Gridview – Brian Jan 21 '13 at 08:31
  • In which event did you add it? To receive events, controls should be added before `ProcessPostData` in the page life cycle. – nunespascal Jan 21 '13 at 08:52
  • hey brother,I add it at PageLoad – Brian Jan 21 '13 at 09:05
  • Too late. Refer the page life cycle `ProcessPostData` is called just after `LoadViewState` – nunespascal Jan 21 '13 at 09:08
  • When i add it at PageLoad().It debug to main page (aspx page),It didn't debug at UserControl page (ascx page).So that,Gridview can't show selectIndex Row. – Brian Jan 21 '13 at 09:08
  • May you access my machine by teamviewer or webex for some advice.yes?.Thanks – Brian Jan 21 '13 at 10:17
  • I am sorry, but that is not possible. Firewalls here will block that – nunespascal Jan 21 '13 at 10:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23082/discussion-between-binh-tieu-and-nunespascal) – Brian Jan 21 '13 at 10:37
  • :-( blocked too. Take a look at these answers i have given earlier. I believe you are facing the same problem. http://stackoverflow.com/questions/11834369/user-control-inside-dynamic-user-control-click-event-not-firing/11834955#11834955 http://stackoverflow.com/questions/11752203/dynamic-controls-associated-events-dont-fire-if-recreated-during-page-load/11752521#11752521 – nunespascal Jan 21 '13 at 10:46
  • So,If we want to fire event after click dynamic usercontrol.We'll set it which was not add at pageload() – Brian Jan 21 '13 at 10:56
  • We should be try it with Page_PreLoad() or Page_Init()? – Brian Jan 21 '13 at 10:58
  • Controls added in Page_Load cannot receive events. They don't exist when events are processed – nunespascal Jan 21 '13 at 10:59
  • I tried create dynamic usercontrol in protected void Page_Init() and LoadViewState(object savedState){ base.LoadViewState(savedState); CreateControls(); }.But it didn't fire debug at selectIndex event in UserControl. – Brian Jan 23 '13 at 03:36
  • I refactored my code.It worked fine when I wrote it in PageLoad() – Brian Jan 24 '13 at 04:34
  • Because I set Id of UserControl is 'device_abc$0'.So that,It didn't recognized when I fire event.I set id to 'device_abc__0',It work fine when I press event. – Brian Jan 24 '13 at 04:36
  • My scenario is when I selectIndex at GridView,It must to rewrite all of web.So that,It's very very slowly.Do you have any solution for it? – Brian Jan 24 '13 at 04:38
  • P/s: In main page,PageLoad(){ for(){register dynamic usercontrol}} – Brian Jan 24 '13 at 04:39
  • P/s: In UserControl page, PageLoad(){ getValue from database for fill into GridView} – Brian Jan 24 '13 at 04:40