David Rudlin
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)
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>
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.
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; }
}
public int UsersCount
{
get { return GridView1.Rows.Count; }
}
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
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.
If you want to expand some functionality from 1st user control (where grid declared), you should add public method/properties to 1st user control.
ASKER
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.UsersCou nt).
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.SomePropert y = LAschoolselector.UsersCoun t
or
LAdataAnalysis.DoSomething (LAschools elector.Us ersCount)
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_UsersCoun tChanged(o bject sender, EventArgs e)
{
// do something with LAdataAnalysis control
}
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.SomePropert
or
LAdataAnalysis.DoSomething
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_UsersCoun
{
// do something with LAdataAnalysis control
}
ASKER
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.FindContro l("UsersCo unt"), 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?
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ctrl As LAschoolSelector = DirectCast(Page.FindContro
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/
http://asp.net-tutorials.com/user-controls/events/
Dim ctrl As LAschoolSelector = DirectCast(Page.FindContro l("LAschoo lselector" ), LAschoolSelector)
But that is bad way of creating user controls.
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)
ASKER
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.FindContro l("UsersCo unt"), 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.
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ctrl As LAschoolSelector = DirectCast(Page.FindContro
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.
ASKER
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.FindContro l("LAschoo lselector" ), LAschoolSelector)
Dim x As Integer = ctrl.UsersCount
End Sub
....but still get"Object reference not set to an instance of an object."
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.FindContro
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.FindCont rol("Conte nt1").Find Control("L Aschoolsel ector"), LAschoolSelector)
so
Dim ctrl As LAschoolSelector = TryCast(Me.Master.FindCont
ASKER
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.FindCont rol("Conte nt1").Find Control("L Aschoolsel ector"), LAschoolSelector)
but no luck.
Dim ctrl As LAschoolSelector = TryCast(Me.Parent.FindCont
but no luck.
You doing something wrong. Code above works.
Master is the property of Page.
Master is the property of Page.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I cannot get the "Me.Master" part of your example. The only thing I can construct is:
Dim ctrl As LAschoolSelector = TryCast(Page.Master.FindCo ntrol("Con tent1").Fi ndControl( "LAschools elector"), LAschoolSelector)
and also:
Dim ctrl As UserControl = TryCast(Page.Master.FindCo ntrol("Con tent1").Fi ndControl( "LAschools elector"), UserControl)
but neither works.
Dim ctrl As LAschoolSelector = TryCast(Page.Master.FindCo
and also:
Dim ctrl As UserControl = TryCast(Page.Master.FindCo
but neither works.
ASKER
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.FindCo ntrol("Con tentPlaceH older1").F indControl ("LAschool selector") , LAschoolSelector)
Dim y As Integer = ctrl.UsersCount
End Sub
Thanks lazyberezovsky for your patience and help
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.FindCo
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.a scx" TagPrefix="uc" TagName="One" %>
<%@ Register Src="~/WebUserControlTwo.a scx" TagPrefix="uc" TagName="Two" %>
<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="Cont entPlaceHo lder1">
<uc:One ID="uc1" OnUsersCountChanged="uc1_U sersCountC hanged" 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(obje ct sender, EventArgs e)
{
uc2.DoSomething(uc1.UsersC ount);
}
}
Let me think.
BTW, why don't use normal communication?
E.g.
<%@ Register Src="~/WebUserControlOne.a
<%@ Register Src="~/WebUserControlTwo.a
<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="Cont
<uc:One ID="uc1" OnUsersCountChanged="uc1_U
<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(obje
{
uc2.DoSomething(uc1.UsersC
}
}
U R Welcome :)
http://www.dotnetcurry.com/ShowArticle.aspx?ID=155