Home Asp.net Write C# or Vb.net Code-behind Code In aspx Page Inline

Write C# or Vb.net Code-behind Code In aspx Page Inline

55
0
Redirect Page on Button Click

Now here in this tutorial, I’ll explain how to write c# or vb.net code-behind code in aspx page.

In my previous tutorials, I’d explained how to show alert message from code-behind, how to call server-side method from javascript, how to call javascript function from code-behind and other more cracking tutorials on Asp.net, JavaScript, jQuery here.

Using following syntax, you can use inline code in .aspx pages. The server-side code will be automatically compiled by the .NET framework the first time the page is requested on the server.

Aspx Code Behind Example C#

<%@ Page Language="C#" Strict="True" %>

Aspx Code Behind Example VB.Net

<%@ Page Language="VB" Strict="True" %>

The compiled .dll file is stored in the Temporary ASP.NET Files system folder. Changing the code in .aspx files will trigger a new compilation, generating new .dll files. The old .dll files are phased out by the framework and eventually deleted.

Note: You need to delete .vb or .cs file first and then use following following code. Also don’t forget to add required namespace on the top of the .aspx page as i included in my example code.

We need to use HTML script tag to write code-behind code to .aspx page, for example <script runat=”server”>your code here..</script> just shown as below.

Code behind C#

Here’s a sample code behind c# example:

<%@ Page Language="C#" Strict="True" %>

<%--Your namespace goes here..--%>
<%@ Import Namespace="System" %>
<!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 id="Head1" runat="server">
    <title>Write code-behind code in .aspx page in Asp.net</title>
    <script runat="server">
        //write your server-side/code-behind code here"¦
        string myVariable = "Quick Way To Learn Asp.net";
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public string myCodeBehindFunction(string val)
        {
        return "AspnetO - " + val;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <h4>Write code-behind code in .aspx page in Asp.net</h4>
        <div>
            <% =myCodeBehindFunction(myVariable)%>
        </div>
    </form>
</body>
</html>

Code behind Vb.net

Here’s a sample code behind vb.net example:

<%@ Page Language="VB" Strict="true" %>

<%--Your namespace goes here..--%>
<%@ Import Namespace="System" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Write code-behind code in .aspx page in Asp.net</title>
    <script runat="server">
        'write your server-side/code-behind code here'
        Dim myVariable As String = "Quick Way To Learn Asp.net"
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

        End Sub

        Public Function myCodeBehindFunction(ByVal val As String) As String
            Return "AspnetO - " & val
        End Function
</script>
</head>
<body>
    <form id="form1" runat="server">
        <h4>Write code-behind code in .aspx page in Asp.net</h4>
        <div>
            <%= myCodeBehindFunction(myVariable)%>
        </div>
    </form>
</body>
</html>

That’s it, you should be able to use code-behind code in .aspx page without any issues!

Previous articlePrompt Alert Before Page Leave in JavaScript or jQuery
Next articleCall Server-side Function Using JavaScript PageMethods
Hi there, I am Mayank, the man behind Technical Mack. I started AspnetO with a motive to educate people on various programming languages ranging from beginners to expert level. Through this blog, I aim to provide more insightful content.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

2 × 5 =