Child Neglect

W

wASP

Hi,

I was under the impression that, when ASP rendered a page,
it recursively traversed the hierarchy, executing all controls
in each ControlCollection of every control in that hierarchy.

I tried adding a Button control to a TextBox ControlCollection,
and it didn't execute. When I add the control to the Placeholder
ControlCollection (after the TextBox control is added), it executes
without any problem:
/* . . . . . . . . . . . . . . . . */
protected void create_ctrl_pair_0x
(String text_box_id, String Btn_ID, String Btn_text)
{
ControlCollection crnt_ctrl = CCP_Dyn_Ctrl_Placeholder.Controls;

// Create UserTextBox TextBox control.

UserTextBox0x = new TextBox ();

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

// Add UserTextBox TextBox control to the Controls collection
// of the Dyn_Control_Placeholder PlaceHolder control.

crnt_ctrl.Add (UserTextBox0x);
crnt_ctrl.Add ( new LiteralControl("<br>XxXxXxXxXxXxXxXxX<br><br>") );

// Create and initialize a Button.

button0x = new Button();
button0x.ID = Btn_ID;
button0x.Text = Btn_text;

// make the button a child of the text box control.
crnt_ctrl = UserTextBox0x.Controls;

crnt_ctrl.Add (button0x);
crnt_ctrl.Add
( new LiteralControl("<br>===================<br><br>") );
}
/* . . . . . . . . . . . . . . . . */
/* | | | | | | | | | | | | | | | | */
/* . . . . . . . . . . . . . . . . */



If this assignment statement is removed from the above:
crnt_ctrl = UserTextBox0x.Controls;
... it works fine. Otherwise, there is no button, as the child is neglected.



Can anyone give me an idea as to what I would have to do to make this work?



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

<html>
<head>

<script runat="server">

/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- */
/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- */
// protected class sys_obj_class
public class sys_obj_class
{
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)
{
this_page01 = Dyn_Ctrl_Placeholder.Page;
Obj_Dyn_Ctrl_Placeholder = Dyn_Ctrl_Placeholder;
Obj_Dyn_Label01 = Dyn_Label;
}
}

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
public void Page_Init (object sender, EventArgs e)
{
int ndx = 2;

Dyn_Control_Placeholder.Controls.Add
( new LiteralControl("<br>||| added 1 on init |||<br><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 PlaceHolder CCP_Dyn_Ctrl_Placeholder;
protected Label Dyn_Lbl_msg01;

/* =#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#= *
<<< constructor >>>
* =#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#= */
protected base_create_ctrl_pair (sys_obj_class sys_obj)
{
CCP_Dyn_Ctrl_Placeholder = sys_obj.Get_Obj_Dyn_Ctrl_Placeholder;
Dyn_Lbl_msg01 = sys_obj.Get_Obj_Dyn_Label01;
}
/* =#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#= */
/* =#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#= */

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 text_box_id, String Btn_ID, String Btn_text)
{
ControlCollection crnt_ctrl = CCP_Dyn_Ctrl_Placeholder.Controls;

// Create UserTextBox TextBox control.

UserTextBox0x = new TextBox ();

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

// Add UserTextBox TextBox control to the Controls collection
// of the Dyn_Control_Placeholder PlaceHolder control.

crnt_ctrl.Add (UserTextBox0x);
crnt_ctrl.Add ( new LiteralControl("<br>XxXxXxXxXxXxXxXxX<br><br>") );

// Create and initialize a Button.

button0x = new Button();
button0x.ID = Btn_ID;
button0x.Text = Btn_text;

// make the button a child of the text box control.
crnt_ctrl = UserTextBox0x.Controls;

crnt_ctrl.Add (button0x);
crnt_ctrl.Add
( new LiteralControl("<br>===================::::<br><br>") );
}
/* . . . . . . . . . . . . . . . . */


/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- *
<<< Button Click Handlers >>>
* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- */
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)
CCP_Dyn_Ctrl_Placeholder.FindControl("UserTextBox1");

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

}
/* - - - - - - - - - - - - - - - - - - - - - - */
/* END: protected class base_create_ctrl_pair */
/* - - - - - - - - - - - - - - - - - - - - - - */

/* /|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\ */
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);
}
/* . . . . . . . . . . . . . */
}
/* /|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\ */
/* /|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\_/|\ */

</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>

<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>
<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>-<+>



THANKS!!!



- wASP
 
B

Bruce Barker

not quite. the page renders all its children. if the one of the form
controls supports children of its own, it renders them.

the default implemenation of Render is RenderChildren. the TextBox overrides
the render with its own, and never calls RenderChildren (because the TextBox
does not support children).

-- bruce (sqlwork.com)
 
W

wASP

not quite. the page renders all its children. if the one of the form
controls supports children of its own, it renders them.

the default implemenation of Render is RenderChildren. the TextBox overrides
the render with its own, and never calls RenderChildren (because the TextBox
does not support children).

-- bruce (sqlwork.com)

I'm thinking that this behavior can be overridden ... maybe?

The first question that springs into my mind is that,
if the TextBox does not support children (the lousy deadbeat),
then why would it have a ControlCollection?

I'll start digging through the docs ...


- wASP
 
R

rl

Why would you want a TextBox to contain a Button as a child? Always
think about the final HTML that is the eventual target of all these
ASP.NET objects. Having a button as a child of a TextBox would
generate invalid HTML:

<input type="text"><input type="submit" /></input>

If what you want is to have a Button that immediately follows a
TextBox, then the HTML you want is:

<input type="text" /><input type="submit" />

And in this case the Button is NOT a child of the TextBox. Remember
that "child of" means "contained by" and now "follows."

Try adding the Button as a child of the Page (or the Control, if that's
where the TextBox is) and specifying the index at which to add the
control:

Page.Controls.AddAt(Page.Controls.IndexOf(tx),bt);

You can also use a Panel or Placeholder control to contain both the
TextBox and the Button:

pnl.Controls.AddAt(pnl.Controls.IndexOf(tx),bt);

rl
 
W

wASP

Why would you want a TextBox to contain a Button as a child? Always
think about the final HTML that is the eventual target of all these
ASP.NET objects. Having a button as a child of a TextBox would
generate invalid HTML:

<input type="text"><input type="submit" /></input>

If what you want is to have a Button that immediately follows a
TextBox, then the HTML you want is:

<input type="text" /><input type="submit" />

And in this case the Button is NOT a child of the TextBox. Remember
that "child of" means "contained by" and now "follows."

*NOW* I get it.

I had a basic misunderstanding as to the implications of making
the button control a child of the textbox control - that is until
you clued me in with your post.

Try adding the Button as a child of the Page (or the Control, if that's
where the TextBox is) and specifying the index at which to add the
control:

Page.Controls.AddAt(Page.Controls.IndexOf(tx),bt);

You can also use a Panel or Placeholder control to contain both the
TextBox and the Button:

pnl.Controls.AddAt(pnl.Controls.IndexOf(tx),bt);

THAT is what I need to do.

I appreciate your efforts to enlighten me on this issue.


THANKS rl!!!


- wASP
 

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

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top