0

I need help understanding what goes into where in the .cs file when i am using Web Developer express 2010.

I am looking at an example on the W3 site where you enter your name and click submit and a label pops up and shows what you entered all server side. http://www.w3schools.com/aspnet/showasp.asp?filename=demo_textbox

I am trying to duplicate this on my site and I need to know where the text in blue (looking at the link above) goes in the .cs file (assuming it goes in there).

Here is what my .cs file looks like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace Assignment2
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }



    }  
}

Where should I put it? Thanks for any help!!!!!!!!

3 Answers3

1

The code in blue would go after Page_Load

public void submit(object sender, EventArgs e)
{
    lbl1.Text="Your name is " + txt1.Text;
}
aserwin
  • 1,040
  • 2
  • 16
  • 34
0

It doesn't go into the .cs file.

For each page, you will have the display elements (page.aspx) and the code (page.aspx.cs). The code you highlighted needs to go into page.aspx

ChrisBint
  • 12,773
  • 6
  • 40
  • 62
  • To help clarify there are two files created with the web form. WebForm1.aspx AND WebForm1.aspx.cs The script in the link you posted goes in the plain aspx file. You can think of the aspx file as the 'template' for the web page response – Russ Jul 25 '12 at 01:44
  • so my aspx.cs file it technically empty except for what i posted above? – user1550225 Jul 25 '12 at 01:45
  • @user1550225 yes, you can do a lot of the scripting stuff in there instead of using ` – Russ Jul 25 '12 at 01:47
0

Generally we separate C# code putting it in a .cs file (or a .vb file if you use VB, instead of C#). This is why Visual Studio created it automatically for you.

But in the example of w3schools, they use the code embedded in aspx, for pedagogical purpose, so they not use a .cs file. And the code they use is VB code, not C# syntax.

It is not a good practice to embed server code in .aspx file but, sometimes you would need to use this.

More info at http://msdn.microsoft.com/en-us/library/ms178135.aspx#Y90