473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Accessing UserControl property value from page control

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

}
Nov 18 '05 #1
3 1671
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" <i@news.msoft.com> wrote in message
news:uP****************@tk2msftngp13.phx.gbl...
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 ";
}

}

Nov 18 '05 #2
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" <ch***********@SPAMunionfedbank.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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" <i@news.msoft.com> wrote in message
news:uP****************@tk2msftngp13.phx.gbl...
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 ";
}

}


Nov 18 '05 #3
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" <b@news.msoft.net> wrote in message
news:OP**************@TK2MSFTNGP12.phx.gbl...
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" <ch***********@SPAMunionfedbank.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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" <i@news.msoft.com> wrote in message
news:uP****************@tk2msftngp13.phx.gbl...
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 ";
}

}



Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: bill yeager | last post by:
When trying to run my web project, I get the following error: Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the...
4
by: Mat | last post by:
Hi, I am having a bit of a problem trying to access / set properties of a usercontrol from the parent codebehind. This is what I have so far.. simple.aspx <%@ Page...
5
by: Dan Nash | last post by:
Hi all, I've got a page with a user control on, added via VS. I'm trying to get to a property of the user control (or more precisely, a public var). Here's the code at the top of my aspx...
3
by: Craig G | last post by:
i have a user control which is basically a datagrid, which has add/edit/delete buttons on the grid is there anyway of accessing the actual datagrid from the form itself? basically i want to...
1
by: Will Gillen | last post by:
I know this has probably been asked before, but I can't seem to find a solid answer in any of the archives. First, before my question, please forgive my limited knowledge of the event lifecycle...
3
by: Jeff | last post by:
Hey ASP.NET 2.0 This the ObjectDataSource in my UserControl, <asp:ObjectDataSource ID="odsMessage" runat="server" SelectMethod="ExecuteMessage" TypeName="AH.MyNetwork.BLL.Network.Message">...
10
by: Benton | last post by:
Hi there, I have a UserControl with a couple of textboxes and a couple of buttons ("Save" and "Cancel"). The Click event for this buttons is in the UserControl's codebehind of course, so here's...
9
by: J055 | last post by:
Hi I have a standard asp page which uses a MasterPage. The MasterPage contains a User control. How can I access a public method in the User control from my WebForm page? I can't move the method...
4
by: =?Utf-8?B?R3JlZyBTdGV2ZW5z?= | last post by:
I have an ASP.NET page with a form that contains two user controls: <%@ Register TagPrefix="x" TagName="c1" Src="ctl1.ascx" %> <%@ Register TagPrefix="x" TagName="c2" Src="ctl2.ascx" %> <body>...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.