Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. 2 different types of coding

2 different types of coding

Scheduled Pinned Locked Moved ASP.NET
htmltutorialquestion
12 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Timothy_1982
    wrote on last edited by
    #1

    What's the deal with coding within your html between <% %> signs, and coding in the .cs file? Can someone explain this to me? maybe with an example ? thank you very much

    T D D C R 5 Replies Last reply
    0
    • T Timothy_1982

      What's the deal with coding within your html between <% %> signs, and coding in the .cs file? Can someone explain this to me? maybe with an example ? thank you very much

      T Offline
      T Offline
      Tal Even Tov
      wrote on last edited by
      #2

      The <% %> type of coding is called inline coding (if I'm not mistaken) and coding in a .cs file is called code-behind coding. There are pros and cons to using either: <% %> You code this way much like you would in classic ASP. The page loads faster than code-behind but does not compile the code into a dll (i.e.: anybody with access to the server can read your code. .cs The page will load a little slower (the first time) because the server compiles the code (usually for the whole site at the same time cause it's usually in the same dll). But the code is stored in a .NET assembly (dll) so your code is much harder to crack. I love chocolate

      1 Reply Last reply
      0
      • T Timothy_1982

        What's the deal with coding within your html between <% %> signs, and coding in the .cs file? Can someone explain this to me? maybe with an example ? thank you very much

        D Offline
        D Offline
        DELETEUSER
        wrote on last edited by
        #3

        There are three way coding in ASP.NET 1) Inline Coding - uses <% %> 2) Code Behind (most common) - uses .cs pages 3) Mixed (or something...I don't remember) ---------- user9 A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.

        1 Reply Last reply
        0
        • T Timothy_1982

          What's the deal with coding within your html between <% %> signs, and coding in the .cs file? Can someone explain this to me? maybe with an example ? thank you very much

          D Offline
          D Offline
          DavidNohejl
          wrote on last edited by
          #4

          Codebehind is here - among other resons - to improve readability by splitting markup and code into two separate files. I suggest you to use inline code ( between <% and %> ) only if necessary, e.g outside form tag. David Never forget: "Stay kul and happy" (I.A.)
          David's thoughts / dnhsoftware.org / MyHTMLTidy

          T 1 Reply Last reply
          0
          • D DavidNohejl

            Codebehind is here - among other resons - to improve readability by splitting markup and code into two separate files. I suggest you to use inline code ( between <% and %> ) only if necessary, e.g outside form tag. David Never forget: "Stay kul and happy" (I.A.)
            David's thoughts / dnhsoftware.org / MyHTMLTidy

            T Offline
            T Offline
            Timothy_1982
            wrote on last edited by
            #5

            thx all of you for this info. so markup is best with inline, and the rest is code behind? :)

            D 1 Reply Last reply
            0
            • T Timothy_1982

              thx all of you for this info. so markup is best with inline, and the rest is code behind? :)

              D Offline
              D Offline
              DavidNohejl
              wrote on last edited by
              #6

              I quite don't understand what you mean... markup is in .aspx, code in .cs, pseudocode in .vb* further reading[^] David *sorry, couldn't resist Never forget: "Stay kul and happy" (I.A.)
              David's thoughts / dnhsoftware.org / MyHTMLTidy

              1 Reply Last reply
              0
              • T Timothy_1982

                What's the deal with coding within your html between <% %> signs, and coding in the .cs file? Can someone explain this to me? maybe with an example ? thank you very much

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                The <%%> signs allow you to place code in the page. The main 'advantage' of this is that you don't need to compile a project or create a dll to distribute your project, if you use this style exclusively. The disadvantanges are that the server hosts your code, and it will be as unreadable/unmaintainable as classic ASP was. The only thing I ever put in <%%> is a function call, and all the code I want to execute is in a protected method of the code behind class. Christian Graus - Microsoft MVP - C++

                1 Reply Last reply
                0
                • T Timothy_1982

                  What's the deal with coding within your html between <% %> signs, and coding in the .cs file? Can someone explain this to me? maybe with an example ? thank you very much

                  R Offline
                  R Offline
                  rwestgraham
                  wrote on last edited by
                  #8

                  This is kind of complicated. There are two places you can execute code in ASP.NET - on the client or on the server. Code behind in the .cs(.vb) file always executes on the server. You program code behind esentially the same as in a Windows app. Code behind is typically used to execute database operations, or navigate to a new page, etc. Client side code is either markup language (HTML, DHTML) or script (VBScript, Javascript, or JScript). You program markup and scripting code in ASP.NET essentially the same as you would ordinary markup and scripting in a non-ASP.NET web page. Now, the <% %> tags are what can be thought of as a processer switch. It basically tells the *server* that anything between the <% %> tags is NOT markup or script and needs to be handled on the *server* side. There are two basic uses of the <% %> tags. The first is processer directives that tell the server how to handle the page. For example if I create an ASP.NET webform in VB.NET it automatically inserts a tag like this: <%@ Page Language="vb" AutoEventWireup="false" Codebehind="EVSubmit.aspx.vb" Inherits="EVMain.EVSubmit"%> This tells the server what language the page uses and basically how to put the code modules together. The second use of the <% %> is for inline functions. A couple of things you need to know about inline functions: You cannot mix languages, so whatever is in the <% %> tags must either be VB.NET or C#, depending on which language your page is. Inline functions are run on the server. Let's look at an example (in VB, sorry). There is an intrinsic function in VB.NET "Now" that returns the current system date/time. There is a second intrinsic function in VB.NET "Format" that accepts numeric input and a formatting mask. I can write code in the markup part of the page like this:

                  Todays date is: <%=Format(Now, "mm/dd/yyyy") %>

                  What this does is tell the server "Run the code in between the <% %> tags as VB.NET on the *server* and insert the results into the HTML stream sent back to the browser. If I look at the page source when I load the above page I will see in the HTML stream something that looks like:

                  Todays date is: 05/26/2005

                  So to summarize, inline coding within <% %> tags within your HTML is definitely not the same as client side scripting, which you do using the traditional etc. Some people may be of the mistaken impression that inline code within the <% %> tags runs on the client. It does not. Other pe

                  T 1 Reply Last reply
                  0
                  • R rwestgraham

                    This is kind of complicated. There are two places you can execute code in ASP.NET - on the client or on the server. Code behind in the .cs(.vb) file always executes on the server. You program code behind esentially the same as in a Windows app. Code behind is typically used to execute database operations, or navigate to a new page, etc. Client side code is either markup language (HTML, DHTML) or script (VBScript, Javascript, or JScript). You program markup and scripting code in ASP.NET essentially the same as you would ordinary markup and scripting in a non-ASP.NET web page. Now, the <% %> tags are what can be thought of as a processer switch. It basically tells the *server* that anything between the <% %> tags is NOT markup or script and needs to be handled on the *server* side. There are two basic uses of the <% %> tags. The first is processer directives that tell the server how to handle the page. For example if I create an ASP.NET webform in VB.NET it automatically inserts a tag like this: <%@ Page Language="vb" AutoEventWireup="false" Codebehind="EVSubmit.aspx.vb" Inherits="EVMain.EVSubmit"%> This tells the server what language the page uses and basically how to put the code modules together. The second use of the <% %> is for inline functions. A couple of things you need to know about inline functions: You cannot mix languages, so whatever is in the <% %> tags must either be VB.NET or C#, depending on which language your page is. Inline functions are run on the server. Let's look at an example (in VB, sorry). There is an intrinsic function in VB.NET "Now" that returns the current system date/time. There is a second intrinsic function in VB.NET "Format" that accepts numeric input and a formatting mask. I can write code in the markup part of the page like this:

                    Todays date is: <%=Format(Now, "mm/dd/yyyy") %>

                    What this does is tell the server "Run the code in between the <% %> tags as VB.NET on the *server* and insert the results into the HTML stream sent back to the browser. If I look at the page source when I load the above page I will see in the HTML stream something that looks like:

                    Todays date is: 05/26/2005

                    So to summarize, inline coding within <% %> tags within your HTML is definitely not the same as client side scripting, which you do using the traditional etc. Some people may be of the mistaken impression that inline code within the <% %> tags runs on the client. It does not. Other pe

                    T Offline
                    T Offline
                    Timothy_1982
                    wrote on last edited by
                    #9

                    First of all Robert, thank you for this very clear explanation. I also want to agree with the fact to minimise the inline code, but look at my problem: i have a page with pictures, the amount of pictures is unknown at coding time, so in my code i'm going to check a dir, if the dir has 10 jpg files, i have to create an array with 10 pictures, if i found 5 files i have to create an array with 5 pictures, ... (i think u get it by now :) ) so now i create this by inline coding, but the problem is in stead of showing the pictures with the tag, i would like to use the ImageButton control of ASP.NET, because when a picture is clicked i want to catch the onclick event and execute some code (filling a session variable with the pictures name). Can you explain me how i can program this with code behind? Because how can i dynamically create objects in a function in code behind, and place them on a position on the

                    ? Or can i do this another way? when i use the html img tag, and add a onclick event method, the only thing i can call with this onclick is a client side method not? so actually i can't use this one. i hope you understand my problem, i quite hard to explain.

                    R 1 Reply Last reply
                    0
                    • T Timothy_1982

                      First of all Robert, thank you for this very clear explanation. I also want to agree with the fact to minimise the inline code, but look at my problem: i have a page with pictures, the amount of pictures is unknown at coding time, so in my code i'm going to check a dir, if the dir has 10 jpg files, i have to create an array with 10 pictures, if i found 5 files i have to create an array with 5 pictures, ... (i think u get it by now :) ) so now i create this by inline coding, but the problem is in stead of showing the pictures with the tag, i would like to use the ImageButton control of ASP.NET, because when a picture is clicked i want to catch the onclick event and execute some code (filling a session variable with the pictures name). Can you explain me how i can program this with code behind? Because how can i dynamically create objects in a function in code behind, and place them on a position on the

                      ? Or can i do this another way? when i use the html img tag, and add a onclick event method, the only thing i can call with this onclick is a client side method not? so actually i can't use this one. i hope you understand my problem, i quite hard to explain.

                      R Offline
                      R Offline
                      Rhys Gravell
                      wrote on last edited by
                      #10

                      Yes you can dynamically create objects in the code behind, and yes you can dynamically create, place and wire up to event handlers WebControl objects at run time. As a really dirty and basic example create a page with a Web Panel control on it called Panel 1 and put the following in the Page_Load event of your code behind file... for (int i = 1; i<6; i++) { TextBox lTextBox = new TextBox(); lTextBox.Text = i.ToString(); Panel1.Controls.Add(lTextBox); lTextBox.Dispose(); } Rhys A bus station is where a bus stops. A train station is where a train stops. On my desk I have a workstation... Vampireware /n/, a project, capable of sucking the lifeblood out of anyone unfortunate enough to be assigned to it, which never actually sees the light of day, but nonetheless refuses to die.

                      T 1 Reply Last reply
                      0
                      • R Rhys Gravell

                        Yes you can dynamically create objects in the code behind, and yes you can dynamically create, place and wire up to event handlers WebControl objects at run time. As a really dirty and basic example create a page with a Web Panel control on it called Panel 1 and put the following in the Page_Load event of your code behind file... for (int i = 1; i<6; i++) { TextBox lTextBox = new TextBox(); lTextBox.Text = i.ToString(); Panel1.Controls.Add(lTextBox); lTextBox.Dispose(); } Rhys A bus station is where a bus stops. A train station is where a train stops. On my desk I have a workstation... Vampireware /n/, a project, capable of sucking the lifeblood out of anyone unfortunate enough to be assigned to it, which never actually sees the light of day, but nonetheless refuses to die.

                        T Offline
                        T Offline
                        Timothy_1982
                        wrote on last edited by
                        #11

                        hmm interesting, will try this out when i get home. thx for the help Rhys. And is it possible to work without a panel?

                        R 1 Reply Last reply
                        0
                        • T Timothy_1982

                          hmm interesting, will try this out when i get home. thx for the help Rhys. And is it possible to work without a panel?

                          R Offline
                          R Offline
                          Rhys Gravell
                          wrote on last edited by
                          #12

                          You can only add child controls to Controls which have a ControlsCollection object, (i.e., Panel1.Conrols Gets a ControlCollection object that represents the child controls for the specified Panel server control). It's quite a complex GUI area but worthwhile investigating and can be utilised to produce some very dynamic pages. Rhys A bus station is where a bus stops. A train station is where a train stops. On my desk I have a workstation... Vampireware /n/, a project, capable of sucking the lifeblood out of anyone unfortunate enough to be assigned to it, which never actually sees the light of day, but nonetheless refuses to die.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups