Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net page without a code behind

I have an ASP.Net page with a C# code behind.

However, I've been asked to not use a code behind - so that it will be easier to deploy in SharePoint.

Is there a way to include the C# code in the ASP.Net page, without using a separate code behind file?

like image 975
Sophia Avatar asked Feb 19 '09 01:02

Sophia


People also ask

How is a ASP.NET presentation page associated with its code behind?

One tactic for separating code from presentation in ASP.NET is code behind. Code behind is a feature that enables you to take most of or all the code out of an ASP.NET page and place it in a separate file. The code is processed normally; the only difference is where the code is located.

How do you set display none in code behind?

Add("style","display:none"); div_id: id which you want to hide. Attributes: that will use value. Add: keyword will add the attribute.

What is the purpose of code behind?

The primary goal of the code-behind file is to enable you to write more modular code so that you can keep the code and layout physically separate. By keeping the code and layout separate, you can exploit the native object orientation of the .


2 Answers

By default Sharepoint does not allow server-side code to be executed in ASPX files. See this for how to resolve that.

However, I would raise that having a code-behind is not necessarily difficult to deploy in Sharepoint (we do it extensively) - just compile your code-behind classes into an assembly and deploy it using a solution.

If still no, you can include all the code you'd normally place in a codebehind like so:

<script language="c#" runat="server"> public void Page_Load(object sender, EventArgs e) {   //hello, world! } </script> 
like image 200
Rex M Avatar answered Oct 19 '22 05:10

Rex M


You can actually have all the code in the aspx page. As explained here.

Sample from here:

<%@ Language=C# %> <HTML>    <script runat="server" language="C#">    void MyButton_OnClick(Object sender, EventArgs e)    {       MyLabel.Text = MyTextbox.Text.ToString();    }    </script>    <body>       <form id="MyForm" runat="server">          <asp:textbox id="MyTextbox" text="Hello World" runat="server"></asp:textbox>          <asp:button id="MyButton" text="Echo Input" OnClick="MyButton_OnClick" runat="server"></asp:button>          <asp:label id="MyLabel" runat="server"></asp:label>       </form>    </body> </HTML> 
like image 21
achinda99 Avatar answered Oct 19 '22 07:10

achinda99