By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,825 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,825 IT Pros & Developers. It's quick & easy.

Dynamic Controls and Page_Init

P: n/a

Hello again to all of you geniuses,

I'm having a problem trying to load dynamic controls at the initialization phase.

I've read the docs, and I thought I had it figured out:
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
.NET Framework Class Library

Control.Init Event [C#]

C#
public event EventHandler Init;

Event Data
The event handler receives an argument of type EventArgs.

Remarks
Server controls should perform any initialization steps that are required
to create and set up an instance. You cannot use view-state information
within this event; it is not populated yet. You should not access another
server control during this event, regardless of whether it is a child or
parent to this control. Other server controls are not certain to be created
and ready for access.

Example
[C#] The following example assigns a custom event handler, Control_Init,
to the Init event of a control, myControl, when the Page_Init method is
called on the page that contains the control.

[C#]
void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for 'Init'.
myControl.Init += new System.EventHandler(Control_Init);
}

void Control_Init(object sender,EventArgs e)
{
Response.Write("The ID of the object initially : " + myControl.ID);
// Change the ID property.
myControl.ID="TestControl";
Response.Write("<br>The changed ID : " + myControl.ID);
}

2001-2002 Microsoft Corporation. All rights reserved.

<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>

One thing I don't understand is: Where did "myControl" come from in the
given example?

In my own effort, I tried to attach a literal control to a placeholder:
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>

void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for 'Init'.
// myControl.Init += new System.EventHandler(Control_Init);
PlaceHolder plc_hldr = (PlaceHolder) FindControl ("Dyn_Control_Placeholder");
plc_hldr.Init += new System.EventHandler (plc_hldr_Control_Init);
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void plc_hldr_Control_Init (object sender,EventArgs e)
{
Label lbl_msg01 = (Label) FindControl ("Message01");

Dyn_Control_Placeholder.Controls.Add
( new LiteralControl("<br>added 1 on init<br>") );
PlaceHolder plc_hldr = (PlaceHolder) FindControl ("Dyn_Control_Placeholder");

lbl_msg01.Text = "The ID of the object initially : " + plc_hldr.ID;
Response.Write("The ID of the object initially : " + plc_hldr.ID);

plc_hldr.Controls.Add ( new LiteralControl("<br>added 2 on init<br>") );
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>

... and nothing happened - the literal control was not there.

I'm hoping that someone smarter than me can clue me in on this
- I would be most grateful.

My complete code is as follows:
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

<script runat="server">

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- */
public class sys_obj_class
{
private ControlCollection pram_ph01_ctrl;
public ControlCollection Get_pram_ph01_ctrl
{ get { return pram_ph01_ctrl; } }

private Page this_page01;
public Page Get_this_page01
{ get { return this_page01; } }

private PlaceHolder Obj_Dyn_Ctrl_Placeholder;
public PlaceHolder Get_Obj_Dyn_Ctrl_Placeholder
{ get { return Obj_Dyn_Ctrl_Placeholder; } }

private Label Obj_Dyn_Label01;
public Label Get_Obj_Dyn_Label01
{ get { return Obj_Dyn_Label01; } }
public sys_obj_class
(PlaceHolder Dyn_Ctrl_Placeholder, Label Dyn_Label)
{ pram_ph01_ctrl = Dyn_Ctrl_Placeholder.Controls;
this_page01 = Dyn_Ctrl_Placeholder.Page;
Obj_Dyn_Ctrl_Placeholder = Dyn_Ctrl_Placeholder;
Obj_Dyn_Label01 = Dyn_Label;
}
}

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for 'Init'.
// myControl.Init += new System.EventHandler(Control_Init);
PlaceHolder plc_hldr = (PlaceHolder) FindControl ("Dyn_Control_Placeholder");
plc_hldr.Init += new System.EventHandler(plc_hldr_Control_Init);
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void plc_hldr_Control_Init (object sender,EventArgs e)
{
Label lbl_msg01 = (Label) FindControl ("Message01");
PlaceHolder plc_hldr = (PlaceHolder) FindControl ("Dyn_Control_Placeholder");

lbl_msg01.Text = "The ID of the object initially : " + plc_hldr.ID;
Response.Write("The ID of the object initially : " + plc_hldr.ID);

plc_hldr.Controls.Add ( new LiteralControl("<br>added on init<br>") );
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
protected void Page_Load (Object sender, EventArgs e)
{
PlaceHolder plc_hldr = (PlaceHolder) FindControl ("Dyn_Control_Placeholder");
Label lbl_msg01 = (Label) FindControl ("Message01");
sys_obj_class sys_obj = new sys_obj_class (plc_hldr, lbl_msg01);

ctrl_pair_creation_01 ccp1 = new ctrl_pair_creation_01 (sys_obj);

ccp1.create_ctrl_pair_01 ();
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
protected class base_create_ctrl_pair
{
protected TextBox UserTextBox0x;
protected Button button0x;
protected ControlCollection ph01_ctrl;
protected PlaceHolder base_Dyn_Ctrl_Placeholder;
protected Label Dyn_Lbl_msg01;

// constructor
protected base_create_ctrl_pair (sys_obj_class sys_obj)
{ ph01_ctrl = sys_obj.Get_pram_ph01_ctrl;
base_Dyn_Ctrl_Placeholder = sys_obj.Get_Obj_Dyn_Ctrl_Placeholder;
Dyn_Lbl_msg01 = sys_obj.Get_Obj_Dyn_Label01;
}

protected TextBox create_text_box_control (String text_box_id)
{ // Create UserTextBox TextBox control.
TextBox new_TextBox = new TextBox ();

// Configure the UserTextBox TextBox control.
new_TextBox.ID = text_box_id;

// Add UserTextBox TextBox control to the Controls collection
// of the Dyn_Control_Placeholder PlaceHolder control.
ph01_ctrl.Add (new_TextBox);

return new_TextBox;
}

/* --------------------------------------------------------- *
Example: The following example creates a Button,
sets its DialogResult property to DialogResult.OK,
and adds it to a Form.
* --------------------------------------------------------- */
protected void InitializeMyButton (String Btn_ID, String Btn_text)
{ // Create and initialize a Button.
button0x = new Button();
button0x.ID = Btn_ID;
button0x.Text = Btn_text;

ph01_ctrl.Add ( new LiteralControl("<br><br>") );
ph01_ctrl.Add (button0x);
}

protected void init_single_line_text_box (TextBox new_TextBox, int col_sz)
{ new_TextBox.Columns = col_sz; }

protected void create_ctrl_pair_0x
(String TB_ID, String DB_ID, String DB_txt)
{ // Create UserTextBox TextBox control.
UserTextBox0x = create_text_box_control (TB_ID);
InitializeMyButton(DB_ID, DB_txt);
}

/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- */
private String submit_click_str01 = "The TextBox";

private String submit_click_str02
= " control above is dynamically generated. <br> You entered: ";
public void Submit_Click (Object sender, EventArgs e)
{ // Retrieve the UserTextBox TextBox control from the
Dyn_Control_Placeholder
// PlaceHolder control.
TextBox TempTextBox = (TextBox)
base_Dyn_Ctrl_Placeholder.FindControl("UserTextBox 1");

// Display the Text property.
Dyn_Lbl_msg01.Text = submit_click_str01 + '1' + submit_click_str02 +
TempTextBox.Text;
}
/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- */

}
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
protected class ctrl_pair_creation_01 : base_create_ctrl_pair
{
private sys_obj_class lcl_sys_obj;

// constructor
public ctrl_pair_creation_01 (sys_obj_class sys_obj) : base (sys_obj)
{ lcl_sys_obj = sys_obj; }

/* . . . . . . . . . . . . . */
public void create_ctrl_pair_01 ()
{ create_ctrl_pair_0x
("UserTextBox1", "DButton01", "Submit 01");

button0x.Click += new EventHandler (Submit_Click);

if (!lcl_sys_obj.Get_this_page01.IsPostBack)
init_single_line_text_box (UserTextBox0x, 22);

ph01_ctrl.Add ( new LiteralControl("<br><br>") );
}
/* . . . . . . . . . . . . . */
}
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */

</script>
</head>

<body>

<form runat="server">

<h3> Control Init </h3>

Enter some text and click the Submit button. <br><br>

<asp:PlaceHolder ID="Dyn_Control_Placeholder" runat="server"/>

<asp:Label ID="Message01" runat="server"/>

</form>

</body>
</html>

<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
- wASP
Nov 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
This is an example of dynamically adding an event handler to a control. They
are assuming the control is already there.

"wASP" <wylbur[at]ev1[dot]net> wrote in message
news:r6********************************@4ax.com...

Hello again to all of you geniuses,

I'm having a problem trying to load dynamic controls at the initialization
phase.

I've read the docs, and I thought I had it figured out:
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
.NET Framework Class Library

Control.Init Event [C#]

C#
public event EventHandler Init;

Event Data
The event handler receives an argument of type EventArgs.

Remarks
Server controls should perform any initialization steps that are required
to create and set up an instance. You cannot use view-state information
within this event; it is not populated yet. You should not access another
server control during this event, regardless of whether it is a child or
parent to this control. Other server controls are not certain to be
created
and ready for access.

Example
[C#] The following example assigns a custom event handler, Control_Init,
to the Init event of a control, myControl, when the Page_Init method is
called on the page that contains the control.

[C#]
void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for 'Init'.
myControl.Init += new System.EventHandler(Control_Init);
}

void Control_Init(object sender,EventArgs e)
{
Response.Write("The ID of the object initially : " + myControl.ID);
// Change the ID property.
myControl.ID="TestControl";
Response.Write("<br>The changed ID : " + myControl.ID);
}

2001-2002 Microsoft Corporation. All rights reserved.

<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>

One thing I don't understand is: Where did "myControl" come from in the
given example?

In my own effort, I tried to attach a literal control to a placeholder:
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>

void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for 'Init'.
// myControl.Init += new System.EventHandler(Control_Init);
PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");
plc_hldr.Init += new System.EventHandler (plc_hldr_Control_Init);
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void plc_hldr_Control_Init (object sender,EventArgs e)
{
Label lbl_msg01 = (Label) FindControl ("Message01");

Dyn_Control_Placeholder.Controls.Add
( new LiteralControl("<br>added 1 on init<br>") );
PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");

lbl_msg01.Text = "The ID of the object initially : " + plc_hldr.ID;
Response.Write("The ID of the object initially : " + plc_hldr.ID);

plc_hldr.Controls.Add ( new LiteralControl("<br>added 2 on
init<br>") );
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>

... and nothing happened - the literal control was not there.

I'm hoping that someone smarter than me can clue me in on this
- I would be most grateful.

My complete code is as follows:
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

<script runat="server">

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
*/
public class sys_obj_class
{
private ControlCollection pram_ph01_ctrl;
public ControlCollection Get_pram_ph01_ctrl
{ get { return pram_ph01_ctrl; } }

private Page this_page01;
public Page Get_this_page01
{ get { return this_page01; } }

private PlaceHolder Obj_Dyn_Ctrl_Placeholder;
public PlaceHolder Get_Obj_Dyn_Ctrl_Placeholder
{ get { return Obj_Dyn_Ctrl_Placeholder; } }

private Label Obj_Dyn_Label01;
public Label Get_Obj_Dyn_Label01
{ get { return Obj_Dyn_Label01; } }
public sys_obj_class
(PlaceHolder Dyn_Ctrl_Placeholder, Label Dyn_Label)
{ pram_ph01_ctrl = Dyn_Ctrl_Placeholder.Controls;
this_page01 = Dyn_Ctrl_Placeholder.Page;
Obj_Dyn_Ctrl_Placeholder = Dyn_Ctrl_Placeholder;
Obj_Dyn_Label01 = Dyn_Label;
}
}

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for 'Init'.
// myControl.Init += new System.EventHandler(Control_Init);
PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");
plc_hldr.Init += new System.EventHandler(plc_hldr_Control_Init);
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void plc_hldr_Control_Init (object sender,EventArgs e)
{
Label lbl_msg01 = (Label) FindControl ("Message01");
PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");

lbl_msg01.Text = "The ID of the object initially : " + plc_hldr.ID;
Response.Write("The ID of the object initially : " + plc_hldr.ID);

plc_hldr.Controls.Add ( new LiteralControl("<br>added on init<br>") );
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
protected void Page_Load (Object sender, EventArgs e)
{
PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");
Label lbl_msg01 = (Label) FindControl ("Message01");
sys_obj_class sys_obj = new sys_obj_class (plc_hldr, lbl_msg01);

ctrl_pair_creation_01 ccp1 = new ctrl_pair_creation_01 (sys_obj);

ccp1.create_ctrl_pair_01 ();
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
protected class base_create_ctrl_pair
{
protected TextBox UserTextBox0x;
protected Button button0x;
protected ControlCollection ph01_ctrl;
protected PlaceHolder base_Dyn_Ctrl_Placeholder;
protected Label Dyn_Lbl_msg01;

// constructor
protected base_create_ctrl_pair (sys_obj_class sys_obj)
{ ph01_ctrl = sys_obj.Get_pram_ph01_ctrl;
base_Dyn_Ctrl_Placeholder =
sys_obj.Get_Obj_Dyn_Ctrl_Placeholder;
Dyn_Lbl_msg01 = sys_obj.Get_Obj_Dyn_Label01;
}

protected TextBox create_text_box_control (String text_box_id)
{ // Create UserTextBox TextBox control.
TextBox new_TextBox = new TextBox ();

// Configure the UserTextBox TextBox control.
new_TextBox.ID = text_box_id;

// Add UserTextBox TextBox control to the Controls collection
// of the Dyn_Control_Placeholder PlaceHolder control.
ph01_ctrl.Add (new_TextBox);

return new_TextBox;
}

/* --------------------------------------------------------- *
Example: The following example creates a Button,
sets its DialogResult property to DialogResult.OK,
and adds it to a Form.
* --------------------------------------------------------- */
protected void InitializeMyButton (String Btn_ID, String
Btn_text)
{ // Create and initialize a Button.
button0x = new Button();
button0x.ID = Btn_ID;
button0x.Text = Btn_text;

ph01_ctrl.Add ( new LiteralControl("<br><br>") );
ph01_ctrl.Add (button0x);
}

protected void init_single_line_text_box (TextBox new_TextBox,
int col_sz)
{ new_TextBox.Columns = col_sz; }

protected void create_ctrl_pair_0x
(String TB_ID, String DB_ID, String DB_txt)
{ // Create UserTextBox TextBox control.
UserTextBox0x = create_text_box_control (TB_ID);
InitializeMyButton(DB_ID, DB_txt);
}
/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
*/
private String submit_click_str01 = "The TextBox";

private String submit_click_str02
= " control above is dynamically generated. <br> You
entered: ";
public void Submit_Click (Object sender, EventArgs e)
{ // Retrieve the UserTextBox TextBox control from the
Dyn_Control_Placeholder
// PlaceHolder control.
TextBox TempTextBox = (TextBox)
base_Dyn_Ctrl_Placeholder.FindControl("UserTextBox 1");

// Display the Text property.
Dyn_Lbl_msg01.Text = submit_click_str01 + '1' +
submit_click_str02 +
TempTextBox.Text;
}

/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
*/

}
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
protected class ctrl_pair_creation_01 : base_create_ctrl_pair
{
private sys_obj_class lcl_sys_obj;

// constructor
public ctrl_pair_creation_01 (sys_obj_class sys_obj) : base
(sys_obj)
{ lcl_sys_obj = sys_obj; }

/* . . . . . . . . . . . . . */
public void create_ctrl_pair_01 ()
{ create_ctrl_pair_0x
("UserTextBox1", "DButton01", "Submit 01");

button0x.Click += new EventHandler (Submit_Click);

if (!lcl_sys_obj.Get_this_page01.IsPostBack)
init_single_line_text_box (UserTextBox0x, 22);

ph01_ctrl.Add ( new LiteralControl("<br><br>") );
}
/* . . . . . . . . . . . . . */
}
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */

</script>
</head>

<body>

<form runat="server">

<h3> Control Init </h3>

Enter some text and click the Submit button. <br><br>

<asp:PlaceHolder ID="Dyn_Control_Placeholder" runat="server"/>

<asp:Label ID="Message01" runat="server"/>

</form>

</body>
</html>

<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
- wASP

Nov 17 '05 #2

P: n/a
On Thu, 28 Jul 2005 14:16:18 -0400, "Marina" <so*****@nospam.com> wrote:
This is an example of dynamically adding an event handler to a control. They
are assuming the control is already there.


Hi Marina,

OK - so I'm trying to add a handler to a control that has yet to be instantiated?

Can you give me a clue as to how would I add a dynamic control
to a placeholder before Page_Load gets fired and the viewstate
is recovered on postback?

... or ...

How could I save the viewstate for a specific control (created dynamically)
then recover it on Page_Load? ... THAT is a more important question.
Do you know of any good docs/books that might spell it out for a fool like myself?

I'm a newbie on this - and I've got a lot to learn.

- wASP
Nov 17 '05 #3

P: n/a
No, the control in this example has been instantiated. It exists, it is
declared in the .aspx page, and by the time Init runs it points to a real
object.

In the Init or Load event, you would declare a variable of the type you
want. You would create an instance of whatever object it is you want, and
then add it to your placeholder. Then you would the handler to this object
you just created. If the page has viewstate on, the control should just
maintain its viewstate. You have to do this on every postback.

"wASP" <wylbur[at]ev1[dot]net> wrote in message
news:i8********************************@4ax.com...
On Thu, 28 Jul 2005 14:16:18 -0400, "Marina" <so*****@nospam.com> wrote:
This is an example of dynamically adding an event handler to a control.
They
are assuming the control is already there.


Hi Marina,

OK - so I'm trying to add a handler to a control that has yet to be
instantiated?

Can you give me a clue as to how would I add a dynamic control
to a placeholder before Page_Load gets fired and the viewstate
is recovered on postback?

... or ...

How could I save the viewstate for a specific control (created
dynamically)
then recover it on Page_Load? ... THAT is a more important question.
Do you know of any good docs/books that might spell it out for a fool like
myself?

I'm a newbie on this - and I've got a lot to learn.

- wASP

Nov 17 '05 #4

P: n/a
On Thu, 28 Jul 2005 15:14:08 -0400, "Marina" <so*****@nospam.com> wrote:
No, the control in this example has been instantiated. It exists, it is
declared in the .aspx page, and by the time Init runs it points to a real
object.
Going back to my original post ...

So then this:

PlaceHolder plc_hldr = (PlaceHolder) FindControl ("Dyn_Control_Placeholder");

... should return a valid reference to the placeholder, right?

If not, then how would I get a valid reference?

In the Init or Load event, you would declare a variable of the type you
want. You would create an instance of whatever object it is you want, and
then add it to your placeholder. Then you would the handler to this object
you just created. If the page has viewstate on, the control should just
maintain its viewstate. You have to do this on every postback.
I thought I did that?

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for 'Init'.
PlaceHolder plc_hldr = (PlaceHolder) FindControl ("Dyn_Control_Placeholder");
plc_hldr.Init += new System.EventHandler (plc_hldr_Control_Init);
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void plc_hldr_Control_Init (object sender,EventArgs e)
{
Dyn_Control_Placeholder.Controls.Add
( new LiteralControl("<br>added 1 on init<br>") );
...
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

I also tried doing this:

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void plc_hldr_Control_Init (object sender,EventArgs e)
{
this.Controls.Add
( new LiteralControl("<br>added 1 on init<br>") );
...
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

... with the same (non)result.
You have to do this on every postback.
I realize this, but I can't even get it to work on the first posting
- never mind postback.

So am I just stupid or som'tin'?

In my own effort, I tried to attach a literal control to a placeholder:
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>

void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for 'Init'.
// myControl.Init += new System.EventHandler(Control_Init);
PlaceHolder plc_hldr = (PlaceHolder) FindControl ("Dyn_Control_Placeholder");
plc_hldr.Init += new System.EventHandler (plc_hldr_Control_Init);
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void plc_hldr_Control_Init (object sender,EventArgs e)
{
Label lbl_msg01 = (Label) FindControl ("Message01");

Dyn_Control_Placeholder.Controls.Add
( new LiteralControl("<br>added 1 on init<br>") );
PlaceHolder plc_hldr = (PlaceHolder) FindControl ("Dyn_Control_Placeholder");

lbl_msg01.Text = "The ID of the object initially : " + plc_hldr.ID;
Response.Write("The ID of the object initially : " + plc_hldr.ID);

plc_hldr.Controls.Add ( new LiteralControl("<br>added 2 on init<br>") );
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>



"wASP" <wylbur[at]ev1[dot]net> wrote in message
news:i8********************************@4ax.com.. .
On Thu, 28 Jul 2005 14:16:18 -0400, "Marina" <so*****@nospam.com> wrote:
This is an example of dynamically adding an event handler to a control.
They
are assuming the control is already there.


Hi Marina,

OK - so I'm trying to add a handler to a control that has yet to be
instantiated?

Can you give me a clue as to how would I add a dynamic control
to a placeholder before Page_Load gets fired and the viewstate
is recovered on postback?

... or ...

How could I save the viewstate for a specific control (created
dynamically)
then recover it on Page_Load? ... THAT is a more important question.
Do you know of any good docs/books that might spell it out for a fool like
myself?

I'm a newbie on this - and I've got a lot to learn.

- wASP


- wASP
Nov 17 '05 #5

P: n/a
Don't you already have a direct reference to this placeholder? You dragged
it on to the page, presumably. Which means a variable got created for it.

"wASP" <wylbur[at]ev1[dot]net> wrote in message
news:uv********************************@4ax.com...
On Thu, 28 Jul 2005 15:14:08 -0400, "Marina" <so*****@nospam.com> wrote:
No, the control in this example has been instantiated. It exists, it is
declared in the .aspx page, and by the time Init runs it points to a real
object.


Going back to my original post ...

So then this:

PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");

... should return a valid reference to the placeholder, right?

If not, then how would I get a valid reference?

In the Init or Load event, you would declare a variable of the type you
want. You would create an instance of whatever object it is you want, and
then add it to your placeholder. Then you would the handler to this
object
you just created. If the page has viewstate on, the control should just
maintain its viewstate. You have to do this on every postback.


I thought I did that?

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for 'Init'.
PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");
plc_hldr.Init += new System.EventHandler (plc_hldr_Control_Init);
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void plc_hldr_Control_Init (object sender,EventArgs e)
{
Dyn_Control_Placeholder.Controls.Add
( new LiteralControl("<br>added 1 on init<br>") );
...
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

I also tried doing this:

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void plc_hldr_Control_Init (object sender,EventArgs e)
{
this.Controls.Add
( new LiteralControl("<br>added 1 on init<br>") );
...
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

... with the same (non)result.
You have to do this on every postback.


I realize this, but I can't even get it to work on the first posting
- never mind postback.

So am I just stupid or som'tin'?

In my own effort, I tried to attach a literal control to a placeholder:
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>

void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for 'Init'.
// myControl.Init += new System.EventHandler(Control_Init);
PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");
plc_hldr.Init += new System.EventHandler (plc_hldr_Control_Init);
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void plc_hldr_Control_Init (object sender,EventArgs e)
{
Label lbl_msg01 = (Label) FindControl ("Message01");

Dyn_Control_Placeholder.Controls.Add
( new LiteralControl("<br>added 1 on init<br>") );
PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");

lbl_msg01.Text = "The ID of the object initially : " + plc_hldr.ID;
Response.Write("The ID of the object initially : " + plc_hldr.ID);

plc_hldr.Controls.Add ( new LiteralControl("<br>added 2 on
init<br>") );
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>



"wASP" <wylbur[at]ev1[dot]net> wrote in message
news:i8********************************@4ax.com. ..
On Thu, 28 Jul 2005 14:16:18 -0400, "Marina" <so*****@nospam.com> wrote:

This is an example of dynamically adding an event handler to a control.
They
are assuming the control is already there.

Hi Marina,

OK - so I'm trying to add a handler to a control that has yet to be
instantiated?

Can you give me a clue as to how would I add a dynamic control
to a placeholder before Page_Load gets fired and the viewstate
is recovered on postback?

... or ...

How could I save the viewstate for a specific control (created
dynamically)
then recover it on Page_Load? ... THAT is a more important question.
Do you know of any good docs/books that might spell it out for a fool
like
myself?

I'm a newbie on this - and I've got a lot to learn.

- wASP


- wASP

Nov 17 '05 #6

P: n/a
> PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");
plc_hldr.Init += new System.EventHandler(plc_hldr_Control_Init);


You can replace that with:

Dyn_Control_Placeholder.Init += new
System.EventHandler(plc_hldr_Control_Init);

Elements on the page are accessible by their ID.

I don't understand why your plc_hldr_Control_Init isn't called though. I
tried a very simple sample, with this code:

void Page_Init(object sender, EventArgs e)
{
myControl.Init += new System.EventHandler(Control_Init);
}

void Control_Init(object sender, EventArgs e)
{
Response.Write("Control_Init called);
}

I could break on the Page_Init() function, but the breakpoint in
Control_Init() never went off. Perhaps someone else has a clue ?

Greetings,
Wessel
Nov 17 '05 #7

P: n/a
On Thu, 28 Jul 2005 15:40:44 -0400, "Marina" <so*****@nospam.com> wrote:
Don't you already have a direct reference to this placeholder? You dragged
it on to the page, presumably. Which means a variable got created for it.
I'm afraid I didn't drag anything onto any pages: I'm not using studio.
I'm just using notepad at this point.

I tried using Wessel's suggestion:

Dyn_Control_Placeholder.Init += new
System.EventHandler(plc_hldr_Control_Init);

... and that didn't work either.

wASP
==============================

"wASP" <wylbur[at]ev1[dot]net> wrote in message
news:uv********************************@4ax.com.. .
On Thu, 28 Jul 2005 15:14:08 -0400, "Marina" <so*****@nospam.com> wrote:
No, the control in this example has been instantiated. It exists, it is
declared in the .aspx page, and by the time Init runs it points to a real
object.


Going back to my original post ...

So then this:

PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");

... should return a valid reference to the placeholder, right?

If not, then how would I get a valid reference?

In the Init or Load event, you would declare a variable of the type you
want. You would create an instance of whatever object it is you want, and
then add it to your placeholder. Then you would the handler to this
object
you just created. If the page has viewstate on, the control should just
maintain its viewstate. You have to do this on every postback.


I thought I did that?

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for 'Init'.
PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");
plc_hldr.Init += new System.EventHandler (plc_hldr_Control_Init);
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void plc_hldr_Control_Init (object sender,EventArgs e)
{
Dyn_Control_Placeholder.Controls.Add
( new LiteralControl("<br>added 1 on init<br>") );
...
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

I also tried doing this:

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void plc_hldr_Control_Init (object sender,EventArgs e)
{
this.Controls.Add
( new LiteralControl("<br>added 1 on init<br>") );
...
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

... with the same (non)result.
You have to do this on every postback.


I realize this, but I can't even get it to work on the first posting
- never mind postback.

So am I just stupid or som'tin'?

In my own effort, I tried to attach a literal control to a placeholder:
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>

void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for 'Init'.
// myControl.Init += new System.EventHandler(Control_Init);
PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");
plc_hldr.Init += new System.EventHandler (plc_hldr_Control_Init);
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void plc_hldr_Control_Init (object sender,EventArgs e)
{
Label lbl_msg01 = (Label) FindControl ("Message01");

Dyn_Control_Placeholder.Controls.Add
( new LiteralControl("<br>added 1 on init<br>") );
PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");

lbl_msg01.Text = "The ID of the object initially : " + plc_hldr.ID;
Response.Write("The ID of the object initially : " + plc_hldr.ID);

plc_hldr.Controls.Add ( new LiteralControl("<br>added 2 on
init<br>") );
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>



"wASP" <wylbur[at]ev1[dot]net> wrote in message
news:i8********************************@4ax.com ...
On Thu, 28 Jul 2005 14:16:18 -0400, "Marina" <so*****@nospam.com> wrote:

>This is an example of dynamically adding an event handler to a control.
>They
>are assuming the control is already there.

Hi Marina,

OK - so I'm trying to add a handler to a control that has yet to be
instantiated?

Can you give me a clue as to how would I add a dynamic control
to a placeholder before Page_Load gets fired and the viewstate
is recovered on postback?

... or ...

How could I save the viewstate for a specific control (created
dynamically)
then recover it on Page_Load? ... THAT is a more important question.
Do you know of any good docs/books that might spell it out for a fool
like
myself?

I'm a newbie on this - and I've got a lot to learn.

- wASP


- wASP


- wASP
Nov 17 '05 #8

P: n/a
On Thu, 28 Jul 2005 21:49:59 +0200, "Wessel Troost" <no*****@like.the.sun> wrote:
PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");
plc_hldr.Init += new System.EventHandler(plc_hldr_Control_Init);
You can replace that with:

Dyn_Control_Placeholder.Init += new
System.EventHandler(plc_hldr_Control_Init);

Elements on the page are accessible by their ID.


I just tried replacing my FindControl with this - it didn't do any good
- same (non)result.

I don't understand why your plc_hldr_Control_Init isn't called though. I
tried a very simple sample, with this code:

void Page_Init(object sender, EventArgs e)
{
myControl.Init += new System.EventHandler(Control_Init);
}

void Control_Init(object sender, EventArgs e)
{
Response.Write("Control_Init called);
}

I could break on the Page_Init() function, but the breakpoint in
Control_Init() never went off. Perhaps someone else has a clue ?

Greetings,
Wessel


THAT is not good - not good at all. :-(

Can you try it with my code?
This is the only change from the code in my first post:
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for 'Init'.
Dyn_Control_Placeholder.Init
+= new System.EventHandler (plc_hldr_Control_Init);
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void plc_hldr_Control_Init (object sender,EventArgs e)
{
Label lbl_msg01 = (Label) FindControl ("Message01");

// Dyn_Control_Placeholder.Controls.Add
this.Controls.Add
( new LiteralControl ("<br>added 1 on init<br>") );
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ *

Maybe it's time I started thinking about getting an IDE - Studio perhaps?

Does anyone have any suggestions?

- wASP
Nov 17 '05 #9

P: n/a

OK.

It seems to me that the only reason why I would want to create dynamic controls
in the initialization stage (Page_Init) would be to maintain viewstate.

If I were to save the viewstates for the dynamic controls by overriding
the functions with my own, then I could load everything during the Page_Load event
- and THEN load the viewstates for those controls at that time of the cycle
- and just sidestep the whole Page_Init problem entirely.

I've been reviewing the docs, and these are the given examples for those controls:
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>

Example

The following example overrides the SaveViewState method in a custom ASP.NET server
control.
When this method is invoked, it determines whether the control has any child controls
and whether the containing Page object is the result of a postback. If both are true,
it changes the Text property of a Label Web server control to read Custom Control Has
Saved State. It then saves the view state of the control as an array of objects,
named allStates.

protected override object SaveViewState()
{ // Change Text Property of Label when this function is invoked.
if(HasControls() && (Page.IsPostBack))
{
((Label)(Controls[0])).Text = "Custom Control Has Saved State";
}
// Save State as a cumulative array of objects.
object baseState = base.SaveViewState();
string userText = UserText;
string passwordText = PasswordText;
object[] allStates = new object[3];
allStates[0] = baseState;
allStates[1] = userText;
allStates[2] = PasswordText;
return allStates;
}

<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>

I can follow that, but what do I do about non-textual controls?
... such as radio buttons?

Would I have to design my own logic to recover the states for those?

That isn't an insurmountable problem (I don't think), but if there is
already a mechanism for doing this, then I wouldn't have to bother.

Do all of the elements in the "allStates" object have to be strings?
... or can I give it ANY type?

I'm thinking that the data stored within that viewstate object
would be in the same direct binary format as it is presented by the control
- as would have to be the case for the recovery of ALL viewstates
- whether it's done automatically or by explicit invocation.

OK - and here's where I need experienced input and perspective ...

Are there any potential pitfalls that I should be aware of in taking this course?

I have a feeling that this isn't going to be as simple as I think it might be.

This is the given example for the LoadViewState method:
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>

Example

The following example overrides the LoadViewState method for a custom ASP.NET
server control. It creates an Object array to contain the view state information
passed in the savedState parameter, and then calls the base implementation of
the LoadViewState method for the first index location of the array. It assigns
the values stored at the next two index locations to variables named UserText
and PasswordText, respectively.

protected override void LoadViewState(object savedState)
{
if (savedState != null)
{
// Load State from the array of objects that was saved at ;
// SavedViewState.
object[] myState = (object[])savedState;
if (myState[0] != null)
base.LoadViewState(myState[0]);
if (myState[1] != null)
UserText = (string)myState[1];
if (myState[2] != null)
PasswordText = (string)myState[2];
}
}
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
- wASP
Nov 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.