473,434 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

codebehind question

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
12 1624
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
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
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
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
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

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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: A.M | last post by:
Hi, Using VS.NET 2003, If i use SRC page attribute instead of CodeBehind, do i still have intelisence and generally IDE support for that? Thanks, Ali
3
by: jm | last post by:
I have a question about codebehind. I have an upload namespace, class and all in a codebehind. I currently have it so it works with one of my .aspx pages. I want to use it for another .aspx...
4
by: Rob Shorney | last post by:
Hi, I am using .Net 2003 , c# asp.net. The situation i have is this. I have a asp.net page which in the codebehind maintains an xml document in memory. The user can click on a button to popup a...
6
by: Thomas Andersson | last post by:
Hi all, I've been thinking of about adding new languages to our website and is currently investigating how this could be done. And I know that one way to go would be to create new aspx-pages...
1
by: Randall Parker | last post by:
Using VS 2003 and Cassini web server. I'm new to ASP.Net and so this may be a dumb question. I'm getting an error where the type 'FarmLand.WebForm1' is not found. The Codebehind C# source file...
6
by: WT | last post by:
Hello, I am searching for a way to generate automatically from codebehind the <!Doctype....for asp.net pages using .net 3.5 c# and vs2008. Subidiary question: if I do a server transfert in my...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.