On Wed, 27 Jul 2005 09:24:48 +0200, "Wessel Troost" <no*****@like.the.sun> wrote:
On top of that, the Page.IsPostBack works OK when in the Page_Load
function,
so that proves that SOMETHING is "there" - I just cannot figure out how
to reference it in a class method/function as of yet.
That must be the source of the confusion. Page_Load is actually a member
function of the Page class, so it has access to the page class. But if
you define a new class, it has no relation to the Page class, even though
it is declared inside the page.
You can't just call IsPostback from anywhere; it has to be from inside the
Page object, or using a reference to the page object.
Golly geeeeeee whizzzz - I did not know that
- and I'm NOT being sarcastic when I tell you that
- so don't take it that way.
I did not know that - seriously.
For some strange reason, I thought that the page object was global to
anything and everything on the page - so this comes as a bit of a stunner.
A big part of my problem in all of this is that I don't have a firm understanding
of the nature of scoping in C# - but now you've given me a clue about it.
Has anyone told you that you're wonderful, Wessel?
Well I'll tell you: You're wonderful, Wessel.
How do I invoke those functions/clases/methods within the class methods
without having to pass references as parameters in the arglist?
From a class member, just call:
IsPostback()
or:
this.IsPostback()
From a class that is unrelated, you have to call:
System.Web.UI.Page ThePage = ...;
ThePage.IsPostback()
Yes, but my problem is with the ellipsis - those 3 dots after “ThePage.”
In any case, after I read your most recent post on this matter,
and become better enlightened, I’ve been re-engineering my little toy
- and I’ve gotten the latest version of it to work:
/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\
\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\-/
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
<script runat="server">
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- */
// protected class sys_obj_class
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;
}
}
/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- */
/* public static void Submit_Click (Object sender, EventArgs e) */
/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
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);
ctrl_pair_creation_02 ccp2 = new ctrl_pair_creation_02 (sys_obj);
ctrl_pair_creation_03 ccp3 = new ctrl_pair_creation_03 (sys_obj);
ccp1.create_ctrl_pair_01 ();
ccp2.create_ctrl_pair_02 ();
ccp3.create_ctrl_pair_03 ();
}
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
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;
}
public void Submit_Click2(Object sender, EventArgs e)
{ TextBox TempTextBox = (TextBox)
base_Dyn_Ctrl_Placeholder.FindControl("UserTextBox 2");
Dyn_Lbl_msg01.Text = submit_click_str01 + '2' + submit_click_str02 +
TempTextBox.Text;
}
public void Submit_Click3(Object sender, EventArgs e)
{ TextBox TempTextBox = (TextBox)
base_Dyn_Ctrl_Placeholder.FindControl("UserTextBox 3");
Dyn_Lbl_msg01.Text = submit_click_str01 + '3' + submit_click_str02 +
TempTextBox.Text;
}
/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- */
}
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
protected class ctrl_pair_creation_01 : base_create_ctrl_pair
{
private sys_obj_class lcl_sys_obj;
public ctrl_pair_creation_01 (sys_obj_class sys_obj) : base (sys_obj)
{ lcl_sys_obj = sys_obj; }
//{ base_create_ctrl_pair (pram_ph01_ctrl); }
/* . . . . . . . . . . . . . */
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>") );
}
/* . . . . . . . . . . . . . */
}
/* - - - - - - - - - - - - - - - - - - - - - - */
protected class ctrl_pair_creation_02 : base_create_ctrl_pair
{
private sys_obj_class lcl_sys_obj;
public ctrl_pair_creation_02 (sys_obj_class sys_obj) : base (sys_obj)
{ lcl_sys_obj = sys_obj; }
//{ base_create_ctrl_pair (pram_ph01_ctrl); }
/* . . . . . . . . . . . . . */
public void create_ctrl_pair_02 ()
{ // Create UserTextBox TextBox control.
create_ctrl_pair_0x
("UserTextBox2", "DButton02", "Submit 02");
button0x.Click += new EventHandler (Submit_Click2);
if (!lcl_sys_obj.Get_this_page01.IsPostBack)
init_single_line_text_box (UserTextBox0x, 22);
ph01_ctrl.Add ( new LiteralControl("<br><br>") );
}
/* . . . . . . . . . . . . . */
}
/* - - - - - - - - - - - - - - - - - - - - - - */
protected class ctrl_pair_creation_03 : base_create_ctrl_pair
{
private sys_obj_class lcl_sys_obj;
public ctrl_pair_creation_03 (sys_obj_class sys_obj) : base (sys_obj)
{ lcl_sys_obj = sys_obj; }
//{ base_create_ctrl_pair (pram_ph01_ctrl); }
/* . . . . . . . . . . . . . */
public void create_ctrl_pair_03 ()
{ // Create UserTextBox TextBox control.
create_ctrl_pair_0x
("UserTextBox3", "DButton03", "Submit 03");
button0x.Click += new EventHandler (Submit_Click3);
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> TextBox Constructor Example </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>
/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\
\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\_/-\-/
As you can see, I’ve gathered up all of the needed objects,
and encapsulated them in a sys_obj (public class sys_obj_class)
- and now it’s just a matter of passing that object around
to the control-construction classes. If I want to construct
any controls for any additional placeholder/label pairs,
all I have to do is to pass the ID’s for each respective
pair to the FindControl functions (to get the associated objects),
then construct a corresponding sys_obj for that pair:
- - - - - - - - - - - - - - -
plc_hldr = (PlaceHolder) FindControl ("Dyn_Control_Placeholder02");
lbl_msg01 = (Label) FindControl ("Message02");
sys_obj_class sys_obj02 = new sys_obj_class (plc_hldr, lbl_msg01);
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
... and place the necessary tags in the HTML section of course.
The next step is to create and manage viewstate for each respective
dynamic web control. By doing that, I can make random insertions
(“addat”) and deletions to the hierarchy without messing up
the viewstate for the page as a whole. I was looking through some
of the docs for that, but I’m not sure if I have a good understanding
of what I was reading as of yet. I’m thinking that my C# books will
give me a better idea.
... and then there’ll be input validation to attach to this mess.
Thanks a lot Wessel - you're wonderful.
- wASP