473,659 Members | 2,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2076
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.MoveToCo ntent();
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_Met hod_That_Constr ucts_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_Met hod_That_Constr ucts_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_tab le">
<asp:Label id ="_lblXml" runat="server" />
</table>

codebehind.cs

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

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(objec t sender, System.EventArg s e)
{
string s =
"<table><tr><td >row1:col1</td><td>row1:col 2</td></tr><tr><td>row2 :col1</td><td>row2:col 2</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
526
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 the wierd part. Say their are 500 records in the file. If I run the process once, maybe 250 will appear. If I run it a second time, maybe 400 or all the records will appear.
3
1591
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 on the html button the OnComplete function is called.
11
1252
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 rk.aspnet&sMessageID=%253C7d8d01c3b4a1%2524412f7770% 2524a601280a@phx.gbl%253E The problem has not fixed till now.
4
3505
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 "Client and Server Scripting in Web Pages" but it only shows server side scripting that is written in HTML in mypage.aspx and not script from mypage.aspx.vb. How can I pass a variable value from mypage.aspx.vp to my client side script?
3
13132
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 how to do this, I created the script to do it and thought I would share it. Its a littel messy but does the job. If anyone has a better solution, please let me know. //Client Site Event Handler to put in Page_Load event...
2
2239
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, both need to run some code and then redirect the user somewhere else. The problem is the redirected page is being displayed in the banner frame. How do I tell either Response.Redirect or Server.Transfer what frame to display the page in or that the...
3
2940
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 say “www.Test1.com” and “www.Test2.com” pointing to a single website. Website content is located onto UNCPath i.e. remote location. Domain 1: www.Test1.com points to \\servername\websitefolder\homedirectory
3
2071
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 :) Actually, I am want to raise an event at client side and want to perform some action on the server side when that event at client is triggered. So here is what I am trying to do, <%@ Page Language="C#" AutoEventWireup="true"...
13
3543
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 some javascript code that calls window.open. I pass the resource url of page B to Page A's window.open call. Page B is then loaded and executed but none of the server-side code is rendered. If I view the source of the page, the code (and page...
0
8427
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8330
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8523
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8626
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6178
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.