Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update Label on page load in web user control

Status
Not open for further replies.

primagic

IS-IT--Management
Jul 24, 2008
476
GB
I have a label in a web user control. I am trying to add the current date to this label on page load.

My code within the user control is:

Code:
Public Property DateLabelText() As String

        Get
            Return lblDate.Text
        End Get

        Set(value As String)
            lblDate.Text = DateTime.Now.ToString("dd MMM yyyy")
        End Set
    End Property

Should this update the label on page load or do I need to add code to the aspx page?
 
I would add it to the page_load event. Sorry, it's not VB, but the syntax should be very similar.
You should already have the page_load event in the code behind. This would be all of the code you need to set the value of the label. I used the shortdatestring. If you need the formatting you specified, that would work like you have it as well.

Code:
protected void Page_Load(object sender, EventArgs e)
{[URL unfurl="true"]http://tek-tips.com/viewthread.cfm?qid=1694424[/URL]
    lblDate.Text = DateTime.Now.ToShortDateString();
}

A quick Google suggests the VB syntax would be something like (not tested):
Code:
Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)  
    lblDate.Text = DateTime.Now.ToShortDateString()
End Sub

Good luck!

- Mark
 
Not working

This is now what I have in my code behind on the usercontrol

Code:
Public MyDate As String
    Public ReadOnly Property GetDate() As String
        Get
            Return MyDate
        End Get
    End Property
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        lblDate.Text = DateTime.Now.ToString("dd MMM yyyy")
        MyDate = lblDate.Text

    End Sub


My webpage

Code:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        AddDate()
    End Sub

    Protected Sub AddDate()
        Dim DateValue As String = ctlHeader.MyDate
        ctlHeader.MyDate = DateValue
    End Sub

However it is giving me an error telling me that 'MyDate' is not a member of the usercontrol. But haven't I declared it properly?
 
I'm not sure why you need a property at all. I'm not all that familiar with VB.Net, but depending on how you have the eventwireup set, you may need that "Handles Me.Load" in the Page_load event of the user control as well (I see it is in your webpage page_load event.

Ideally, the user control should set its own label on load and that's all you should need. Your page shouldn't have to know or care about what the label's value is. If you were trying to share some specific value, that may be a different story, but you're just showing the current server date.

My User control Code Behind:
Code:
public partial class MyUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblDate.Text = DateTime.Now.ToString("dd MMM yyyy");
    }
}

My Web Page Markup (just dragged user control onto the design surface):
Code:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="TestApp._Default" %>

<%@ Register src="MyUserControl.ascx" tagname="MyUserControl" tagprefix="uc1" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        <uc1:MyUserControl ID="MyUserControl1" runat="server" />
    </p>
    <p>
    </p>
</asp:Content>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top