Link to home
Start Free TrialLog in
Avatar of tim_chamberlain
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.
Avatar of raterus
raterus
Flag of United States of America image

Your issue is deeper than just setting SelectedMenu, your usercontrol isn't instantiated.  There are many reason why may occur.

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.
Avatar of tim_chamberlain
tim_chamberlain

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.DebuggerStepThrough()> 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 designerPlaceholderDeclaration 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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateProfile.Click
        Response.Redirect("createprofile.aspx")
    End Sub

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        Response.Redirect("intro.aspx")
    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.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Public WithEvents tbsMenu As Microsoft.Web.UI.WebControls.TabStrip

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration 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_SelectedIndexChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbsMenu.SelectedIndexChange
        HandleTabChange(tbsMenu.SelectedIndex)
    End Sub


    Sub HandleTabChange(ByVal i As Integer)

        Select Case i
            Case 0 'start
                Response.Redirect("default.aspx")
            Case 1 'introduction
                Response.Redirect("intro.aspx")
            Case 2 'about
                Response.Redirect("about.aspx")
            Case 3 'pd
                Response.Redirect("discussion.aspx")
            Case 4 'cc
                Response.Redirect("capabilities.aspx")
            Case 5 'dcg
                Response.Redirect("development.aspx")
            Case 6 'pp
                Response.Redirect("plan.aspx")
            Case 7 'gc
                Response.Redirect("comments.aspx")
            Case 8 'logout
                FormsAuthentication.SignOut()
                Response.Redirect("intro.aspx")
        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("~/UserControls/data.ascx")


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("~/UserControls/data.ascx")
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.
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("~/UserControls/data.ascx")
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"
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("~/UserControls/data.ascx")
ctl.ID = "tbMenu"
phMenu.Controls.Add(ctl)
Dim tbsMenu As tabstrip = DirectCast(ctl,tabstrip)
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/tabstrip.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
ASKER CERTIFIED SOLUTION
Avatar of raterus
raterus
Flag of United States of America 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
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/tabstrip.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
i meant
Dim ctl As tabstrip
sorry raterus, when I started writiing my comment, ur comment was not there ..
Thanks everyone who assisted with this question.  raterus, your code works using:

Private tbsMenu as TabStrip

tbsMenu = DirectCast(LoadControl("controls/tabstrip.ascx"), tabstrip)
tbsMenu.ID = "tbsMenu"
Me.phMenu.Controls.Add(tbsMenu) *not ctl
i think, that is what i had written ... isn't it? or am i missing something?
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 ..