tim_chamberlain
asked on
Pass a parameter to a user control
I have a simple user control containing a tab strip used for page navigation. I need each page that uses the control to pass a parameter to the user control. Pretty basic stuff. I have tried several configurations to acheive this and both return an "Object reference not set to an instance of an object" error. I have used the methods described below with success in other applications but can't seem to identify where the issue is here:
In the user control:
Private _menuitem As String
Public Property SelectedMenu() As String
Get
Return _menuitem
End Get
Set(ByVal Value As String)
_menuitem = Value
End Set
End Property
In the aspx page:
Method 1:
Dim tbsMenu As tabstrip = DirectCast(Me.FindControl( "tbsMenu") , tabstrip)
tbsMenu.SelectedMenu = "introduction"
Method 2:
Protected WithEvents tbsMenu As tabstrip
tbsMenu.SelectedMenu = "introduction"
Any pointers on how I can get this to work would be appreciated.
Thanks very much.
In the user control:
Private _menuitem As String
Public Property SelectedMenu() As String
Get
Return _menuitem
End Get
Set(ByVal Value As String)
_menuitem = Value
End Set
End Property
In the aspx page:
Method 1:
Dim tbsMenu As tabstrip = DirectCast(Me.FindControl(
tbsMenu.SelectedMenu = "introduction"
Method 2:
Protected WithEvents tbsMenu As tabstrip
tbsMenu.SelectedMenu = "introduction"
Any pointers on how I can get this to work would be appreciated.
Thanks very much.
ASKER
raterus,
I've checked point 1 and the ID's are the same. I've tried a few different things but still no success. Here's the aspx page code and the ascx code. Thanks for your help.
Public Class _default
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
End Sub
Protected WithEvents lblNewUser As System.Web.UI.WebControls. Label
Protected WithEvents btnCreateProfile As System.Web.UI.WebControls. Button
Protected WithEvents lblReturningUser As System.Web.UI.WebControls. Label
Protected WithEvents btnLogin As System.Web.UI.WebControls. Button
Protected WithEvents imgNewUsers As System.Web.UI.WebControls. Image
Protected WithEvents imgReturningUsers As System.Web.UI.WebControls. Image
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara tion As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tbsMenu As New tabstrip
tbsMenu.ID = "tbMenu"
Page.Controls.Add(tbsMenu)
If Not Page.IsPostBack Then
tbsMenu.SelectedMenu = "introduction"
Me.lblNewUser.Text = "If this is your first time using this application you will need to create a profile for yourself that will store your performance review and performance plan details. Only you and your manager can view the information you enter. If you are a manager you will need to create a profile before completing your own performance review or reviewing those of your direct reports.<br><br>Please click the button below to set up your profile."
Me.lblReturningUser.Text = "If you have used this application before and have created a profile you will need your username and password to access your performance review information.<br><br>Please click the button below to log in."
End If
End Sub
Private Sub btnCreateProfile_Click(ByV al sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateProfile.Click
Response.Redirect("createp rofile.asp x")
End Sub
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Response.Redirect("intro.a spx")
End Sub
End Class
ASCX:
Imports System.Web.Security
Public Class tabstrip
Inherits System.Web.UI.UserControl
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
End Sub
Public WithEvents tbsMenu As Microsoft.Web.UI.WebContro ls.TabStri p
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara tion As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private _menuitem As String
Public Property SelectedMenu() As String
Get
Return _menuitem
End Get
Set(ByVal Value As String)
_menuitem = Value
End Set
End Property
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Select Case SelectedMenu
Case "start"
Me.tbsMenu.SelectedIndex = 0
Case "introduction"
Me.tbsMenu.SelectedIndex = 1
Case "about"
Me.tbsMenu.SelectedIndex = 2
Case "pd"
Me.tbsMenu.SelectedIndex = 3
Case "cc"
Me.tbsMenu.SelectedIndex = 4
Case "dcg"
Me.tbsMenu.SelectedIndex = 5
Case "pp"
Me.tbsMenu.SelectedIndex = 6
Case "gc"
Me.tbsMenu.SelectedIndex = 7
Case "logout"
HandleTabChange(8)
End Select
End Sub
Private Sub tbsMenu_SelectedIndexChang e(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbsMenu.SelectedIndexChang e
HandleTabChange(tbsMenu.Se lectedInde x)
End Sub
Sub HandleTabChange(ByVal i As Integer)
Select Case i
Case 0 'start
Response.Redirect("default .aspx")
Case 1 'introduction
Response.Redirect("intro.a spx")
Case 2 'about
Response.Redirect("about.a spx")
Case 3 'pd
Response.Redirect("discuss ion.aspx")
Case 4 'cc
Response.Redirect("capabil ities.aspx ")
Case 5 'dcg
Response.Redirect("develop ment.aspx" )
Case 6 'pp
Response.Redirect("plan.as px")
Case 7 'gc
Response.Redirect("comment s.aspx")
Case 8 'logout
FormsAuthentication.SignOu t()
Response.Redirect("intro.a spx")
End Select
End Sub
End Class
I've checked point 1 and the ID's are the same. I've tried a few different things but still no success. Here's the aspx page code and the ascx code. Thanks for your help.
Public Class _default
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg
End Sub
Protected WithEvents lblNewUser As System.Web.UI.WebControls.
Protected WithEvents btnCreateProfile As System.Web.UI.WebControls.
Protected WithEvents lblReturningUser As System.Web.UI.WebControls.
Protected WithEvents btnLogin As System.Web.UI.WebControls.
Protected WithEvents imgNewUsers As System.Web.UI.WebControls.
Protected WithEvents imgReturningUsers As System.Web.UI.WebControls.
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tbsMenu As New tabstrip
tbsMenu.ID = "tbMenu"
Page.Controls.Add(tbsMenu)
If Not Page.IsPostBack Then
tbsMenu.SelectedMenu = "introduction"
Me.lblNewUser.Text = "If this is your first time using this application you will need to create a profile for yourself that will store your performance review and performance plan details. Only you and your manager can view the information you enter. If you are a manager you will need to create a profile before completing your own performance review or reviewing those of your direct reports.<br><br>Please click the button below to set up your profile."
Me.lblReturningUser.Text = "If you have used this application before and have created a profile you will need your username and password to access your performance review information.<br><br>Please
End If
End Sub
Private Sub btnCreateProfile_Click(ByV
Response.Redirect("createp
End Sub
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Response.Redirect("intro.a
End Sub
End Class
ASCX:
Imports System.Web.Security
Public Class tabstrip
Inherits System.Web.UI.UserControl
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.Debugg
End Sub
Public WithEvents tbsMenu As Microsoft.Web.UI.WebContro
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private _menuitem As String
Public Property SelectedMenu() As String
Get
Return _menuitem
End Get
Set(ByVal Value As String)
_menuitem = Value
End Set
End Property
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Select Case SelectedMenu
Case "start"
Me.tbsMenu.SelectedIndex = 0
Case "introduction"
Me.tbsMenu.SelectedIndex = 1
Case "about"
Me.tbsMenu.SelectedIndex = 2
Case "pd"
Me.tbsMenu.SelectedIndex = 3
Case "cc"
Me.tbsMenu.SelectedIndex = 4
Case "dcg"
Me.tbsMenu.SelectedIndex = 5
Case "pp"
Me.tbsMenu.SelectedIndex = 6
Case "gc"
Me.tbsMenu.SelectedIndex = 7
Case "logout"
HandleTabChange(8)
End Select
End Sub
Private Sub tbsMenu_SelectedIndexChang
HandleTabChange(tbsMenu.Se
End Sub
Sub HandleTabChange(ByVal i As Integer)
Select Case i
Case 0 'start
Response.Redirect("default
Case 1 'introduction
Response.Redirect("intro.a
Case 2 'about
Response.Redirect("about.a
Case 3 'pd
Response.Redirect("discuss
Case 4 'cc
Response.Redirect("capabil
Case 5 'dcg
Response.Redirect("develop
Case 6 'pp
Response.Redirect("plan.as
Case 7 'gc
Response.Redirect("comment
Case 8 'logout
FormsAuthentication.SignOu
Response.Redirect("intro.a
End Select
End Sub
End Class
First Problem
>>
Dim tbsMenu As New tabstrip
tbsMenu.ID = "tbMenu"
Page.Controls.Add(tbsMenu)
<<
tabstrip is a UserControl. You do not load a user control like that, you do it this way:
Dim ctl As Control = LoadControl("~/UserControl s/data.asc x")
Next, Page.Controls.Add will add this to the end of the controls collection and not inside the HtmlForm Control.
Thus, typically we use the PlaceHolder (it does not emit any HTML Tags, and it does not implement INamingContainer, so the child controls inside it are accessible directly via FindControl method of the principal container)
Thus,
Dim ctl As Control = LoadControl("~/UserControl s/data.asc x")
ctl.ID = "tbMenu"
phMenu.Controls.Add(ctl)
Second Problem
==========
It would be far wiser to add the User Control inside the Init Event instead of the Load Event, but if you can get away with it, fine.
>>
Dim tbsMenu As New tabstrip
tbsMenu.ID = "tbMenu"
Page.Controls.Add(tbsMenu)
<<
tabstrip is a UserControl. You do not load a user control like that, you do it this way:
Dim ctl As Control = LoadControl("~/UserControl
Next, Page.Controls.Add will add this to the end of the controls collection and not inside the HtmlForm Control.
Thus, typically we use the PlaceHolder (it does not emit any HTML Tags, and it does not implement INamingContainer, so the child controls inside it are accessible directly via FindControl method of the principal container)
Thus,
Dim ctl As Control = LoadControl("~/UserControl
ctl.ID = "tbMenu"
phMenu.Controls.Add(ctl)
Second Problem
==========
It would be far wiser to add the User Control inside the Init Event instead of the Load Event, but if you can get away with it, fine.
ASKER
b1xml2,
Thanks for the reply. I need to clarify a couple of things:
This code should go in Page_Init:
Dim ctl As Control = LoadControl("~/UserControl s/data.asc x")
ctl.ID = "tbMenu"
phMenu.Controls.Add(ctl)
What is phMenu in the context of your example?
Then to pass the parameter to the control you would use code like the following(?):
Dim tbsMenu As tabstrip = DirectCast(Me.FindControl( "tbMenu"), tabstrip)
tbsMenu.SelectedMenu = "start"
Thanks for the reply. I need to clarify a couple of things:
This code should go in Page_Init:
Dim ctl As Control = LoadControl("~/UserControl
ctl.ID = "tbMenu"
phMenu.Controls.Add(ctl)
What is phMenu in the context of your example?
Then to pass the parameter to the control you would use code like the following(?):
Dim tbsMenu As tabstrip = DirectCast(Me.FindControl(
tbsMenu.SelectedMenu = "start"
1. phMenu is just pseudo code pointing to a placeholder in your page.. The point being the control needs to be inside the HtmlForm control.
2.
Dim ctl As Control = LoadControl("~/UserControl s/data.asc x")
ctl.ID = "tbMenu"
phMenu.Controls.Add(ctl)
Dim tbsMenu As tabstrip = DirectCast(ctl,tabstrip)
2.
Dim ctl As Control = LoadControl("~/UserControl
ctl.ID = "tbMenu"
phMenu.Controls.Add(ctl)
Dim tbsMenu As tabstrip = DirectCast(ctl,tabstrip)
ASKER
b1xml2,
I must be missing something here - still getting "Object reference not set..." error. Here's the page code: (I've replaced the user control on the page with a placeholder called phMenu. )
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
Dim ctl As Control = LoadControl("controls/tabs trip.ascx" )
ctl.ID = "tbsMenu"
Me.phMenu.Controls.Add(ctl )
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim tbsMenu As tabstrip = DirectCast(tbsMenu, tabstrip)
tbsMenu.SelectedMenu = "start"
End If
End Sub
I must be missing something here - still getting "Object reference not set..." error. Here's the page code: (I've replaced the user control on the page with a placeholder called phMenu. )
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
Dim ctl As Control = LoadControl("controls/tabs
ctl.ID = "tbsMenu"
Me.phMenu.Controls.Add(ctl
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim tbsMenu As tabstrip = DirectCast(tbsMenu, tabstrip)
tbsMenu.SelectedMenu = "start"
End If
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try this
Dim ctl As tbsmenu
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
ctl = LoadControl("controls/tabs trip.ascx" )
ctl.ID = "tbsMenu"
Me.phMenu.Controls.Add(ctl )
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
ctl.SelectedMenu = "start"
End If
End Sub
Dim ctl As tbsmenu
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
ctl = LoadControl("controls/tabs
ctl.ID = "tbsMenu"
Me.phMenu.Controls.Add(ctl
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
ctl.SelectedMenu = "start"
End If
End Sub
i meant
Dim ctl As tabstrip
Dim ctl As tabstrip
sorry raterus, when I started writiing my comment, ur comment was not there ..
ASKER
Thanks everyone who assisted with this question. raterus, your code works using:
Private tbsMenu as TabStrip
tbsMenu = DirectCast(LoadControl("co ntrols/tab strip.ascx "), tabstrip)
tbsMenu.ID = "tbsMenu"
Me.phMenu.Controls.Add(tbs Menu) *not ctl
Private tbsMenu as TabStrip
tbsMenu = DirectCast(LoadControl("co
tbsMenu.ID = "tbsMenu"
Me.phMenu.Controls.Add(tbs
i think, that is what i had written ... isn't it? or am i missing something?
He's just following the rules,
https://www.experts-exchange.com/help.jsp#hi68
https://www.experts-exchange.com/help.jsp#hi68
raterus, for the record, I had not seen ur comment before i posted and also my code was not a corrected version from ur comments ..
I have no problem with what he has done .. but I thought he would have atleast split the points .. no worries ....:-) will get them somewhere else ..
I have no problem with what he has done .. but I thought he would have atleast split the points .. no worries ....:-) will get them somewhere else ..
1) id defined in ascx page doesn't match "tbsMenu" in code behind
2) you are dynamically loading tbsMenu
if this doesn't help, can you post the code of your aspx page.