asp.net value of textbox

G

Guest

I created a very simple web page in asp.net.
there are only a textbox and a button on the page. when the page loads into
web browser there is current date in the textbox. but the date can be changed
by user as well.
now, when you press the button the date should be inserted into a database.
the problem is that , yes it has been inserted, but it is always the current
date. even if a user modified the date, it would be inserted the current date
into the db

why? how to changed that?

my code in aspx (c#):

protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = DateTime.Now.ToShortDateString();
}
protected void Button1_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnection con = new
System.Data.SqlClient.SqlConnection();
con.ConnectionString = "string";
con.Open();
System.Data.SqlClient.SqlCommand com = new
System.Data.SqlClient.SqlCommand("", con);
com.CommandText = "insert into table (date1) values ('" +
TextBox1.Text + "')";
com.ExecuteNonQuery();
con.Close();
}
 
G

Guest

Chris,
This is happening because your code sets the value of TextBox1 's Text
property back to TextBox1.Text = DateTime.Now.ToShortDateString();
in your Page_Load eventhandler, regardless of what the user may have put in
there before they presssed the submit button. So when there is a postback,
the value the user has entered is replaced by your own code.
What you *Could* do is
if(TextBox1.Text = "")
TextBox1.Text = DateTime.Now.ToShortDateString();

Hope that helps.
Peter
 
R

Rad

I created a very simple web page in asp.net.
there are only a textbox and a button on the page. when the page loads into
web browser there is current date in the textbox. but the date can be changed
by user as well.
now, when you press the button the date should be inserted into a database.
the problem is that , yes it has been inserted, but it is always the current
date. even if a user modified the date, it would be inserted the current date
into the db

why? how to changed that?

my code in aspx (c#):

protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = DateTime.Now.ToShortDateString();
}
protected void Button1_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnection con = new
System.Data.SqlClient.SqlConnection();
con.ConnectionString = "string";
con.Open();
System.Data.SqlClient.SqlCommand com = new
System.Data.SqlClient.SqlCommand("", con);
com.CommandText = "insert into table (date1) values ('" +
TextBox1.Text + "')";
com.ExecuteNonQuery();
con.Close();
}


What you want to do is wrap the assigment like follows:

protected void Page_Load(object sender, EventArgs e){

if(!Page.IsPostback)
DateTime.Now.ToShortDateString();

}

This way the textbox will only be automatically set when the page
loads the first time
 
G

Guest

ok, that helps!

if(!Page.IsPostback)
DateTime.Now.ToShortDateString();

can you please explain me what is Page.IsPostback property?

and still I dont understant one thing. I know that when the page loads the
current date is put into textbox, but in my opinion the page loads first and
then the button is clicked (so the value of textbox after page loading should
be taken), doesnt it?
 
R

Rad

ok, that helps!

if(!Page.IsPostback)
DateTime.Now.ToShortDateString();

can you please explain me what is Page.IsPostback property?

and still I dont understant one thing. I know that when the page loads the
current date is put into textbox, but in my opinion the page loads first and
then the button is clicked (so the value of textbox after page loading should
be taken), doesnt it?

In summary, the Page.IsPostBack property determines if a form has been
submitted (eg. a button in the webform has been clicked).

It is generally false when a page is loaded for the first time, and
becomes true when you click the button.

So this code

if(!Page.IsPostback)
DateTime.Now.ToShortDateString();

Will only fire the first time you load the page in the browser. In
your code before, since you did not check this property, the code
always ran.

In general asp.net development you'd write code to initialize the page
in this fashion


if(!Page.IsPostback){
//
// Load your controls here
//
}
 
G

Guest

By the way Chris, when you click the button, this causes the page to load
again ... a postback.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top