Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

my 'hello world' does not work! 2

Status
Not open for further replies.

qiw360

Programmer
Feb 10, 2004
12
0
0
CA
Hi, I am a beginner in C# ASP.Net. I tried a very simple program: make a textbox and a button, each time I hit the button, the number in the textbox should increase by 1. My program do not work for me. When I hit the button, the number in the textbox do not change at all. I am sorry to ask this "hello world" kind question. But, what I did wrong?

Thanks

*******************

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication2Test
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected int i = 0;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
i++;
TextBox1.Text = i.ToString();

}
}
}
 
Hi!
Try modifying your code like this:
Code:
private void Button1_Click(object sender, System.EventArgs e)
{
 if(TextBox1.Text!=null)
  i=(int) TextBox1.Text;
 i++;
 TextBox1.Text = i.ToString();
}

[morning]
 
Hi, Joulius:

Thanks for your tip. Unfortunately it does not work for me. The line: i=(int) TextBox1.Text; did not compile. (Can't convert string into int).
 
Oops, my mind was in other place ;)
Code:
i= System.Convert.ToInt32(TextBox1.Text);

[morning]
 
FYI- you are seeing this problem because in web programming the whole page is essentially dumped after the server sends back a response. I.E. When you see the page, the variable "i" does not exist until another request for the page is made, at which point "i" is made from scratch (naturally, it doesn't retain its value). The server does this to conserve resources.

ASP.NET state management helps solve this issue with ViewState, Session, Application, Cookies, etc. For example, the ViewState, a collection of information sent back and forth over the internet by the client and server, can keep track of the input values of the client, like the text in a TextBox, thus you CAN retrieve "i"'s old value by looking at the Text property of the TextBox as Joulius has done.

 
Oh, I see, it is the HTTP stateless thing! You pointed me to the right direction. Thanks a lot.

In my C# ASP.NET project, should I program ViewState and Session in C# codes (i.e. in the .aspx.cs) or ASP codes (i.e. in the .aspx file)? I think all should be done in C# code since it is namely a C# project, but in most posts on ASP.NET, people like to show ASP code. So I am really confused. Thanks.
 
ViewState is enabled on controls by default. You can turn it off on a control-by-control basis (in the designer, just set the EnableViewState property to true or false), on a page basis, by setting the EnableViewState property of the @Page directive, or on an application or machine basis if you really want to.

Viewstate is easier to manage than you may think. You don't ordinarily need to explicitly do anything to access the values, you simply see the values by looking at the properites of your control (via txtMyText.Text or something).

The disadvantage to ViewState is that it results in more information getting sent back and forth which, of course, eats more bandwidth.
 

Yes, viewstates for both the page and controls are enable by default. After adding the line Joulius suggested, my codes work. It is nice to have it working and know why. You guys are so helpful, thanks a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top