locked
Code c# doesnt work within an aspx page RRS feed

  • Question

  • User795102932 posted

    I am using classic asp with precompiled site, and I want to execute c # code on an aspx page, but when I want to do some code inside the script tags it doesn't work.

    Within the script tags it does not recognize the C # language,

    maybe it can be the web.config or something in the IDE.
    I need help with this.
    Thanks

    <%@ page language="C#" masterpagefile="~/Master/Classic/Home.master" autoeventwireup="true" inherits="LoginPage, WebClient4"  %>
    <%@ Register TagPrefix="dgs" Namespace="WebClient4.Controls" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title>Sports</title>
                <meta http-equiv="refresh" content="0; URL="/>
            </head>
    
            <body>
                <dgs:LoginForm runat="server" ID="ctlLogin" SkinID="crlLoginForm" /> 
            </body> 
        </html>
    </asp:Content>
    
    
    <script language="CS" runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Context.Request.Form["Account"] != null && Context.Request.Form["Account"] != string.Empty)
            {
                string userName = Context.Request.Form["Account"];
            }
    
            if (Context.Request.Form["Password"] != null && Context.Request.Form["Password"] != string.Empty)
            {
                string password = Context.Request.Form["Password"];
            }
        }
    </script>

    Thursday, September 5, 2019 4:08 PM

