getting values from a textbox

J

J

Using codebehind, I populate some textboxes in the Page_Load method.
At this point the user can edit the data in the textboxes.
Then I want to click on a Save button and write the edited data in the
textboxes into a table. But referencing the textboxes only shows the
original value, not the newly edited values.

protected void btnSave_Click(object sender, EventArgs e)
{
// This contains original values not edited values
dsContact.Tables["Contact"].Rows[0]["FName"] = txtFirstName.Text;
dsContact.Tables["Contact"].Rows[0]["LName"] = txtLastName.Text;
....
}


Thanks.
 
S

sloan

Make sure the txt boxes are "runat='server'" variety.

Aka, not an Html Textbox (input), but rather a
<asp:textbox></asp:textbox>
 
J

J

They are of the asp variety.

sloan said:
Make sure the txt boxes are "runat='server'" variety.

Aka, not an Html Textbox (input), but rather a
<asp:textbox></asp:textbox>



J said:
Using codebehind, I populate some textboxes in the Page_Load method.
At this point the user can edit the data in the textboxes.
Then I want to click on a Save button and write the edited data in the
textboxes into a table. But referencing the textboxes only shows the
original value, not the newly edited values.

protected void btnSave_Click(object sender, EventArgs e)
{
// This contains original values not edited values
dsContact.Tables["Contact"].Rows[0]["FName"] = txtFirstName.Text;
dsContact.Tables["Contact"].Rows[0]["LName"] = txtLastName.Text;
...
}


Thanks.
 
S

sloan

Something weird.

On a button save click event (which is a postback essentially) ..code like
this should work


string newFirstName = txtFirstName.Text;


Try that first, and get the "did the ds update" out of the debugging
equation.

You might want to consider a strongly typed dataset as well.
All that row(indexer).column(indexer) stuff is a recipe for bugs and really
for the birds.
ADO.Net is not an upgrade to ADO,....



See example below.

...............................


Add / New Item / DataSet.

Call it "EmployeeDS". (or EmployeeDS.xsd)

Right click (in the design area) ... Add a table. "Employee"

Add 3 columns

ID (int)
LastName (string)
FirstName (string)


...
then you can "code up" a version like this

EmployeeDS ds = new EmployeeDS();

//first way
ds.Employee.AddNewEmployeeRow ( 123, "Smith" , "John");
//second way
EmployeeDS.Employee.EmployeeRow newRow = ds.Employee.NewEmployeeRow();
newRow.ID = 234;
newRow.LastName = "Jones";
newRow.FirstName = "Mary";

ds.Employee.AddNewEmployeeRow ( newRow ) ;

string x = ds.GetXml();




J said:
They are of the asp variety.

sloan said:
Make sure the txt boxes are "runat='server'" variety.

Aka, not an Html Textbox (input), but rather a
<asp:textbox></asp:textbox>



J said:
Using codebehind, I populate some textboxes in the Page_Load method.
At this point the user can edit the data in the textboxes.
Then I want to click on a Save button and write the edited data in the
textboxes into a table. But referencing the textboxes only shows the
original value, not the newly edited values.

protected void btnSave_Click(object sender, EventArgs e)
{
// This contains original values not edited values
dsContact.Tables["Contact"].Rows[0]["FName"] = txtFirstName.Text;
dsContact.Tables["Contact"].Rows[0]["LName"] = txtLastName.Text;
...
}


Thanks.
 
S

Scott Roberts

You realize that when the user clicks the Save button that another postback
occurs and the Page_Load event happens again, right? If you are setting the
value of the Textbox in Page_Load without checking the IsPostBack property
then you are overwriting the user's changes.

You need to get a firm grasp on the ASP.NET page lifecycle:
http://www.codeproject.com/KB/aspnet/lifecycle.aspx
 

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