Accessing UserControl property value from page control

I

Iain

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 ";
}

}
 
C

Chris Bower

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.
 
I

Iain

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 said:
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 said:
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 ";
}

}
 
C

Chris Bower

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 said:
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 said:
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.

calendar
(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"]
+ said:
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 ";
}

}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top