473,396 Members | 1,755 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,396 software developers and data experts.

How to send values to a custom control?

HI all,

I'm writing a control that resides on a page which instantiates an object
from the database.
My goal is to make the control use the same object for the control as on the
page.
Something like this:

Page code behind (C#):
private void Page_Load(object sender, System.EventArgs e)
{
int objId = Request.QueryString("id");
mytype obj = getobjfromDb(objid);
MyControl theControl = new MyContro(obj);
}

Control code:

public class MyControl: WebControl, INamingContainer
{
public MyControl(mytype myObject)
{
myobj = myObject;
}

protected override void OnLoad(EventArgs e)
{
this.controls.add(new literal(myojbj.ToString());
}

private mytype myobj;
}

I find this difficult to acheive as I can't really get a grasp of the event
firing order.
* Where can I call the contructor of the control?
* How will the events find their way back to my control?
* Is the implementation of controls described in depth some where?
(Something more than
http://msdn.microsoft.com/library/de...nLifecycle.asp)
* Is the process the page framwork goes through when processign a request
discussed somewhere?

cheers,
mortb
Nov 18 '05 #1
3 1544
Hi,

Don't use constructor in the control.

-------Control.cs---------
....
public event EventHandler OnClickRender;
protected void Click_RenderReport(object sender, EventArgs e)
{
if(OnClickRender != null)
OnClickRender(this, new EventArgs());
}
....
-------End Control.cs---------

-------Page.cs---------

protected void RenderReport(object sender, EventArgs e)
{
...
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
ReportOptions1.OnClickRender += new EventHandler(RenderReport);
}
-------End Page.cs---------

This sample shows you how your page can get event from the your control.
The page has the "RenderReport" method. And control has a button that has
"Click_RenderReport" event. You have to create public event in the your
control.
The "Click_RenderReport" method compare the "OnClickRender" event. Then you
have to add new method to the your page. For example: "RenderReport"
And the "OnInit" method must have line like this:
ReportOptions1.OnClickRender += new EventHandler(RenderReport);
If you rememeber, you created the "OnClickRender " public event. When you
press on the button in the control at that time your page can catch this
event. If you want to pass your datas to your control, you have to create a
public property. This is an example; At that time you can pass your
parameter to the control.

control.Email = "bla-bal";

public string Email
{
get
{
return txtEmail.Text.Trim();
}
set
{
txtEmail.Text = value;
}
}
bye-bye

"mortb" <mo****@hotmail.com> wrote in message
news:eq**************@TK2MSFTNGP12.phx.gbl...
HI all,

I'm writing a control that resides on a page which instantiates an object
from the database.
My goal is to make the control use the same object for the control as on the page.
Something like this:

Page code behind (C#):
private void Page_Load(object sender, System.EventArgs e)
{
int objId = Request.QueryString("id");
mytype obj = getobjfromDb(objid);
MyControl theControl = new MyContro(obj);
}

Control code:

public class MyControl: WebControl, INamingContainer
{
public MyControl(mytype myObject)
{
myobj = myObject;
}

protected override void OnLoad(EventArgs e)
{
this.controls.add(new literal(myojbj.ToString());
}

private mytype myobj;
}

I find this difficult to acheive as I can't really get a grasp of the event firing order.
* Where can I call the contructor of the control?
* How will the events find their way back to my control?
* Is the implementation of controls described in depth some where?
(Something more than
http://msdn.microsoft.com/library/de...nLifecycle.asp) * Is the process the page framwork goes through when processign a request
discussed somewhere?

cheers,
mortb

Nov 18 '05 #2
Hi,

You have to load controls using Page.LoadControl(virtualPathToControl)
rather than creating an instance manually. I recommend you create a
few properties on the user control and then set its properties. For
example,
// your user control class
public class MyUserControl: UserControl
{
....
public object MyDatabaseObject
{
get
{
dbObj=value;
}
}
}

// load and set properties

MyUserControl ctrl = Page.LoadControl("path") as MyUserControl;
ctrl.MyDatabaseObject=xyz;

sayed

"mortb" <mo****@hotmail.com> wrote in message news:<eq**************@TK2MSFTNGP12.phx.gbl>...
HI all,

I'm writing a control that resides on a page which instantiates an object
from the database.
My goal is to make the control use the same object for the control as on the
page.
Something like this:

Page code behind (C#):
private void Page_Load(object sender, System.EventArgs e)
{
int objId = Request.QueryString("id");
mytype obj = getobjfromDb(objid);
MyControl theControl = new MyContro(obj);
}

Control code:

public class MyControl: WebControl, INamingContainer
{
public MyControl(mytype myObject)
{
myobj = myObject;
}

protected override void OnLoad(EventArgs e)
{
this.controls.add(new literal(myojbj.ToString());
}

private mytype myobj;
}

I find this difficult to acheive as I can't really get a grasp of the event
firing order.
* Where can I call the contructor of the control?
* How will the events find their way back to my control?
* Is the implementation of controls described in depth some where?
(Something more than
http://msdn.microsoft.com/library/de...nLifecycle.asp)
* Is the process the page framwork goes through when processign a request
discussed somewhere?

cheers,
mortb

Nov 18 '05 #3
Thanks!

Thanks!

Why can't I use the contrutor?

/mortb

"Michael Tkachev" <m_*******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi,

Don't use constructor in the control.

-------Control.cs---------
...
public event EventHandler OnClickRender;
protected void Click_RenderReport(object sender, EventArgs e)
{
if(OnClickRender != null)
OnClickRender(this, new EventArgs());
}
...
-------End Control.cs---------

-------Page.cs---------

protected void RenderReport(object sender, EventArgs e)
{
...
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
ReportOptions1.OnClickRender += new EventHandler(RenderReport);
}
-------End Page.cs---------

This sample shows you how your page can get event from the your control.
The page has the "RenderReport" method. And control has a button that has
"Click_RenderReport" event. You have to create public event in the your
control.
The "Click_RenderReport" method compare the "OnClickRender" event. Then you have to add new method to the your page. For example: "RenderReport"
And the "OnInit" method must have line like this:
ReportOptions1.OnClickRender += new EventHandler(RenderReport);
If you rememeber, you created the "OnClickRender " public event. When you
press on the button in the control at that time your page can catch this
event. If you want to pass your datas to your control, you have to create a public property. This is an example; At that time you can pass your
parameter to the control.

control.Email = "bla-bal";

public string Email
{
get
{
return txtEmail.Text.Trim();
}
set
{
txtEmail.Text = value;
}
}
bye-bye

"mortb" <mo****@hotmail.com> wrote in message
news:eq**************@TK2MSFTNGP12.phx.gbl...
HI all,

I'm writing a control that resides on a page which instantiates an object from the database.
My goal is to make the control use the same object for the control as on

the
page.
Something like this:

Page code behind (C#):
private void Page_Load(object sender, System.EventArgs e)
{
int objId = Request.QueryString("id");
mytype obj = getobjfromDb(objid);
MyControl theControl = new MyContro(obj);
}

Control code:

public class MyControl: WebControl, INamingContainer
{
public MyControl(mytype myObject)
{
myobj = myObject;
}

protected override void OnLoad(EventArgs e)
{
this.controls.add(new literal(myojbj.ToString());
}

private mytype myobj;
}

I find this difficult to acheive as I can't really get a grasp of the

event
firing order.
* Where can I call the contructor of the control?
* How will the events find their way back to my control?
* Is the implementation of controls described in depth some where?
(Something more than

http://msdn.microsoft.com/library/de...nLifecycle.asp)
* Is the process the page framwork goes through when processign a request discussed somewhere?

cheers,
mortb


Nov 18 '05 #4

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

Similar topics

0
by: adam | last post by:
i have custom user control and i'm trying to pass values to custom user control......I need help it seems to me i cannot pass the value to user control from dropdownlist. I have property in a...
9
by: eswanson | last post by:
I have a web page I need to post a file plus some other fields to it. How can I do this from a asp.net page. I know I can send individual fields to the other page, but how do I send a file to the...
9
by: james.e.coleman | last post by:
Hello, I have created a custom dropdownlist that is used multiple times within a single page. When trying to set the values of the controls with the page in which they are being used, they all...
2
by: Steve Franks | last post by:
In ASP.NET 2.0 you can now apparently do this: <asp:label runat="server" text="some browser" IE:text="any IE browser" IE5:text="the IE 5 browser" PIE:text="the Pocket PC browser" /> Now the...
3
by: Alan | last post by:
Hi, is it possible to create a project that can offer different members of a Public Enum according to a condition ? I mean, Project A has a public enum. Project B & C access A's enum. However B...
2
by: Pipo | last post by:
Nobody knows how to get the values provided in the client can be read in the user-control? If have made a Web Custom Control with 2 textboxes and 1 dropdownlist. The user fills in my control (the...
0
by: Kay | last post by:
Hello, I have developed a web custom control, I want one of the properties of the control to appear as a dropdown list so the user can select from the list. My question is: how do I define an...
0
by: Kay | last post by:
Hello, I have developed a web custom control, I want one of the properties of the control to appear as a dropdown list so the user can select from the list. My question is: how do I define an...
4
by: Jon Paal | last post by:
I have a custom server control which renders some html. I want to capture some interim values generated by the control as it processes the data. is there a way to capture and retrieve the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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,...

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.