Where to put server side c# code (in .aspx or in the aspx.cs ?)

M

Mr Flibble

I can embed code within <% %> within my page or put it in the .aspx.cs
file (perhaps within the load method). What are the advantages over each?

Background:
I have some XML and I want to display it in some tables.

In my opinion I can either loop through some XML data and use
Repsonse.write or some other mechanism to populate a table, OR, I can
loop through it via the load method of the associated .aspx.cs file and
use the response.write to build my table. I'm a bit confused as to
where is the better place?
 
R

Richard Brown

Why not take a look at the server side gridview control. You can bind
this directly to an xml file.

You can use this to display and edit(if you want) your xml, without the
need for a custom code.

Re code behind 'v' embeded in the aspx, I think it's down to your
preference personaly I think it's a much nicer separation of logic 'v'
design for this to be in a separate .cs file.
 
M

Mr Flibble

* Richard Brown said:
Why not take a look at the server side gridview control. You can bind
this directly to an xml file.

I have but sadly it's not flexible enough for what I require.
Re code behind 'v' embeded in the aspx, I think it's down to your
preference personaly I think it's a much nicer separation of logic 'v'
design for this to be in a separate .cs file.

Sure I'd like my code in the .cs file where it is seperate from the HTML

At the moment I have (in my HTML):

// some HTML is snipped for brevity

<table border="1">
<%

// some C# code here snipped for brevity
// reader is an XmlReader and is valid..

Response.Write("<tr>");
Response.Write("<td>");
reader.MoveToContent();
string x = reader.Read();
Response.Write(reader.Read());
Response.Write("</td>");
Response.Write("</tr>");


%>
</table>

// more HTML down here...

OK so how could I instead generate this table from the .aspx.cs file
since the thing I cant get my head around is how to be able to insert
the HTML for the table within the correct place in the Response stream.
A call to a public method?

Perhaps I can have in the .aspx file:

<table border="1">
<% // some call to my .aspx.cs file that performs a series of
//Repsonse.Writes
Some_Public_Method_That_Constructs_The_Table();
<table>

But so far I've not worked out how to call code within the aspx.cs file
from within the HTML of the .aspx file.

Am I barking up the wrong tree?
 
M

Mr Flibble

* Mr Flibble said:
<table border="1">
<% // some call to my .aspx.cs file that performs a series of
//Repsonse.Writes
Some_Public_Method_That_Constructs_The_Table();
<table>

OK I managed to get it working by just trying exactly what I posted!

The definition of the table is now in the .aspx and the code that
populates it is in the .aspx.cs.
 
W

Will Asrari

You can use a label if you want to have control of the output.

..aspx page

<table class="body_table">
<asp:Label id ="_lblXml" runat="server" />
</table>

codebehind.cs

for (int i = 0; i < # of iterations; i++)
{
_lblXml.Text = "<tr>" + Environment.NewLine;
_lblXml.Text += "<td>" + _var1 + "</td>" + Environment.NewLine;
_lblXml.Text += "<td>" + _var2 + "</td>" + Environment.NewLine;
_lblXml.Text += "</tr>" + Environment.NewLine;
}

I hope this helps. You could also explore the GridView, DataList, or
Repeater as has already been suggested.
 
R

Richard Brown

I assume you want to build your table from your xml at page load and
insert it into your page somewhere. (correct me if I have the wrong end
of the stick?)

If so you could also take a look at the asp:literal control, add one to
your page where you want to insert your table.

In your page_load (or whereever) build your table html from your xml,
then set the literal control text to the html generated.

i.e.

..aspx

<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<!--StartFragment -->
<pre>
<asp:Literal ID="myTable" Runat="server"></asp:Literal>
</pre>
</form>
</body>

..cs
private void Page_Load(object sender, System.EventArgs e)
{
string s =
"<table><tr><td>row1:col1</td><td>row1:col2</td></tr><tr><td>row2:col1</td><td>row2:col2</td></tr></table>";
myTable.Text = s;
}
 
M

Mr Flibble

* Richard Brown said:
I assume you want to build your table from your xml at page load and

Hi Richard thanks for your comments.
insert it into your page somewhere. (correct me if I have the wrong end
of the stick?)


This is correct.

If so you could also take a look at the asp:literal control, add one to
your page where you want to insert your table.

What is the advantage to this than just having a method call within the
<% %> embeded in the page? Do I gain anything by having a control
handle the output?
 
R

Richard Brown

What is the advantage to this than just having a method call within the
<% %> embedded in the page? Do I gain anything by having a control
handle the output?

It's a very good question, and I have to say the only answer I could
come up with is because it's the method that best fits the asp.net OO
approach.

Both methods will produce the same result and the Response.Write is
probably going to be faster (unless maybe you are doing a lot of heavy
string concatenation).

I found one other discussion on this topic in the group which makes
interesting reading (ish).

See:
http://groups.google.co.uk/group/mi...+response.write&rnum=1&hl=en#f19bffd6928c37d2

Richard
 
W

Will Asrari

What is the advantage to this than just having a method call within the <% %> embeded in the page? Do I gain anything by having a control handle the output?

You should always try to separate code from content.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top