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

Al230762Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krunal_shahCommented:
check the article below for the solution,
http://www.dotnetcurry.com/ShowArticle.aspx?ID=155
0
lazyberezovskyCommented:
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.
0
lazyberezovskyCommented:
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; }
     }
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Al230762Author Commented:
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

0
lazyberezovskyCommented:
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.
0
Al230762Author Commented:
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?
0
lazyberezovskyCommented:
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
}
0
Al230762Author Commented:
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?
0
lazyberezovskyCommented:
See events (PageTitleUpdated) and properties (PageTitle) in user controls:
http://asp.net-tutorials.com/user-controls/events/

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

But that is bad way of creating user controls.
0
lazyberezovskyCommented:
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

0
Al230762Author Commented:
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.
0
lazyberezovskyCommented:
Again, if you r going this way (not best way), you should search control, not property name. See my previous example.
0
Al230762Author Commented:
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."
0
lazyberezovskyCommented:
Mmm. I get it - you are using MasterPage.
so
  Dim ctrl As LAschoolSelector = TryCast(Me.Master.FindControl("Content1").FindControl("LAschoolselector"), LAschoolSelector)
0
Al230762Author Commented:
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.
0
lazyberezovskyCommented:
You doing something wrong. Code above works.
Master is the property of Page.
0
lazyberezovskyCommented:
See: http://www.west-wind.com/Weblog/posts/5127.aspx

Here is generic method:
Dim ctrl As ControlType = TryCast(Me.Master.FindControl("ContentPlaceHolderID").FindControl("ControlID"), ControlType)
'use ctrl.PropertyName
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Al230762Author Commented:
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.
0
Al230762Author Commented:
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
0
lazyberezovskyCommented:
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);
    }
}
0
lazyberezovskyCommented:
U R Welcome :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.