473,395 Members | 2,713 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,395 software developers and data experts.

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

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?
May 22 '06 #1
8 2059
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.

May 22 '06 #2
* Richard Brown wrote:
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?
May 22 '06 #3
* Mr Flibble wrote:
<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.
May 22 '06 #4
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.

May 22 '06 #5
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;
}

May 23 '06 #6
* Richard Brown wrote:
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?
May 23 '06 #7
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/mic...9bffd6928c37d2

Richard

May 23 '06 #8
> 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.

May 23 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: jabailo | last post by:
I wrote a program that loops through a file of records. It parses each line in the file and sends them to a web service that inserts them into an AS400DB2 database using Asynch calls. This is...
3
by: Reetu | last post by:
Hi All, I have an html button on .aspx page. <INPUT class="sbttn" id="btnComplete0" onclick="onComplete ()" type="button" value="Mark Completed" name="btnComplete0"> When the user clicks...
11
by: Tom | last post by:
Hi all, I posted the same question one week ago in http://communities.microsoft.com/newsgroups/previewFrame.as p? ICP=msdn&sLCID=us&sgroupURL=microsoft.public.dotnet.framewo...
4
by: Bob T | last post by:
Hi All, I am trying to pass a variable from my VB asp.net script (from for example Sub Page_Load in mypage.aspx.vb) to my Client side script. I have found and looked at a very good example...
3
by: Earl Teigrob | last post by:
I wanted my "Terms and Conditions" Checkbox control to participate in my ASP.NET validation just like all the the other controls on the page. After some time of searching the web for an example of...
2
by: Chris Podmore | last post by:
This is driving me mad so any help will be much appreciated. I have an html page with 3 frames, banner, left and right. The banner frames source is an aspx page. The aspx page has two buttons,...
3
by: Purti Malhotra | last post by:
Hi All, In our Web hosting environment we are using Virtual hosting i.e. multiple websites are on one server and multiple domains are pointing to a single website. Issue: We have two domains...
3
by: rolf.oltmans | last post by:
Hello, I've been trying to access the html element "Body" in my server side code. I've searched the archives of this group and have been able to put together an example that doesn't work :)...
13
by: Bob Jones | last post by:
Here is my situation: I have an aspx file stored in a resource file. All of the C# code is written inline via <script runat="server"tags. Let's call this page B. I also have page A that contains...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.