Link to home
Start Free TrialLog in
Avatar of David Rudlin
David RudlinFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How to access gridview inside usercontrol from another usercontrol

Hello... i have 2 ascx user controls (both with VB code behind) placed in a contentplaceholder inside an updatepanel within a aspx page based on a masterpage... in the first user control i have a gridview and in the second i want to loop through the rows and get values from the first user control's gridview

I would like some guidance on how I can access the gridview of the first usercontrol from the second usercontrol please.

Thanks in advance. (Code for aspx page attached)
<%@ Page Language="VB" MasterPageFile="~/Pages/MasterPage.master" Title="LA module - Data Analysis" %>
<%@ Register TagPrefix="PMW" Namespace="PMW" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<PMW:LAselector SkinFileName="LAmodule/LAselector.ascx" ID="LAschoolselector" runat="server" Secured="true" />
<PMW:LAdataAnalysis ID ="LAdataAnalysis" runat = "Server" SkinFileName="audit/LAdataAnalysis.ascx"  Secured="true" />
</asp:Content>

Open in new window

Avatar of krunal_shah
krunal_shah

check the article below for the solution,
http://www.dotnetcurry.com/ShowArticle.aspx?ID=155
Most hard way is to create property with type GridView in first user control that will return grid.
Better way is to add properties/methods for functionality that you need from grid inside first user control.
E.g: you have Users user control, with gridview for listing users. And you want to know users count in this control from second control. Then better way to create property:

  public int UsersCount
      {
            get {  return GridView1.Rows.Count; }
     }
Avatar of David Rudlin

ASKER

thanks for the link krunal_shah:

 lazyberezovsky: thank you - do I place your suggested code in the 2nd usercontrol to get the gridview in the 1st usercontrol ? I have added the following to the 2nd usercontrol for access the gridview ("grdGroups") in the 1st usercontrol but of course I get "Name 'grdGroups is not declared':

Public ReadOnly Property UsersCount() As Integer
            Get
                Return grdGroups.Rows.Count
            End Get
        End Property

No :)
If you want to expand some functionality from 1st user control (where grid declared), you should add public method/properties to 1st user control.
Right, thank you - I realised that as soon as I had sent that last post. So I added the property to usercontrol 1. How do I now get that "UsersCount" property in the 2nd usercontrol?
If 1st usercontrol is nested in 2nd usercontrol, then just call 1st usercontrol instance (LAschoolselector.UsersCount).

But in your case both controls are independent.
Hmm. Let me think.

1) you can link controls together in page code
    e.g. in Page_Load
    LAdataAnalysis.SomeProperty = LAschoolselector.UsersCount
    or
   LAdataAnalysis.DoSomething(LAschoolselector.UsersCount)

2) if you want 2nd control to change when something happens in 1st control, then you need to add event to 1st control
e.g. public event EventHandler UsersCountChanged and fire that event when you add/remove rows to grid.
In page code you can subscribe to that event:
protected void LAschoolselector_UsersCountChanged(object sender, EventArgs e)
{
      // do something with  LAdataAnalysis control
}
As suggested in the article suggested by krunal_shah I added the following to the 2nd usercontrol:

  Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim ctrl As LAschoolSelector = DirectCast(Page.FindControl("UsersCount"), LAschoolSelector)
            Dim x As Integer = ctrl.UsersCount
        End Sub

but I get "Object reference not set to an instance of an object."

Any idea why?
See events (PageTitleUpdated) and properties (PageTitle) in user controls:
http://asp.net-tutorials.com/user-controls/events/

Dim ctrl As LAschoolSelector = DirectCast(Page.FindControl("LAschoolselector"), LAschoolSelector)

But that is bad way of creating user controls.
Hm, if type of controls is LAselector and ID is LAschoolselecto, then:
Dim ctrl As LAselector = DirectCast(Page.FindControl("LAschoolselector"), LAselector)

Open in new window

