Hello guys, I wonder if anybody could clarify this for me. I've created a new website (File > New > Web site) and as we all know I now have a aspx file (Default.aspx) and a aspx.cs file (Default.aspx.cs). The aspx file contains a form and when submitted, it sends some data to a small SQL table. What I would like to do is to create another page (web page, aspx file - I don't know, whatever) to display the data saved in the SQL table, so that I essentially end up with two pages: one which allows you to submit the info to the database and another one that displays that content. I have to say that my knowledge of asp.net, visual studio and SQL is very basic, so I tried to add a new item by clicking on the project (I seem to understand that a web form is another aspx file) but there is a problem: ideally, I will need to use the same aspx.cs file (with the addition of the relevant code to display the database content of course) to retrieve the information from the SQL table rather than write the C# code in a separate aspx.cs file. Is that possible? I kind of assumed it was and so in my second aspx file (Results.aspx) I included ...CodeFile="Default.aspx.cs" Inherits="Default"... so that it uses the same aspx.cs file, but the compiler didn't like it. So, my assumption might be wrong after all. Anyway, any suggestion as to what to do to achieve the above? Am I heading in the completely wrong direction?
thanks

Recommended Answers

All 7 Replies

Am I heading in the completely wrong direction?

If both pages share code, IMO the best option would be to create a class that does the actual work. You can then use that class in both pages.

An other option would be to create a base page (parent page class), from which both your pages will be derived. You can do that manually by creating a new page, and in the other two pages replace the parent class in the aspx.cs file.

Thanks pritaeas, well I'm not sure if they share code to be honest, but the idea of classes seems a good one. OK so, I have my two aspx pages Default.aspx and Results.aspx: the first uploads content to the database so I'll create a upload class file and since the second one retrieve it, I'll create a retrieve class file. Both the aspx files are linked to the same partial class (Defaut.aspx.cs, but how do I link 2 aspx files to the same aspx.cs file?) and in there on page load I can call the two separate classes, does it sound reasonable?

If you add a new class to your project, then both aspx pages will know it exists. You can then create an instance in the page load and use it where needed.

I don't think you want to link two aspx pages to the same cs file, I don't really see why you'd want that.

I don't think you want to link two aspx pages to the same cs file, I don't really see why you'd want that

agreed... that's not how asp.net works. Each of these .cs files are also known as the code-behind pages. Their purpose if for you to be able seperate the presentation and logic layer of your web page. The code-behind page is not required as you can add your vb/c# code in the aspx page within <script runat="server"> instead. However, it "looks" cleaner if you keep your code in a seperate file (.aspx.cs) rather than in the same page.

So, the class is a good idea, but if your project is going to have two pages total, im not sure that creating a class for this purpose is going to buy you a whole lot of time/efficiency.

I hope that helps...

OK thanks guys, so back to square one then. Sorry but this isn't yet clear in my head. Page Default.aspx has a reference to Default.aspx.cs where the code that uploads info onto the database resides. As JorgeM pointed out I know I could include the C# code in the aspx page but I prefer to have it in its own aspx.cs file for clarity. Then page Results.aspx will retrieve the info from the database, so if there is no much point in using classes due to the size of my project, is it OK to use another aspx.cs file, which Results.aspx referes to and which contains the code to retrieve stuff from the database? Is this approach better?
The reason why I asked whether it was possible to have one aspx.cs file for two aspx pages was because in my head I thought that it would have been more manageable and clearer

Yes that's a better approach. Every aspx page should have its own aspx.cs page. Aspx is for your presentation and aspx.cs is for your code.

You should get yourself a good book on beginning asp.net. I like the books from apress.

OK thanks, yep I've read already a beginner book, ASP.NET 3.5 A beginners Guide by William B. Sanders, as I think I said somewhere in another post, but funnily enough there were never multiple pages in the examples :-)!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.