Results 1 to 5 of 5

Thread: cant set that label...from other page

  1. #1

    Thread Starter
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492

    cant set that label...from other page

    I have a footer page:

    footer.ascx which I reuse as a user control over and over across many pages. So I wanted to place a label on the bottom of this footer and change it based on the page I was on...

    Basically this footer is a water mark to say the page that I am on...
    For instacnce, if I am looking at default.aspx I want it to read "HOME" in that label. If I am on admin.aspx I want the label to read ADMIN.

    For the life of me I cant make this work...
    I placed the label in the footer...and I tried to write functions to obtain this text but it would not work...

    Like I tried

    Dim f as Footer

    f.SetText("Admin")

    Meaning it would call a public function inside this page and set the text. In the footer class I would have:

    Public Function SetText(strText as String)
    Me.lblMyLabel = strText
    End Function

    But I keep getting errors like cannot set null reference to this yada yaday yada...

    I tried dimming it as a user control and trying to find it on the page and set it and that didnt work.

    Can someone help me out ??????????

    Thanks,
    Jon

  2. #2
    Addicted Member
    Join Date
    Sep 2004
    Posts
    133
    Try something like this:

    Code:
    Dim f as New Footer
    f.SetText("Admin")
    Or, if it is added as a usercontrol I think you can use the name of the usercontrol.

    For example:
    Code:
    footercontrolname.SetText("Admin")

  3. #3

    Thread Starter
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by schenz
    Try something like this:

    Code:
    Dim f as New Footer
    f.SetText("Admin")
    Or, if it is added as a usercontrol I think you can use the name of the usercontrol.

    For example:
    Code:
    footercontrolname.SetText("Admin")
    I have3 tried that before with no luck...
    I get the following:

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:


    Line 40: End Sub
    Line 41: Public Function SetFooter(ByVal f As String)
    Line 42: Me.lblFooterName.Text = f
    Line 43: End Function
    Line 44: End Class


    Look I have a footer.ascx that has that function setfooter.

    Code:
    Public Class footer
        Inherits System.Web.UI.UserControl
        Protected WithEvents lblFooterName As System.Web.UI.WebControls.Label
    #Region " Web Form Designer Generated Code "
    
        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    
        End Sub
    
        '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
            'Put user code to initialize the page here
            'lblFooterName.Text = "home"
        End Sub
        Public Function SetFooter(ByVal f As String)
            Me.lblFooterName.Text = f
        End Function
    End Class
    I call this function with:

    Dim f as New Footer
    f.SetText("ADMIN")

    But it doesnt work as I have said, I have posted this problem on many forums and no one has been able to help me.

    In a windows type application this would take maybe 30 seconds...

    This is so useless

  4. #4
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,633
    This is because the page label doesn't get initialised. You can do:
    VB Code:
    1. Public Function SetFooter(ByVal f As String)
    2.         lblFooterName = New System.Web.UI.WebControls.Label
    3.         Me.lblFooterName.Text = f
    4.     End Function
    But thats just wrong.

    You are going about this the wrong way.

    What you need to do is this. In the usercontrol have:
    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'Put user code to initialize the page here
    3.     End Sub
    4.  
    5.     Public Property FooterText() As String
    6.         Get
    7.             Return Me.lblFooterName.Text
    8.         End Get
    9.         Set(ByVal Value As String)
    10.             Me.lblFooterName.Text = Value
    11.         End Set
    12.     End Property
    Then on your web page:
    Code:
    	<body MS_POSITIONING="GridLayout">
    		<form id="Form1" method="post" runat="server">
    			<uc1:Footer id="Footer1" FooterText="Moooooooooose" runat="server"></uc1:Footer>
    		</form>
    	</body>
    Does that make sense?

    Woof

  5. #5
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,633
    If you want to set it from the Page_Load event, then in your web page you add the following code:
    VB Code:
    1. Public Class WebForm1
    2.     Inherits System.Web.UI.Page
    3.     Protected Footer1 As Footer 'this is the line you need to add
    Where WebForm1 is the name of your web page, and Footer1 is the ID of the usercontrol on your page. They MUST have the same name.
    Then in Page_Load just do:
    VB Code:
    1. Me.Footer1.FooterText = "BADGERS!"
    Simple.

    Woof

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width