All replies

  • User-719153870 posted

    Hi Irnel,

    Irnel

    I am using classic asp with precompiled site,

    Irnel

    Within the script tags it does not recognize the C # language,

    As far as i know, you can't use c# in classic asp project.

    You can refer to below links and find the same answer as i told. Why not try asp.net?

    How could I use C# for Classic ASP programming?

    Difference between asp and asp.net 

    Best Regard,

    Yang Shen

    Friday, September 6, 2019 1:33 AM
  • User795102932 posted

    Hi Yang

    I was searching the internet and exactly a lot of documentation says the same thing you say to me.

    But I have another precompiled site, almost exactly the same with the same structure, and if I can use c # within a script, then that's why I launched the question in the forum.

    I was reading about code behind and inline code, so what I want to use is inline code.

    Maybe it was my mistake and it was not an asp classic site, maybe it is an asp.net web forms site, because in the application bin there are several .dll also use master page etc.

    And my question would be because in one place I can use it and not in another. I come from the world of asp.net mvc, and these things are a bit strange to me.

    Friday, September 6, 2019 3:08 PM
  • User753101303 posted

    Hi,

    Classic ASP is something else and seems irrelevant here.

    What means "doesn't work"? Always tell what happens instead. Unless you can't reach a breakpoint with the debugger (which could have other causes), you have no way to know if this code works as it doesn't produce any visible effect? You just assign values to block level variables.

    If you have an error message, it could be also that the script block needs to be inside the Content tag. I'll try when back at home...

    Friday, September 6, 2019 4:10 PM
  • User795102932 posted

    Hi

    Now reviewing better I am completely sure that it is a precompiled site (ASP.NET Web Forms).

    I understand what you tell me, and you're right. When I say that the code does not work,

    You can look at the original post and see the photo I put with the code. If you look there is a c # code inside the tags

    <script language="CS" runant="server">

             .... block code c #

             1- I cannot delcate a variable

             2 - I can't declare a class

            3 - I cannot declare a method (Page_Load (....))

    </script>

    So my problem is because the C # language is not recognized within the script.

    Friday, September 6, 2019 4:21 PM
  • User409696431 posted

    CS is not a language, it is a file extension for C# code-behind pages.  C# is the language.  And a script tag needs runat="server" (not runant) to run server-side languages.

    For example:

    <script runat="server" language="C#">
       void MyButton_OnClick(Object sender, EventArgs e)
       {
          MyLabel.Text = MyTextbox.Text.ToString();
       }
    </script>

    Saturday, September 7, 2019 4:23 AM
  • User795102932 posted

    Hi KathyW

    <script runat="server" language="C#">
       void MyButton_OnClick(Object sender, EventArgs e)
       {
          MyLabel.Text = MyTextbox.Text.ToString();
       }
    </script>

    I am sorry for the errors in the code I wrote it by hand, but the error is not when declaring the script.

    You have an onclik (method) event inside the script. My problem is that I cannot declare anything, neither a variable nor and method etc. He doesn't recognize the c # language, and my question is why will that be, I'm supposedly trying to use inline code

    Look at this code that I have on the login.aspx page

    <%@ page language="C#" masterpagefile="~/Master/Classic/Home.master" autoeventwireup="true" inherits="LoginPage, WebClient4"  %>
    <%@ Register TagPrefix="dgs" Namespace="WebClient4.Controls" %>
    
    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <%@ Import Namespace="System.Configuration" %>
    <%@ Import Namespace="System.ComponentModel" %>
    <%@ Import Namespace="System.Collections.Generic" %>
    <%@ Import Namespace="System.Runtime.Remoting" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title>Sports</title>
                <meta http-equiv="refresh" content="0; URL="/>
            </head>
    
            <body>
                <dgs:LoginForm runat="server" ID="ctlLogin" SkinID="crlLoginForm" /> 
            </body> 
        </html>
    </asp:Content>
    
    <script language="C#" runat="server">
       
        // code c # here is not recognized
    
    </script>

    Saturday, September 7, 2019 12:44 PM
  • User409696431 posted

    Since you haven't posted your C# code, it would impossible to tell you what is wrong.  Post the smallest example you can of something that doesn't work.

    Saturday, September 7, 2019 12:51 PM
  • User475983607 posted

    The page directive specifically inherits from LoginPage and WebClient4 which is were you will find the event handlers.  Not sure why you are inheriting form the user control...

    <%@ page language="C#" masterpagefile="~/Master/Classic/Home.master" autoeventwireup="true" inherits="LoginPage, WebClient4"  %>

    You need to make a design decision.  Do you want to use code behind or not?  If not, then change the page directive.

    <%@ Import Namespace="System" %>
    <%@ Page Language="c#"%>

    https://support.microsoft.com/en-us/help/976112/introduction-to-asp-net-inline-expressions-in-the-net-framework

    Saturday, September 7, 2019 1:02 PM
  • User795102932 posted

    Hi mgebhard

    It is not my decision to work with inline code, because I have a precompiled site, I do not have access to the code behind. only DLL, in this case what I want is to change some behavior and execute some methods from the login.aspx page or any other one that I need, and the only way to do this as I have read is through inline code. The problem that I have raised in the forum has been not the architecture or the decision to use one thing or the other, but because the code c # is not recognized within the script. I have another practically identical precompiled site and if I can use inline code.

    Saturday, September 7, 2019 1:25 PM
  • User795102932 posted

    Sorry, I have been responding to the people who have commented

    I have a precompiled site (ASP.NET web Forms) and I don't have access to the code behind, so I want to handle different behaviors in the aspx pages, login.aspx etc where I need it, so I have read that the only way to have c # code in one aspx page is with inline code.

    therefore I need to have c # code in an aspx page, now the problem is when I try to use the c # language within the script tag, it doesn't recognize it. That is the real problem.

    <script language="C#" runat="server">
       
        // code c # here is not recognized
    
    </script>


    Saturday, September 7, 2019 1:48 PM
  • User475983607 posted

    By default the page class are defined as a partial class. 

    public partial class _default : System.Web.UI.Page
    {

    Create a partial class and add the logic you need. 

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods

    Saturday, September 7, 2019 2:17 PM
  • User-719153870 posted

    Hi Irnel,

    Look at this code that I have on the login.aspx page

    <%@ page language="C#" masterpagefile="~/Master/Classic/Home.master" autoeventwireup="true" inherits="LoginPage, WebClient4"  %>
    <%@ Register TagPrefix="dgs" Namespace="WebClient4.Controls" %>
    
    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <%@ Import Namespace="System.Configuration" %>
    <%@ Import Namespace="System.ComponentModel" %>
    <%@ Import Namespace="System.Collections.Generic" %>
    <%@ Import Namespace="System.Runtime.Remoting" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <title>Sports</title>
                <meta http-equiv="refresh" content="0; URL="/>
            </head>
    
            <body>
                <dgs:LoginForm runat="server" ID="ctlLogin" SkinID="crlLoginForm" /> 
            </body> 
        </html>
    </asp:Content>
    
    <script language="C#" runat="server">
       
        // code c # here is not recognized
    
    </script>

    I have tested your code in a web forms site project both in .NET Framework 4.8 and 2.0, it will both throw errors. Did you get any error?

    First, i will need to put CodeFile="LoginPage.aspx.cs" in <% page ...%>;

    Second, i will need to remove the second parameter in inherits;

    Finally, the <html>,<head> and <body> tags in <asp:content> will also need to be deleted, or the page will keep refreshing.

    If you get any similar or any error message?

    Or, you may need to share us your master page code and your LoginPage latest code to see where you put your content1.

    Also, this article shows how to use inline code in ASP.NET, which you can refer to.

    Best Regard,

    Yang Shen 

    Monday, September 9, 2019 8:35 AM