How to send values to a custom control?

M

mortb

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/d...guide/html/cpconControlExecutionLifecycle.asp)
* Is the process the page framwork goes through when processign a request
discussed somewhere?

cheers,
mortb
 
M

Michael Tkachev

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
 
S

Sayed Hashimi

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,156
Latest member
KetoBurnSupplement
Top