codebehind question

K

Karl Hungus

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
 
S

Scott M.

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

Karl Hungus

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

Scott M.

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
 
M

Martha[MSFT]

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

Karl Hungus

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

Scott M.

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

Karl Hungus

Excellent. that helps alot.

Thanks!


Scott M. said:
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.
 
K

Kevin Spencer

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

Karl Hungus

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

Kevin Spencer

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.
 

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

Members online

Forum statistics

Threads
473,787
Messages
2,569,631
Members
45,338
Latest member
41Pearline46

Latest Threads

Top