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();
} 6 3128
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();
}
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();
}
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
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?
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
//
}
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?
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: dskillingstad |
last post by:
I'm trying to assign a custom value to a textbox. Here's what I have.
I've created a module and "default value" code for a textbox which
generates a custom auto-number (yyyy-0000) when a New...
|
by: Martin |
last post by:
Dear Group
Sorry for bothering you again but I need expert advice on this.
I have placed a HTML textbox on my aspx form and converted it to run
as a server control. At some point in my code I...
|
by: jason |
last post by:
Pardon my ignorance on this. The below code works, except, when I edit
a record and update the two drop downs take the first entry in the
dropdownlist if not selected. I'd also like the dropdown to...
|
by: Justin Morris via DotNetMonster.com |
last post by:
<asp:TextBox ID="TextBox1" runat="server" value='<%=Server.HtmlEncode
(Request.Cookies("Username")("Username"))%>'/>
<input name="Password" type="text" id="Password" value='<%...
|
by: datakix |
last post by:
After 16 hours of frustration, I've managed to solve this problem for a
project I'm working on.
The 'trick' is set EnableViewState="False" for the asp:textbox inside
the Repeater control.
The...
|
by: d.steininger |
last post by:
Hi there!
Is there a way to bind a Textbox.Text to a ScrollBar.Value (not the
Textbox-own Scrollbar) and vice versa?
The Problem:
I have to deal with two controls. The Textbox should accept...
|
by: simon |
last post by:
hello,
new to vb.net, have a few questions about DataGrid. I have a dataGrid
that is working pulling a dataset back from a stored proc and binding
to the datagrid for display
the datagrid's...
|
by: remya1000 |
last post by:
i'm using ASP with MSAccess as database.
i have two buttons and two textbox in my page.
when i press my first button (First month) i need to display the current month in one textbox and last one...
|
by: Ahmedhussain |
last post by:
Hi there,
I m doing work on a gridview and Im getting an error:
A potentially dangerous Request.Form value was detected from the client (ctl00$Content$GridView1$ctl03$TextBox1="<span...
|
by: justice750 |
last post by:
Hi All,
I am using a FormView control. The allows me to update records in the database. However, when a database field is null I can not update the field on the form. It works fine when the field...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |