0

Script does not work when i remove language="C#" and add type="text/C#" in its tag. What is the difference between both of them.

Another Question is why can't i access html Controls while writing code in aspx.cs file, what is the difference between both styles of writing C#

and how can i access html controls so easily if i am writing code in aspx.cs file , like in the following event function and what is the object for source in its parameters

<head runat="server">
<title></title>
<script language="C#" runat="server">
  void Submit_bttn1(object Source,EventArgs e)
  {
    SpanMsg.InnerHtml= "your Suggestions are  "+Textarea1.Value;
  }  
 </script>
</head>



<body>
<form id="form1" runat="server">
<p>
                               Page 2
</p>
    <div>
     <textarea id="Textarea1" cols="15" rows="5" runat="server"></textarea>
     <input type="submit" value="Submit" runat="server" onserverclick="Submit_bttn1" />
     <span id="SpanMsg" runat="server"></span>
    </div>

      <p>&nbsp;</p>
    <p>
     <asp:Button ID="Button1" runat="server" Text="Button" />
   </p>
  </form>
 </body>
</html>

There must be some pros and cons of writing C# code in both styles so what would be the scenerio for writing C# code in html page . Thanks

1
  • 1
    usually from my experience if you are wanting to write client side scripting and or trigger events on the client vs the server side you you would write code in the markup I would guess that you are trying to execute / invoke some method prior to doing a PostBack look at how to use Update Panel as well perhaps this can help you Commented Dec 21, 2013 at 12:58

4 Answers 4

1

Using inline code:

PROS:

  1. Easy to create
  2. Easy to write
  3. Easy deployment
  4. etc.

CONS:

  1. Spaghetti code
  2. Limited intelissense
  3. Hard to debug
  4. etc.

Using code behind:

PROS:

  1. Separation of concerns (if created well)
  2. Compile time warnings (VS)
  3. Better debugging
  4. Code reuse etc..

CONS:

  1. It can be a tangled mess if app isnot properly designed.
  2. Less control in html
  3. Required VS.NET etc.

I prefer code behind because of the seperation of UI and business logic. But from time to time I am having a vacation in inline when I am creating a web part page in sharepoint.

Sign up to request clarification or add additional context in comments.

Comments

1

from my side there is no pros and cons.

before .net 2.0 there is code like this the html and c# was in one page but the fell the need of code separation. so in 2.0 the separate the code and html for separation of concern .

so now we have aspx.cs page and aspx page in asp.net aspx.cs is for the c# code and aspx is for html

we can write c# in aspx but when the code will become to large then developer will confused

that's it thanks

:D

2 Comments

So when there is a ease of writing separate code there is no need to write code in script ...I got your point
now a days mostly people follow the MVC for separation of concerns
0

The .aspx page contains HTML and client side code (JavaScript). The code behind files contain server side logic. It's important to make the distinction.

The only reason it's handy to embed the C# on the .aspx page is to show quick examples of code.

Comments

0

I just followed a tutorial on how to upload a file to a SharePoint library (2007). The c# sample code was written in the HTML markup. I wondered why and landed here. I assume the reason is because it is easier access to SharePoint (or maybe the only way?). My two 2 cents for what it's worth.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.