Accessing UserControl property value from page control

Discussion in 'ASP .Net' started by Iain, Nov 17, 2003.

  1. Iain

    Iain Guest

    Hi

    I have page (testCal.aspx) that contains a usercontrol (custCalendar.ascx) -
    see below signature for code. The UC contains a linkbutton which which when
    clicked, posts back and displays a calendar. When a value in the calendar is
    selected it posts back and then updates a public property (CurrentDate) on
    the UC. Note that 2 postbacks occur.

    I have been attempting to access the public property on the UC, from the
    page (testCal) shown below. The problem is that the label on the page, set
    to CurrentDate on Page_Load, only changes when the LinkButton is clicked to
    change the date for a second time. ie. On a further postback.

    Thanks in advance.

    -Iain



    public class testCal : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Label lbl;
    protected custCalendar myCal;

    private void Page_Load(object sender, System.EventArgs e)
    {
    Response.Write("<br>Source = " + Request.Form["__eventtarget"] +"<br>");
    lbl.Text = CurrentDate;
    }
    }



    public abstract class custCalendar : System.Web.UI.UserControl
    {
    protected System.Web.UI.WebControls.TextBox txtDate;
    protected System.Web.UI.WebControls.LinkButton lnkShowCalendar;
    protected System.Web.UI.WebControls.Calendar myCalendar;

    public String BackColor ="white";
    public String ForeColor ="black";

    public string CurrentDate {
    get { return txtDate.Text; }
    set { txtDate.Text = value; }
    }

    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    txtDate.Text = CurrentDate;
    }


    public void CheckDay(Object Source, DayRenderEventArgs E) {
    //snipped
    }

    public void HandleCalendar(Object sender, EventArgs e) {
    myCalendar.Visible = !myCalendar.Visible;
    if (myCalendar.Visible)
    lnkShowCalendar.Text = " 5 ";
    else
    lnkShowCalendar.Text = " 6 ";
    }

    public void SelectionChanged(Object sender, EventArgs e) {
    txtDate.Text = myCalendar.SelectedDate.ToShortDateString();
    CurrentDate = myCalendar.SelectedDate.ToShortDateString();
    myCalendar.Visible = !myCalendar.Visible;
    lnkShowCalendar.Text = " 6 ";
    }

    }
    Iain, Nov 17, 2003
    #1
    1. Advertising

  2. Iain

    Chris Bower Guest

    You could make an event in your usercontrol that you fire whenever the date
    changes, then consume the event in your parent form. When the event fires
    just update your label.

    "Iain" <> wrote in message
    news:uPcd%...
    > Hi
    >
    > I have page (testCal.aspx) that contains a usercontrol

    (custCalendar.ascx) -
    > see below signature for code. The UC contains a linkbutton which which

    when
    > clicked, posts back and displays a calendar. When a value in the calendar

    is
    > selected it posts back and then updates a public property (CurrentDate) on
    > the UC. Note that 2 postbacks occur.
    >
    > I have been attempting to access the public property on the UC, from the
    > page (testCal) shown below. The problem is that the label on the page, set
    > to CurrentDate on Page_Load, only changes when the LinkButton is clicked

    to
    > change the date for a second time. ie. On a further postback.
    >
    > Thanks in advance.
    >
    > -Iain
    >
    >
    >
    > public class testCal : System.Web.UI.Page
    > {
    > protected System.Web.UI.WebControls.Label lbl;
    > protected custCalendar myCal;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > Response.Write("<br>Source = " + Request.Form["__eventtarget"] +"<br>");
    > lbl.Text = CurrentDate;
    > }
    > }
    >
    >
    >
    > public abstract class custCalendar : System.Web.UI.UserControl
    > {
    > protected System.Web.UI.WebControls.TextBox txtDate;
    > protected System.Web.UI.WebControls.LinkButton lnkShowCalendar;
    > protected System.Web.UI.WebControls.Calendar myCalendar;
    >
    > public String BackColor ="white";
    > public String ForeColor ="black";
    >
    > public string CurrentDate {
    > get { return txtDate.Text; }
    > set { txtDate.Text = value; }
    > }
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > // Put user code to initialize the page here
    > txtDate.Text = CurrentDate;
    > }
    >
    >
    > public void CheckDay(Object Source, DayRenderEventArgs E) {
    > //snipped
    > }
    >
    > public void HandleCalendar(Object sender, EventArgs e) {
    > myCalendar.Visible = !myCalendar.Visible;
    > if (myCalendar.Visible)
    > lnkShowCalendar.Text = " 5 ";
    > else
    > lnkShowCalendar.Text = " 6 ";
    > }
    >
    > public void SelectionChanged(Object sender, EventArgs e) {
    > txtDate.Text = myCalendar.SelectedDate.ToShortDateString();
    > CurrentDate = myCalendar.SelectedDate.ToShortDateString();
    > myCalendar.Visible = !myCalendar.Visible;
    > lnkShowCalendar.Text = " 6 ";
    > }
    >
    > }
    >
    >
    Chris Bower, Nov 17, 2003
    #2
    1. Advertising

  3. Iain

    Iain Guest

    Thanks for your reply.

    Could you perhaps provide a sample that is slightly more specific. I'm new
    to ASP.NET events...

    How do I consume the event in the UC from the WebForm?

    "Chris Bower" <> wrote in message
    news:%...
    > You could make an event in your usercontrol that you fire whenever the

    date
    > changes, then consume the event in your parent form. When the event fires
    > just update your label.
    >
    > "Iain" <> wrote in message
    > news:uPcd%...
    > > Hi
    > >
    > > I have page (testCal.aspx) that contains a usercontrol

    > (custCalendar.ascx) -
    > > see below signature for code. The UC contains a linkbutton which which

    > when
    > > clicked, posts back and displays a calendar. When a value in the

    calendar
    > is
    > > selected it posts back and then updates a public property (CurrentDate)

    on
    > > the UC. Note that 2 postbacks occur.
    > >
    > > I have been attempting to access the public property on the UC, from the
    > > page (testCal) shown below. The problem is that the label on the page,

    set
    > > to CurrentDate on Page_Load, only changes when the LinkButton is clicked

    > to
    > > change the date for a second time. ie. On a further postback.
    > >
    > > Thanks in advance.
    > >
    > > -Iain
    > >
    > >
    > >
    > > public class testCal : System.Web.UI.Page
    > > {
    > > protected System.Web.UI.WebControls.Label lbl;
    > > protected custCalendar myCal;
    > >
    > > private void Page_Load(object sender, System.EventArgs e)
    > > {
    > > Response.Write("<br>Source = " + Request.Form["__eventtarget"] +"<br>");
    > > lbl.Text = CurrentDate;
    > > }
    > > }
    > >
    > >
    > >
    > > public abstract class custCalendar : System.Web.UI.UserControl
    > > {
    > > protected System.Web.UI.WebControls.TextBox txtDate;
    > > protected System.Web.UI.WebControls.LinkButton lnkShowCalendar;
    > > protected System.Web.UI.WebControls.Calendar myCalendar;
    > >
    > > public String BackColor ="white";
    > > public String ForeColor ="black";
    > >
    > > public string CurrentDate {
    > > get { return txtDate.Text; }
    > > set { txtDate.Text = value; }
    > > }
    > >
    > > private void Page_Load(object sender, System.EventArgs e)
    > > {
    > > // Put user code to initialize the page here
    > > txtDate.Text = CurrentDate;
    > > }
    > >
    > >
    > > public void CheckDay(Object Source, DayRenderEventArgs E) {
    > > //snipped
    > > }
    > >
    > > public void HandleCalendar(Object sender, EventArgs e) {
    > > myCalendar.Visible = !myCalendar.Visible;
    > > if (myCalendar.Visible)
    > > lnkShowCalendar.Text = " 5 ";
    > > else
    > > lnkShowCalendar.Text = " 6 ";
    > > }
    > >
    > > public void SelectionChanged(Object sender, EventArgs e) {
    > > txtDate.Text = myCalendar.SelectedDate.ToShortDateString();
    > > CurrentDate = myCalendar.SelectedDate.ToShortDateString();
    > > myCalendar.Visible = !myCalendar.Visible;
    > > lnkShowCalendar.Text = " 6 ";
    > > }
    > >
    > > }
    > >
    > >

    >
    >
    Iain, Nov 17, 2003
    #3
  4. Iain

    Chris Bower Guest

    Here's an example in VB that should work for ya.

    'Codebehind in custCalendar.ascx
    Public Class custCalendar
    Inherits UserControl

    Public Event DateChanged(sender as Object, e as EventArgs) 'Event
    declaration
    Protected WithEvents myCalendar as Calendar

    Public ReadOnly Property SelectedDate As Date
    Return myCalendar.SelectedDate()
    End Property

    Private Sub myCalendar_SelectionChanged(sender as Object, e as
    EventArgs) Handles myCalendar.SelectionChanged
    RaiseEvent DateChanged(sender, e) 'The Calendar's selection changed,
    raise the DateChanged event
    End Sub

    End Class


    'Codebehind in testCal.aspx
    Public Class testCal
    Inherits Page

    Protected WithEvents myLabel as Label
    Protected WithEvents mycustCalendar as custCalendar

    'Consume the DateChanged event of our custCalendar instance
    Private Sub mycustCalendar_DateChanged(sender as Object, e as EventArgs)
    Handles mycustCalendar.DateChanged
    myLabel.Text = mycustCalendar.SelectedDate.ToString()
    End Sub
    End Class

    "Iain" <> wrote in message
    news:...
    > Thanks for your reply.
    >
    > Could you perhaps provide a sample that is slightly more specific. I'm

    new
    > to ASP.NET events...
    >
    > How do I consume the event in the UC from the WebForm?
    >
    > "Chris Bower" <> wrote in message
    > news:%...
    > > You could make an event in your usercontrol that you fire whenever the

    > date
    > > changes, then consume the event in your parent form. When the event

    fires
    > > just update your label.
    > >
    > > "Iain" <> wrote in message
    > > news:uPcd%...
    > > > Hi
    > > >
    > > > I have page (testCal.aspx) that contains a usercontrol

    > > (custCalendar.ascx) -
    > > > see below signature for code. The UC contains a linkbutton which which

    > > when
    > > > clicked, posts back and displays a calendar. When a value in the

    > calendar
    > > is
    > > > selected it posts back and then updates a public property

    (CurrentDate)
    > on
    > > > the UC. Note that 2 postbacks occur.
    > > >
    > > > I have been attempting to access the public property on the UC, from

    the
    > > > page (testCal) shown below. The problem is that the label on the page,

    > set
    > > > to CurrentDate on Page_Load, only changes when the LinkButton is

    clicked
    > > to
    > > > change the date for a second time. ie. On a further postback.
    > > >
    > > > Thanks in advance.
    > > >
    > > > -Iain
    > > >
    > > >
    > > >
    > > > public class testCal : System.Web.UI.Page
    > > > {
    > > > protected System.Web.UI.WebControls.Label lbl;
    > > > protected custCalendar myCal;
    > > >
    > > > private void Page_Load(object sender, System.EventArgs e)
    > > > {
    > > > Response.Write("<br>Source = " + Request.Form["__eventtarget"]

    +"<br>");
    > > > lbl.Text = CurrentDate;
    > > > }
    > > > }
    > > >
    > > >
    > > >
    > > > public abstract class custCalendar : System.Web.UI.UserControl
    > > > {
    > > > protected System.Web.UI.WebControls.TextBox txtDate;
    > > > protected System.Web.UI.WebControls.LinkButton lnkShowCalendar;
    > > > protected System.Web.UI.WebControls.Calendar myCalendar;
    > > >
    > > > public String BackColor ="white";
    > > > public String ForeColor ="black";
    > > >
    > > > public string CurrentDate {
    > > > get { return txtDate.Text; }
    > > > set { txtDate.Text = value; }
    > > > }
    > > >
    > > > private void Page_Load(object sender, System.EventArgs e)
    > > > {
    > > > // Put user code to initialize the page here
    > > > txtDate.Text = CurrentDate;
    > > > }
    > > >
    > > >
    > > > public void CheckDay(Object Source, DayRenderEventArgs E) {
    > > > //snipped
    > > > }
    > > >
    > > > public void HandleCalendar(Object sender, EventArgs e) {
    > > > myCalendar.Visible = !myCalendar.Visible;
    > > > if (myCalendar.Visible)
    > > > lnkShowCalendar.Text = " 5 ";
    > > > else
    > > > lnkShowCalendar.Text = " 6 ";
    > > > }
    > > >
    > > > public void SelectionChanged(Object sender, EventArgs e) {
    > > > txtDate.Text = myCalendar.SelectedDate.ToShortDateString();
    > > > CurrentDate = myCalendar.SelectedDate.ToShortDateString();
    > > > myCalendar.Visible = !myCalendar.Visible;
    > > > lnkShowCalendar.Text = " 6 ";
    > > > }
    > > >
    > > > }
    > > >
    > > >

    > >
    > >

    >
    >
    Chris Bower, Nov 18, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phl
    Replies:
    2
    Views:
    504
    =?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=
    Nov 18, 2004
  2. Fabiano
    Replies:
    2
    Views:
    574
    =?Utf-8?B?c3Jpbmk=?=
    Jun 3, 2005
  3. tdavisjr
    Replies:
    3
    Views:
    2,767
    =?Utf-8?B?S2V2Lk5FVA==?=
    Jan 3, 2006
  4. protic
    Replies:
    0
    Views:
    338
    protic
    Oct 15, 2008
  5. Nathan Sokalski
    Replies:
    25
    Views:
    1,391
    Nathan Sokalski
    Mar 4, 2010
Loading...

Share This Page