Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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
<%@ Page language="c#" Src="test.cs" %>
 
test.cs
//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 9-Aug-12 6:07am
Xaltus124

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

<![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:
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.
  Permalink  
v2
Comments
Xaltus at 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
0 CPallini 355
1 BillWoodruff 334
2 George Jonsson 279
3 Sergey Alexandrovich Kryukov 243
4 Gihan Liyanage 225
0 OriginalGriff 5,050
1 CPallini 4,225
2 Sergey Alexandrovich Kryukov 3,639
3 George Jonsson 2,911
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 9 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100