Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been struggling with what seemed like an easy task in asp.net/c# for longer than I would like. I thought this would be easy, since I understand the syntax of C/C++, JavaScript, VBScript, and VB.Net.

I have an aspx page and I would like to include a c# source file that defines and calls a function that uses HTTP Response. I do not want to have any of this code on the aspx page because there is no syntax coloring for c# embedded on an aspx page in Notepad++.

Any help would be appreciated. Here's some pseudo code to help you understand where I'm trying to end up:

test.aspx
ASP.NET
<%@ Page language="c#" Src="test.cs" %>


test.cs
C#
//Import
using System.Web.HttpContext.Current.Response;

//Define
public class myclass()
{
    public void myfunction()
    {

        Response.Write("Hello World");
    }
}

//Call
myclass::myfunction();


Also, do you -have- create an object from a class before accessing the members of it? C++ allows direct access to a classes' functions via the :: operator.
Posted

1 solution

ASP.NET
<![CDATA[<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
</head>
<body>
    <form id="form1"  runat="server">   
    
    </form>
</body>
</html>


Your csharp code will be:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

Remember that .NET uses OOP paradigm =)

May be this helps you: http://www.asp.net/web-pages[^]

Hope you find it useful.
 
Share this answer
 
v2
Comments
Xaltus 16-Aug-12 11:15am    
Hi. Thanks for your reply. Just a quick note:

This did not work and came back with an error:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

It says it couldn't inherit WebApplication1.WebForm1

However, this does work:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Src="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>


Also, it said that System.Linq is not a valid namespace, so I removed it. This works like a charm. Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900