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

codebehind question

P: n/a
If I use a code behind class for an aspx page, what is the best way to get
data from the codebehind class into my aspx page?

I know about databinding, but is there a more basic way of just referencing
the variables or calling getters?

thanks in advance
Nov 18 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Just drop a label (or any control that support text/data representation).
For a control that displays text (like a textbox or label), just set the
text property of the control to the value from the codebehind. For
something like a checkbox or radiobutton, you'd set the checked property of
the control based on the boolean value in the codebehind.
"Karl Hungus" <nn*********@hotmail.com> wrote in message
news:ud***********************@twister.nyc.rr.com. ..
If I use a code behind class for an aspx page, what is the best way to get
data from the codebehind class into my aspx page?

I know about databinding, but is there a more basic way of just referencing the variables or calling getters?

thanks in advance

Nov 18 '05 #2

P: n/a
OK, thats clear. But how do I reference the value of the codebehind.

lets say I have a codebehind object, that I instantiate in the aspx page
within the pageload event like

void Page_Load(Object Src, EventArgs E) {

XMLManager xman = new XMLManager();
xman.ReadXML();

}

within the XMLManager class I have a

public String testStr;

which I set....how do I retrieve that value?
"Scott M." <s-***@BADSPAMsnet.net> wrote in message
news:uN**************@TK2MSFTNGP12.phx.gbl...
Just drop a label (or any control that support text/data representation).
For a control that displays text (like a textbox or label), just set the
text property of the control to the value from the codebehind. For
something like a checkbox or radiobutton, you'd set the checked property of the control based on the boolean value in the codebehind.

Nov 18 '05 #3

P: n/a
Don't think of it as "pulling" the value from the codebehind to the aspx
page. Instead "push" the value from the codebehind to the aspx page.

In other words, you wouldn't need to grab the codebehind value testStr from
the aspx page. Instead, place a label (for example) on the aspx page and in
the Page_Load event of the codebehind place the string value in the label.

In the .aspx page:

<ASP:LABEL ID="lblDataHolder" RUNAT="Sever"></ASP:LABEL>

In the codebehind:

void Page_Load(Object Src, EventArgs E) {

XMLManager xman = new XMLManager();
xman.ReadXML();

//This takes the public string value in the class and places it into the
label on the web page
lblDataHolder.Text = xman.testStr;

}

-Scott

"Karl Hungus" <nn*********@hotmail.com> wrote in message
news:Ek***********************@twister.nyc.rr.com. ..
OK, thats clear. But how do I reference the value of the codebehind.

lets say I have a codebehind object, that I instantiate in the aspx page
within the pageload event like

void Page_Load(Object Src, EventArgs E) {

XMLManager xman = new XMLManager();
xman.ReadXML();

}

within the XMLManager class I have a

public String testStr;

which I set....how do I retrieve that value?
"Scott M." <s-***@BADSPAMsnet.net> wrote in message
news:uN**************@TK2MSFTNGP12.phx.gbl...
Just drop a label (or any control that support text/data representation). For a control that displays text (like a textbox or label), just set the
text property of the control to the value from the codebehind. For
something like a checkbox or radiobutton, you'd set the checked property

of
the control based on the boolean value in the codebehind.


Nov 18 '05 #4

P: n/a
The Page_Load method should be placed in the code behind file. Since your
..aspx file inherits from the code behind it will have access to all the
fields of the code behind. Why do you need to access variables in your .aspx
file anyway? The whole point is not to mix the presentation from the
programmatic logic.
I think you should get a clear concept of code-behind. Place all your
controls in the .aspx file and event handlers in the code-behind file. From
the event handlers you can do your xml processing.

Did I answer your question or did I miss it altogether?

"Karl Hungus" <nn*********@hotmail.com> wrote in message
news:Ek***********************@twister.nyc.rr.com. ..
OK, thats clear. But how do I reference the value of the codebehind.

lets say I have a codebehind object, that I instantiate in the aspx page
within the pageload event like

void Page_Load(Object Src, EventArgs E) {

XMLManager xman = new XMLManager();
xman.ReadXML();

}

within the XMLManager class I have a

public String testStr;

which I set....how do I retrieve that value?
"Scott M." <s-***@BADSPAMsnet.net> wrote in message
news:uN**************@TK2MSFTNGP12.phx.gbl...
Just drop a label (or any control that support text/data representation). For a control that displays text (like a textbox or label), just set the
text property of the control to the value from the codebehind. For
something like a checkbox or radiobutton, you'd set the checked property

of
the control based on the boolean value in the codebehind.


Nov 18 '05 #5

P: n/a
OK. that helps alot. Thanks!
"Scott M." <s-***@BADSPAMsnet.net> wrote in message
news:uM**************@TK2MSFTNGP12.phx.gbl...
Don't think of it as "pulling" the value from the codebehind to the aspx
page. Instead "push" the value from the codebehind to the aspx page.

