AspnetO

We code, that works!

  • Home
  • Asp.net
  • MVC
  • Interview Questions
You are here: 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

By: Mayank Modi | Falls In: Asp.net, C#, VB | Last Updated: Oct 10, 2020

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.

For C#:

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

For 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.

For C#

<%@ 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>

For Vb.net

<%@ 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>

Signup Today And Get Latest Tutorials For Free!

Subscribe to us and get free latest tutorials notifications whenever we publish a new contents.

<

About Mayank Modi

Mayank is a web developer and designer who specializes in back-end as well as front-end development. He's a Founder & Chief Editor of AspnetO. If you'd like to connect with him, follow him on Twitter as @immayankmodi.

Leave a Reply Cancel reply

Search Your Topic



Social Connections

  • 1,438 Fans
  • 3,098 Followers
  • 51 Followers
  • 1,559 Subscribers

Get Latest Tutorials For Free



Top Posts

  • CSS3 Transition: fadeIn and fadeOut like Effects to Hide Show Elements
  • Top 10 OOPS Concepts In C# .NET With Examples
  • Show Confirm Message Box from Code-behind in Asp.net
  • Call JavaScript Function from Code-behind in Asp.net C# Vb
  • Pass Multiple Parameters in Asp.net QueryString Example

Contribute to AspnetO

If you want to contribute your unique blog articles or tutorials (Free / Paid) to AspnetO in any languages, you're most welcome. Just send me your previous articles, and topics on which you are interested to post an tutorial. Contact us at email listed in contact us page. Selected candidates will be contacted.

Search by Tags

Ado.net Ajax appSettings Asp.net C# CheckBox CheckBoxList ConnectionStrings Control CSS CSS3 Difference Download DropDownList Export Facebook fadeIn fadeOut fadeTo fadeToggle File File Extension FileUpload Function GridView IIS Interview Questions JavaScript jQuery MVC OOP RadioButtonList RDP Repeater Send Mail Solutions Split SQL Stored Procedure TextBox Upload Validation VB Web.config Web Hosting

The Man Behind AspnetO

Mayank Modi

Hi there,

Myself Mayank Modi, a Full Stack Developer (.NET Stack) and a blogger from Surat, India.

I'm welcoming you to my blog - AspnetO, a programmers community blog where we code, that works!

I started AspnetO as a hobby and now we're growing day by day. We're now having 5000+ programmers that get benefits and learn new things about website design and development under our community blog.

Here at AspnetO, I write about Beginners to Advance level of tutorials on programming languages like Asp.net using C# and Vb.net, MVC, SQL Server, JavaScript, jQuery etc. In sort, all about .NET Framework and website development stuff and sometimes sharing tips and tricks that can help you to grow up your programming skills.

You can get more details about me and my blog at About us page.

Subscribe To Newsletter

Enter your email address to subscribe to this blog and receive notifications of new posts right to your inbox

Join 1000+ other subscribers

<

Recent Posts

  • Main Difference between SessionState and ViewState in Asp.net
  • How to Get appSettings Value from Web.config File?
  • How to Get ConnectionString from Web.config in Asp.net?
  • Difference between appSettings and connectionStrings in Web.config
  • Get Folder Files List and Export to CSV in .NET
  • Get Files List From Directory Recursively in C# Vb.net
  • Get Hash Value From Current Page URL In jQuery
  • Handle Multiple Submit Buttons in Single MVC Form

Copyright © 2014 - 2021 · All Rights Reserved.

About | Copyrights | Privacy | Terms | Contact | Advertise | Sitemap
Previous Prompt Alert Before Page Leave in JavaScript or jQuery
Next Call Server-side Function Using JavaScript PageMethods