Running C# code in aspx file

Discussion in 'ASP .Net' started by Mario, May 25, 2010.

  1. Mario

    Mario Guest

    The row below:

    <asp:Label runat="server" ID="Label4" Text="<%#
    System.Math.Ceiling((double)(Container.TotalRowCount)/(double)Container.PageSize)
    %>" />

    And it works fine, but I wonder, how to write similar code when I don't have
    code completion between <% %> tag ? Code above I've finished in .cs file,
    after I saw similar example, then I copied in a aspx file.
     
    Mario, May 25, 2010
    #1
    1. Advertising

  2. On May 25, 2:34 pm, "Mario" <> wrote:
    > The row below:
    >
    > <asp:Label runat="server" ID="Label4" Text="<%#
    > System.Math.Ceiling((double)(Container.TotalRowCount)/(double)Container.Pag eSize)
    > %>" />
    >
    > And it works fine, but I wonder, how to write similar code when I don't have
    > code completion between  <% %> tag ? Code above I've finished in .cs file,
    > after I saw similar example, then I copied in a aspx file.


    You can add your code between <script></script> tags in the asx page

    <script runat="server">
    Sub Page_Load(sender As Object, e As EventArgs)
    Label4.Text = "Test"
    End Sub
    </script>

    For more information about Code Behind and Code Inline look here
    http://quickstarts.asp.net/QuickStartv20/aspnet/doc/pages/codebehind.aspx
     
    Alexey Smirnov, May 25, 2010
    #2
    1. Advertising

  3. Mario

    Mario Guest

    "Alexey Smirnov" <> wrote in message
    news:...
    On May 25, 2:34 pm, "Mario" <> wrote:
    > The row below:
    >
    > <asp:Label runat="server" ID="Label4" Text="<%#
    > System.Math.Ceiling((double)(Container.TotalRowCount)/(double)Container.Pag
    > eSize)
    > %>" />
    >
    > And it works fine, but I wonder, how to write similar code when I don't
    > have
    > code completion between <% %> tag ? Code above I've finished in .cs file,
    > after I saw similar example, then I copied in a aspx file.


    You can add your code between <script></script> tags in the asx page

    <script runat="server">
    Sub Page_Load(sender As Object, e As EventArgs)
    Label4.Text = "Test"
    End Sub
    </script>

    For more information about Code Behind and Code Inline look here
    http://quickstarts.asp.net/QuickStartv20/aspnet/doc/pages/codebehind.aspx

    I tried wrote script above the <head> tag:
    <script runat="server">

    System.Math.

    </script>

    And I had code completion for System. Math but further Math methods are
    still unrichable for code completion, so I must look at help to find what
    method I need, while in Default.aspx.cs I have completion for every method
    or variable of some class. 99% of code I write in .cs file, but sometimes I
    only need some small part of a code and then I need to search the whole
    System.Math help. I just check if my VS2008 is setup correctly.
     
    Mario, May 26, 2010
    #3
  4. On May 26, 8:02 am, "Mario" <> wrote:
    > I tried wrote script above the <head> tag:
    > <script runat="server">
    >
    > System.Math.
    >
    > </script>
    >
    > And I had code completion for System. Math but further Math methods are
    > still unrichable for code completion, so I must look at help to find what
    > method I need, while in Default.aspx.cs I have completion for every method
    > or variable of some class. 99% of code I write in .cs file, but sometimes I
    > only need some small part of a code and then I need to search the whole
    > System.Math help. I just check if my VS2008 is setup correctly.


    Intellisense will not work if code block is incorrect. In your example
    above, you missed the body of a function where you would like to
    execute your code with a reference to System.Math. For VB example
    please refer to my last post. C# version should look like

    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
    Label1.Text = System.Math.Sin(1).ToString();
    }
    </script>

    Hope this helps
     
    Alexey Smirnov, May 26, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vish
    Replies:
    2
    Views:
    1,782
    Steve C. Orr, MCSD
    Jul 16, 2003
  2. =?Utf-8?B?TWF0dGhpYXMgSC4=?=

    Running code when viewing aspx code

    =?Utf-8?B?TWF0dGhpYXMgSC4=?=, Feb 11, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    466
    Stefano
    Feb 11, 2005
  3. Paolo Pignatelli
    Replies:
    6
    Views:
    2,237
    Paolo Pignatelli
    Feb 21, 2005
  4. John M
    Replies:
    1
    Views:
    1,316
    Kumar Reddi
    May 29, 2005
  5. Mr Flibble
    Replies:
    8
    Views:
    38,593
    Will Asrari
    May 23, 2006
Loading...

Share This Page