By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,960 Members | 2,238 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,960 IT Pros & Developers. It's quick & easy.

asp.net value of textbox

P: n/a
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();
}
Nov 14 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Chris" wrote:
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();
}
Nov 14 '06 #2

P: n/a
Sorry that should have been (in C#)
if(TextBox1.Text == "")

Cheers.

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Chris" wrote:
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();
}
Nov 14 '06 #3

P: n/a
Rad

On Tue, 14 Nov 2006 11:31:02 -0800, Chris
<Ch***@discussions.microsoft.comwrote:
>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
Nov 14 '06 #4

P: n/a
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?
Nov 14 '06 #5

P: n/a
Rad
On Tue, 14 Nov 2006 13:22:02 -0800, Chris
<Ch***@discussions.microsoft.comwrote:
>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
//
}
Nov 14 '06 #6

P: n/a
Rad
By the way Chris, when you click the button, this causes the page to load
again ... a postback.

------------------------------

Bits. Bytes.
http://bytes.thinkersroom.com
------------------------------
"Chris" wrote:
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?
Nov 15 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.