In other words, you wouldn't need to grab the codebehind value testStr from the aspx page. Instead, place a label (for example) on the aspx page and in the Page_Load event of the codebehind place the string value in the label.

In the .aspx page:

<ASP:LABEL ID="lblDataHolder" RUNAT="Sever"></ASP:LABEL>

In the codebehind:

void Page_Load(Object Src, EventArgs E) {

XMLManager xman = new XMLManager();
xman.ReadXML();

//This takes the public string value in the class and places it into the
label on the web page
lblDataHolder.Text = xman.testStr;

}

Nov 18 '05 #6

P: n/a

I get what youre saying.

Im coming from a JSP background where it was fairly standard to instantiate
one or more helper objects in a JSP page. Then using those objects methods,
youd set variables on the page and populate the html with them. One really
common thing was to write some database connection class that returned a
resultSet and then loop through that to spit out a list of something in the
html.

Anyway...I guess whats confusing me is if I wanted to have several objects.
Say I needed to hit the db and email something on the same aspx page. I
write helper objects for the db and emailing. Then what do I do? place both
classes in one codebehind file? or can I access other cs files from my
codebehind file? Im used to writing one class per file, like in java.

thanks for your reply.
"Martha[MSFT]" <ma******@online.microsoft.com> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...
The Page_Load method should be placed in the code behind file. Since your
.aspx file inherits from the code behind it will have access to all the
fields of the code behind. Why do you need to access variables in your ..aspx file anyway? The whole point is not to mix the presentation from the
programmatic logic.
I think you should get a clear concept of code-behind. Place all your
controls in the .aspx file and event handlers in the code-behind file. From the event handlers you can do your xml processing.

Did I answer your question or did I miss it altogether?

Nov 18 '05 #7

P: n/a
Each codebehind is one class. But you could create your data class and
instantiate it and use it from your codebehind class.

Think of the codebehind as the programming logic for the web page. In that
logic, you can create instances of any other classes you want to get the job
done. The codebehind also provides a rich set of event handlers for the
controls place on the page as well as the page object itself.
"Karl Hungus" <nn*********@hotmail.com> wrote in message
news:s9****************@twister.nyc.rr.com...

I get what youre saying.

Im coming from a JSP background where it was fairly standard to instantiate one or more helper objects in a JSP page. Then using those objects methods, youd set variables on the page and populate the html with them. One really
common thing was to write some database connection class that returned a
resultSet and then loop through that to spit out a list of something in the html.

Anyway...I guess whats confusing me is if I wanted to have several objects. Say I needed to hit the db and email something on the same aspx page. I
write helper objects for the db and emailing. Then what do I do? place both classes in one codebehind file? or can I access other cs files from my
codebehind file? Im used to writing one class per file, like in java.

thanks for your reply.
"Martha[MSFT]" <ma******@online.microsoft.com> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...
The Page_Load method should be placed in the code behind file. Since your .aspx file inherits from the code behind it will have access to all the
fields of the code behind. Why do you need to access variables in your

.aspx
file anyway? The whole point is not to mix the presentation from the
programmatic logic.
I think you should get a clear concept of code-behind. Place all your
controls in the .aspx file and event handlers in the code-behind file.

From
the event handlers you can do your xml processing.

Did I answer your question or did I miss it altogether?


Nov 18 '05 #8

P: n/a

Excellent. that helps alot.

Thanks!
"Scott M." <s-***@BADSPAMsnet.net> wrote in message
news:OT**************@TK2MSFTNGP10.phx.gbl...
Each codebehind is one class. But you could create your data class and
instantiate it and use it from your codebehind class.

Think of the codebehind as the programming logic for the web page. In that logic, you can create instances of any other classes you want to get the job done. The codebehind also provides a rich set of event handlers for the
controls place on the page as well as the page object itself.
"Karl Hungus" <nn*********@hotmail.com> wrote in message
news:s9****************@twister.nyc.rr.com...

I get what youre saying.

Im coming from a JSP background where it was fairly standard to

instantiate
one or more helper objects in a JSP page. Then using those objects

methods,
youd set variables on the page and populate the html with them. One really
common thing was to write some database connection class that returned a
resultSet and then loop through that to spit out a list of something in

the
html.

Anyway...I guess whats confusing me is if I wanted to have several

objects.
Say I needed to hit the db and email something on the same aspx page. I
write helper objects for the db and emailing. Then what do I do? place

both
classes in one codebehind file? or can I access other cs files from my
codebehind file? Im used to writing one class per file, like in java.

thanks for your reply.
"Martha[MSFT]" <ma******@online.microsoft.com> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...
The Page_Load method should be placed in the code behind file. Since

your .aspx file inherits from the code behind it will have access to all the fields of the code behind. Why do you need to access variables in your

