Dumb Question About External References

W

wASP

Hello everyone,

I'm new to C# and ASP.NET, so pardon my stupidity on this one.

I'm having a problem with referencing methods/functions external
to a class member function.

My code is as follows:
. . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . .
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
public class ccp_01 : base_create_ctrl_pair
{
public ccp_01 (ControlCollection pram_ph01_ctrl)
: base (pram_ph01_ctrl) {}
//{ 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 (!Page.IsPostBack)
init_single_line_text_box (UserTextBox0x, 22);

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

. . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . .

/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- */
public void Submit_Click(Object sender, EventArgs e)
{ // Retrieve the UserTextBox TextBox control from the TextBoxControlPlaceHolder
// PlaceHolder control.

TextBox TempTextBox
= (TextBox)TextBoxControlPlaceHolder.FindControl("UserTextBox1");

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



The compiler issues diagnostics, and they are as follows
(with the offending lines in square brackets):
. . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . .

C:\WINNT\system32> "c:\winnt\microsoft.net\framework\v1.1.4322\csc.exe"
/t:library /utf8output
/R:"c:\winnt\assembly\gac\system.enterpriseservices\1.0.5000.0__b03f5f7f11d50a3a
\system.enterpriseservices.dll"
/R:"c:\winnt\assembly\gac\system.data\1.0.5000.0__b77a5c561934e089\system.data.dll"

/R:"c:\winnt\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.dll"

/R:"c:\winnt\assembly\gac\system.web.mobile\1.0.5000.0__b03f5f7f11d50a3a\system.web.mobile.dll"
/R:"c:\winnt\assembly\gac\system.xml\1.0.5000.0__b77a5c561934e089\system.xml.dll"

/R:"c:\winnt\assembly\gac\system.web.services\1.0.5000.0__b03f5f7f11d50a3a\system.web.services.dll"
/R:"c:\winnt\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll"
/R:"c:\winnt\assembly\gac\system.web\1.0.5000.0__b03f5f7f11d50a3a\system.web.dll"
/R:"c:\winnt\microsoft.net\framework\v1.1.4322\mscorlib.dll"
/out:"C:\WINNT\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files

\webfolder01\08460e05\9c82db3e\rdoqxnq-.dll"
/debug- /optimize+ /warnaserror
/w:1 "C:\WINNT\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files

\webfolder01\08460e05\9c82db3e\rdoqxnq-.0.cs"


Microsoft (R) Visual C# .NET Compiler version 7.10.6001.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.

I:\Virtual Webfolders\ViewState02.aspx(123,50): error CS0038:
Cannot access a nonstatic member of outer type 'ASP.ViewState02_aspx'
via nested type 'ASP.ViewState02_aspx.ccp_01'

[button0x.Click += new EventHandler (Submit_Click);]


I:\Virtual Webfolders\ViewState02.aspx(125,19): error CS0120:
An object reference is required for the nonstatic field, method,
or property 'System.Web.UI.Page.IsPostBack'

[if (!Page.IsPostBack)]

. . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . .



This is probably a piece of cake for most of you.

Can anyone clue me in as to what I need to do to make the external references?

Thanks In Advance.

- wASP
 
W

Wessel Troost

I'm having a problem with referencing methods/functions external
to a class member function.
It looks like you're calling System.Web.Ui.Page members outside the Page
class. The ccp_01 class doesn't have a IsPostback member!

A solution can be to pass a Page object to the constructor of the ccp_01
class.

If you post your entire ASPX page, I'll see if I can get an example.

Greetings,
Wessel
 
W

wASP

It looks like you're calling System.Web.Ui.Page members outside the Page
class. The ccp_01 class doesn't have a IsPostback member!

A solution can be to pass a Page object to the constructor of the ccp_01
class.

If you post your entire ASPX page, I'll see if I can get an example.

Greetings,
Wessel

Hi Wessel,

Here's the whole thing (it's just a toy for my own enlightenment):
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

<script runat="server">

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

String submit_click_str02
= " control above is dynamically generated. <br> You entered: ";
/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- */
public void Submit_Click(Object sender, EventArgs e)
{ TextBox TempTextBox =
(TextBox)TextBoxControlPlaceHolder.FindControl("UserTextBox1");
Message.Text = submit_click_str01 + '1' + submit_click_str02 +
TempTextBox.Text;
}

protected void Submit_Click2(Object sender, EventArgs e)
{ TextBox TempTextBox =
(TextBox)TextBoxControlPlaceHolder.FindControl("UserTextBox2");
Message.Text = submit_click_str01 + '2' + submit_click_str02 +
TempTextBox.Text;
}

protected void Submit_Click3(Object sender, EventArgs e)
{ TextBox TempTextBox =
(TextBox)TextBoxControlPlaceHolder.FindControl("UserTextBox3");
Message.Text = submit_click_str01 + '3' + submit_click_str02 +
TempTextBox.Text;
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
protected void Page_Load(Object sender, EventArgs e)
{
ControlCollection pram_ph01_ctrl = TextBoxControlPlaceHolder.Controls;

ccp_01 ccp1 = new ccp_01 (pram_ph01_ctrl);
ccp_02 ccp2 = new ccp_02 (pram_ph01_ctrl);
ccp_03 ccp3 = new ccp_03 (pram_ph01_ctrl);

ccp1.create_ctrl_pair_01 ();
ccp2.create_ctrl_pair_02 ();
ccp3.create_ctrl_pair_03 ();
}
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
public class base_create_ctrl_pair
{
protected TextBox UserTextBox0x;
protected Button button0x;
protected ControlCollection ph01_ctrl;

// constructor
protected base_create_ctrl_pair (ControlCollection pram_ph01_ctrl)
{ ph01_ctrl = pram_ph01_ctrl; }

/* . . . . . . . . . . . . . . . */
public 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 TextBoxControlPlaceHolder PlaceHolder control.
ph01_ctrl.Add (new_TextBox);

return new_TextBox;
}
/* . . . . . . . . . . . . . . . */

/* --------------------------------------------------------- */
/* --------------------------------------------------------- */
protected void InitializeMyButton (String Btn_ID, String Btn_text)
{ // Create and initialize a Button.
button0x = new Button();
ph01_ctrl.Add (button0x);

button0x.ID = Btn_ID;
button0x.Text = Btn_text;

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

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);
}
} /* END: public class base_create_ctrl_pair */
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */

/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
public class ccp_01 : base_create_ctrl_pair
{
// constructor
public ccp_01 (ControlCollection pram_ph01_ctrl)
: base (pram_ph01_ctrl) {}

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

button0x.Click += new EventHandler (Submit_Click);

if (!Page.IsPostBack)
init_single_line_text_box (UserTextBox0x, 22);

ph01_ctrl.Add ( new LiteralControl("<br><br>") );
}
}
/* - - - - - - - - - - - - - - - - - - - - - - */
public class ccp_02 : base_create_ctrl_pair
{
public ccp_02 (ControlCollection pram_ph01_ctrl)
: base (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 (!Page.IsPostBack)
init_single_line_text_box (UserTextBox0x, 22);

ph01_ctrl.Add ( new LiteralControl("<br><br>") );
}
}
/* - - - - - - - - - - - - - - - - - - - - - - */
public class ccp_03 : base_create_ctrl_pair
{
public ccp_03 (ControlCollection pram_ph01_ctrl)
: base (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 (!Page.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="TextBoxControlPlaceHolder" runat="server"/>

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

</form>

</body>
</html>
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\



What follows are the diagnostics:
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\

Microsoft (R) Visual C# .NET Compiler version 7.10.6001.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.

I:\Virtual Webfolders\Clean\ViewState02.aspx(109,52): error CS0038:
Cannot access a nonstatic member of outer type 'ASP.ViewState02_aspx'
via nested type 'ASP.ViewState02_aspx.ccp_01'

I:\Virtual Webfolders\Clean\ViewState02.aspx(111,21): error CS0120:
An object reference is required for the nonstatic field, method,
or property 'System.Web.UI.Page.IsPostBack'

I:\Virtual Webfolders\Clean\ViewState02.aspx(128,50): error CS0038:
Cannot access a nonstatic member of outer type 'ASP.ViewState02_aspx'
via nested type 'ASP.ViewState02_aspx.ccp_02'

I:\Virtual Webfolders\Clean\ViewState02.aspx(130,19): error CS0120:
An object reference is required for the nonstatic field, method,
or property 'System.Web.UI.Page.IsPostBack'

I:\Virtual Webfolders\Clean\ViewState02.aspx(147,50): error CS0038:
Cannot access a nonstatic member of outer type 'ASP.ViewState02_aspx'
via nested type 'ASP.ViewState02_aspx.ccp_03'

I:\Virtual Webfolders\Clean\ViewState02.aspx(149,19): error CS0120:
An object reference is required for the nonstatic field, method,
or property 'System.Web.UI.Page.IsPostBack'

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\



What baffles me is that, if the compiler can make reference to the IsPostBack member,
and recognise it as being in the proper namespace ...
- - - - - - - - - - - - - - - - - - - - - - - -
An object reference is required for the nonstatic field, method,
or property 'System.Web.UI.Page.IsPostBack'
- - - - - - - - - - - - - - - - - - - - - - - -

... then why can't it resolve the reference?


Maybe I need to break down and go buy a good reference for C#,
instead of trying to rely upon my C++ background - and glean what I can
from the MS docs and various websites/forums.

Does anyone have any recomendations?

- wASP
 
W

Wessel Troost

What baffles me is that, if the compiler can make reference to the
IsPostBack member,
and recognise it as being in the proper namespace ...
- - - - - - - - - - - - - - - - - - - - - - - -
An object reference is required for the nonstatic field, method,
or property 'System.Web.UI.Page.IsPostBack'
- - - - - - - - - - - - - - - - - - - - - - - -

... then why can't it resolve the reference?
The namespace is correct, but there is no object. An object is an
instance of a class. Your code is the C++ equivalent of (uncompiled code):

class Class
{
void Method();
}

int main()
{
// This doesn't work; nonstatic method needs an object, not a class
Class.Method();
// This will work; we have an instance of the class to call
Class TheObject();
TheObject.Method();
return 0;
}
Maybe I need to break down and go buy a good reference for C#,
instead of trying to rely upon my C++ background - and glean what I can
from the MS docs and various websites/forums.
Well yeah, and you could use a better coding style, if you''ll pardon me.
pram_ph01_ctrl doesn't tell me much about what the variable does.
Consider arrTextControls or m_TextControls or TextControls?
Does anyone have any recomendations?
Take a subscription on Safari (http://oreilly.safari.com) which allows you
to scan books and only read the ones that are really good. Way cheaper
than buying real books :)

Working sample follows.

Greetings,
Wessel



<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

<script runat="server">

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

String submit_click_str02
= " control above is dynamically generated. <br> You entered: ";
/*
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- */
public void Submit_Click(Object sender, EventArgs e)
{ TextBox TempTextBox =
(TextBox)TextBoxControlPlaceHolder.FindControl("UserTextBox1");
Message.Text = submit_click_str01 + '1' + submit_click_str02 +
TempTextBox.Text;
}

protected void Submit_Click2(Object sender, EventArgs e)
{ TextBox TempTextBox =
(TextBox)TextBoxControlPlaceHolder.FindControl("UserTextBox2");
Message.Text = submit_click_str01 + '2' + submit_click_str02 +
TempTextBox.Text;
}

protected void Submit_Click3(Object sender, EventArgs e)
{ TextBox TempTextBox =
(TextBox)TextBoxControlPlaceHolder.FindControl("UserTextBox3");
Message.Text = submit_click_str01 + '3' + submit_click_str02 +
TempTextBox.Text;
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
protected void Page_Load(Object sender, EventArgs e)
{
ControlCollection pram_ph01_ctrl =
TextBoxControlPlaceHolder.Controls;

ccp_01 ccp1 = new ccp_01(pram_ph01_ctrl);
ccp_02 ccp2 = new ccp_02(pram_ph01_ctrl);
ccp_03 ccp3 = new ccp_03(pram_ph01_ctrl);

ccp1.create_ctrl_pair_01(new EventHandler(Submit_Click),
this.IsPostBack);
ccp2.create_ctrl_pair_02(new EventHandler(Submit_Click2),
this.IsPostBack);
ccp3.create_ctrl_pair_03(new EventHandler(Submit_Click3),
this.IsPostBack);
}
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
public class base_create_ctrl_pair
{
protected TextBox UserTextBox0x;
protected Button button0x;
protected ControlCollection ph01_ctrl;

// constructor
protected base_create_ctrl_pair (ControlCollection
pram_ph01_ctrl)
{ ph01_ctrl = pram_ph01_ctrl; }

/* . . . . . . . . . . . . . . . */
public 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 TextBoxControlPlaceHolder PlaceHolder control.
ph01_ctrl.Add (new_TextBox);

return new_TextBox;
}
/* . . . . . . . . . . . . . . . */

/* --------------------------------------------------------- */
/* --------------------------------------------------------- */
protected void InitializeMyButton (String Btn_ID, String
Btn_text)
{ // Create and initialize a Button.
button0x = new Button();
ph01_ctrl.Add (button0x);

button0x.ID = Btn_ID;
button0x.Text = Btn_text;

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

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);
}
} /* END: public class base_create_ctrl_pair */
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */

/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
public class ccp_01 : base_create_ctrl_pair
{
// constructor
public ccp_01 (ControlCollection pram_ph01_ctrl )
: base (pram_ph01_ctrl) {}

public void create_ctrl_pair_01 ( EventHandler oEventHandler,
bool bIsPostback )
{ create_ctrl_pair_0x
("UserTextBox1", "DButton01", "Submit 01");

button0x.Click += oEventHandler;

if (!bIsPostback)
init_single_line_text_box (UserTextBox0x, 22);

ph01_ctrl.Add ( new LiteralControl("<br><br>") );
}
}
/* - - - - - - - - - - - - - - - - - - - - - - */
public class ccp_02 : base_create_ctrl_pair
{
public ccp_02 (ControlCollection pram_ph01_ctrl)
: base (pram_ph01_ctrl) {}

public void create_ctrl_pair_02 ( EventHandler oEventHandler,
bool bIsPostBack )
{ // Create UserTextBox TextBox control.
create_ctrl_pair_0x
("UserTextBox2", "DButton02", "Submit 02");

button0x.Click += oEventHandler;

if (!bIsPostBack)
init_single_line_text_box (UserTextBox0x, 22);

ph01_ctrl.Add ( new LiteralControl("<br><br>") );
}
}
/* - - - - - - - - - - - - - - - - - - - - - - */
public class ccp_03 : base_create_ctrl_pair
{
public ccp_03 (ControlCollection pram_ph01_ctrl)
: base (pram_ph01_ctrl) {}

public void create_ctrl_pair_03(EventHandler oEventHandler, bool
bIsPostBack)
{ // Create UserTextBox TextBox control.
create_ctrl_pair_0x
("UserTextBox3", "DButton03", "Submit 03");

button0x.Click += oEventHandler;

if (!bIsPostBack)
init_single_line_text_box (UserTextBox0x, 22);

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

</script>
</head>

<body>

<form id="Form1" runat="server">

<h3> TextBox Constructor Example </h3>

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

<asp:placeHolder ID="TextBoxControlPlaceHolder" runat="server"/>

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

</form>

</body>
</html>
 
W

wASP

The namespace is correct, but there is no object. An object is an
instance of a class. Your code is the C++ equivalent of (uncompiled code):

class Class
{
void Method();
}

int main()
{
// This doesn't work; nonstatic method needs an object, not a class
Class.Method();
// This will work; we have an instance of the class to call
Class TheObject();
TheObject.Method();
return 0;
}

I understand this, but it's my understanding that a Page object is created
automatically whenever the request is put to the server for the webpage
- so there should be a page object already instantiated.

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.

C# isn't as easy to figure out as the books make it out to be.

Well yeah, and you could use a better coding style, if you''ll pardon me.

I normally do.

REMEMBER: This is just a TOY - not something intended as an end-product.

pram_ph01_ctrl doesn't tell me much about what the variable does.
Consider arrTextControls or m_TextControls or TextControls?

I normally use VERY long variable/reference names - I just didn't do it in
the cases that you're refering to - sorry - I should have used search & substitute
to give those symbols more descriptive names before posting it - my appologies
to you and to anyone else who is reading this. I was just in a hurry to get it posted
- so I can get this problem resolved - and move on to the next problem.

Take a subscription on Safari (http://oreilly.safari.com) which allows you
to scan books and only read the ones that are really good. Way cheaper
than buying real books :)

DAMMIT! Too late to tell me that - I made a quick trip to Barnes & Nobles
this afternoon to grab a few books - I couldn't wait for a response that
might or might not come.

OTOH: I suppose there's no reason why I couldn't go ahead and subscribe anyway
- it would give me more sources to draw from - and you can never have too many.

Sooooo - THANKS!

Working sample follows.

I appreciate the effort - and I have saved it to my forum message folder
(it will be there throughout all eternity as I never delete anything)
- but it doesn't really answer my original question:
How do I invoke those functions/clases/methods within the class methods
without having to pass references as parameters in the arglist?

REMEMBER: This is just a TOY - I'm not really trying to solve specific problems
related to the functioning of my little toy - but to resolve the basic issues
that this toy might raise - and HAS raised. I know that I never did have
this kind of a problem with C++.

Can you tell that this is frustrating the HELL out of me?

... well it IS!


I've been reading my books, and I'll make another post after I get a bit better
educated on this matter. Stay tuned for further developments ...


Thanks Wessel!


- wASP
 
W

Wessel Troost

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.
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()

Greetings,
Wessel
 
W

wASP

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.

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("UserTextBox1");

// 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("UserTextBox2");
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("UserTextBox3");
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
 

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

Top