No, the usercontrol ID is "LAschoolSelector" (i might have confused you with a typo earlier) so in usercontrol2, attempting to get UsersCount from  usercontrol1 ("LAschoolSelector")   I have:

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim ctrl As LAschoolSelector = DirectCast(Page.FindControl("UsersCount"), LAschoolSelector)
            Dim x As Integer = ctrl.UsersCount
        End Sub
but as I said previosuly but I get "Object reference not set to an instance of an object."
I have also looked at the last link you sent me but that hasn't helped I'm afraid.
Again, if you r going this way (not best way), you should search control, not property name. See my previous example.
lazyberezovsky - thanks for your help so far.
Still trying the way that you DON'T recommend :

In usercontrol1 ("LAschoolselector") I have:

 Public ReadOnly Property UsersCount() As Integer
            Get
                Return Me.grdSchools.Rows.Count
            End Get
        End Property

In usercontrol2 I have:

 Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim ctrl As LAschoolSelector = DirectCast(Page.FindControl("LAschoolselector"), LAschoolSelector)
            Dim x As Integer = ctrl.UsersCount
        End Sub

....but still get"Object reference not set to an instance of an object."
Mmm. I get it - you are using MasterPage.
so
  Dim ctrl As LAschoolSelector = TryCast(Me.Master.FindControl("Content1").FindControl("LAschoolselector"), LAschoolSelector)
In your example, i can't get a reference to the Master page !"MasterPage.Master"). I also tried:

            Dim ctrl As LAschoolSelector = TryCast(Me.Parent.FindControl("Content1").FindControl("LAschoolselector"), LAschoolSelector)
 
but no luck.
You doing something wrong. Code above works.
Master is the property of Page.
ASKER CERTIFIED SOLUTION
Avatar of lazyberezovsky
lazyberezovsky
Flag of Belarus image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I cannot get the "Me.Master" part of your example. The only thing I can construct is:

Dim ctrl As LAschoolSelector = TryCast(Page.Master.FindControl("Content1").FindControl("LAschoolselector"), LAschoolSelector)

and also:

 Dim ctrl As UserControl = TryCast(Page.Master.FindControl("Content1").FindControl("LAschoolselector"), UserControl)

but neither works.
OK. I have it now:

In usercontrol1:

 Public ReadOnly Property UsersCount() As Integer
            Get
                Return Me.grdSchools.Rows.Count
            End Get
        End Property

In usercontrol2:

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim ctrl As LAschoolSelector = TryCast(Page.Master.FindControl("ContentPlaceHolder1").FindControl("LAschoolselector"), LAschoolSelector)
            Dim y As Integer = ctrl.UsersCount
        End Sub

Thanks lazyberezovsky for your patience and help
Ah, sorry, forgot that you are trying to find control from other control.
Let me think.

BTW, why don't use normal communication?
E.g.

<%@ Register Src="~/WebUserControlOne.ascx" TagPrefix="uc" TagName="One" %>
<%@ Register Src="~/WebUserControlTwo.ascx" TagPrefix="uc" TagName="Two" %>
<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
    <uc:One ID="uc1" OnUsersCountChanged="uc1_UsersCountChanged" runat="server" />
    <uc:Two ID="uc2" runat="server" />
</asp:Content>

public partial class WebUserControlOne : System.Web.UI.UserControl
{
    public event EventHandler UsersCountChanged;

    public string UsersCount
    {
        get { return TextBox1.Text; }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (UsersCountChanged != null)
            UsersCountChanged(this, EventArgs.Empty);
    }
}

public partial class WebUserControlTwo : System.Web.UI.UserControl
{
    public void DoSomething(string value)
    {
        Label1.Text = value;
    }
}

And:
public partial class _Default : System.Web.UI.Page
{
    protected void uc1_UsersCountChanged(object sender, EventArgs e)
    {
        uc2.DoSomething(uc1.UsersCount);
    }
}
U R Welcome :)