.aspx
file anyway? The whole point is not to mix the presentation from the
programmatic logic.
I think you should get a clear concept of code-behind. Place all your
controls in the .aspx file and event handlers in the code-behind file.

From
the event handlers you can do your xml processing.

Did I answer your question or did I miss it altogether?



Nov 18 '05 #9

P: n/a
Several people have given you answers that dance around the answer here, so
I'll give it to you straight: The CodeBehind class IS the page. Actually,
the Page Template INHERITS the CodeBehind class. So, the question is moot.
If data is in the CodeBehind class, it is already IN the Page.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Karl Hungus" <nn*********@hotmail.com> wrote in message
news:ud***********************@twister.nyc.rr.com. ..
If I use a code behind class for an aspx page, what is the best way to get
data from the codebehind class into my aspx page?

I know about databinding, but is there a more basic way of just referencing the variables or calling getters?

thanks in advance

Nov 18 '05 #10

P: n/a
Thats very helpful to understand it that way.

I guess the thing for me is it seems like using codebehind is like an
include - which always seemed like faking it. Im used to doing the
separation on more of an object level...like I mentioned earlier, using
custom objects and then instantiating them within jsp -- more like
compositing instead of inhertance.
"Kevin Spencer" <ke***@takempis.com> wrote in message
news:eT**************@TK2MSFTNGP10.phx.gbl...
Several people have given you answers that dance around the answer here, so I'll give it to you straight: The CodeBehind class IS the page. Actually,
the Page Template INHERITS the CodeBehind class. So, the question is moot.
If data is in the CodeBehind class, it is already IN the Page.

Nov 18 '05 #11

P: n/a
Understood. Inheritance is a very powerful aspect of OOP. When you inherit a
class in another class, the class which inherits becomes the other class.
Think of it like a basic car. Every car inherits "car" which means that it
has an engine, wheels, etc. Once you've created a new class that inherits
another, you can add properties to it, such as adding air conditioning to
the "car" class to create the "airconditioned car" class. The Page Template
inherits the CodeBehind class, which means that it IS the CodeBehind class.
The additional HTML and other code in the Template are like the air
conditioning - part of the derived class which doesn't exist in the base
class. Incidentally, the CodeBehind class inherits System.Web.UI.Page, which
makes it a Page with extra stuff added as well.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Karl Hungus" <nn*********@hotmail.com> wrote in message
news:7F****************@twister.nyc.rr.com...
Thats very helpful to understand it that way.

I guess the thing for me is it seems like using codebehind is like an
include - which always seemed like faking it. Im used to doing the
separation on more of an object level...like I mentioned earlier, using
custom objects and then instantiating them within jsp -- more like
compositing instead of inhertance.
"Kevin Spencer" <ke***@takempis.com> wrote in message
news:eT**************@TK2MSFTNGP10.phx.gbl...
Several people have given you answers that dance around the answer here,

so
I'll give it to you straight: The CodeBehind class IS the page. Actually, the Page Template INHERITS the CodeBehind class. So, the question is moot. If data is in the CodeBehind class, it is already IN the Page.


Nov 18 '05 #12

P: n/a
inheritance is the less of the two, compared with compositing. Most design
patterns are based on composites rather than inheritance.

I would rather have my page be "composed of" than "is a" relationship.

http://www.javaworld.com/jw-11-1998/...echniques.html


"Kevin Spencer" <ke***@takempis.com> wrote in message
news:uY**************@tk2msftngp13.phx.gbl...
Understood. Inheritance is a very powerful aspect of OOP. When you inherit a class in another class, the class which inherits becomes the other class.
Think of it like a basic car. Every car inherits "car" which means that it
has an engine, wheels, etc. Once you've created a new class that inherits
another, you can add properties to it, such as adding air conditioning to
the "car" class to create the "airconditioned car" class. The Page Template inherits the CodeBehind class, which means that it IS the CodeBehind class. The additional HTML and other code in the Template are like the air
conditioning - part of the derived class which doesn't exist in the base
class. Incidentally, the CodeBehind class inherits System.Web.UI.Page, which makes it a Page with extra stuff added as well.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Karl Hungus" <nn*********@hotmail.com> wrote in message
news:7F****************@twister.nyc.rr.com...
Thats very helpful to understand it that way.

I guess the thing for me is it seems like using codebehind is like an
include - which always seemed like faking it. Im used to doing the
separation on more of an object level...like I mentioned earlier, using
custom objects and then instantiating them within jsp -- more like
compositing instead of inhertance.
"Kevin Spencer" <ke***@takempis.com> wrote in message
news:eT**************@TK2MSFTNGP10.phx.gbl...
Several people have given you answers that dance around the answer
here,
so
I'll give it to you straight: The CodeBehind class IS the page.

Actually, the Page Template INHERITS the CodeBehind class. So, the question is moot. If data is in the CodeBehind class, it is already IN the Page.



Nov 18 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.