Newb textbox question

G

Guest

I have a textbox on a form that is populated from the database when the form
loads. When I check textbox.Text when the user clicks my submit button, the
value is always what it was when the form loaded, no matter of what the user
changed the text to. Any suggestions?

protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = Customers.GetCustomer(Request["Customer"]);
if (ds.Tables[0].Rows.Count == 0)
return;

name.Text = ds.Tables[0].Rows[0][0].ToString();
phone.Text = ds.Tables[0].Rows[0][1].ToString();
contact.Text = ds.Tables[0].Rows[0][2].ToString();
address.Text = ds.Tables[0].Rows[0][3].ToString();
city.Text = ds.Tables[0].Rows[0][4].ToString();
state.SelectedValue = ds.Tables[0].Rows[0][5].ToString();
zip.Text = ds.Tables[0].Rows[0][6].ToString();
notes.Text = ds.Tables[0].Rows[0][7].ToString();
}
protected void editSubmit_Click(object sender, EventArgs e)
{
if (editSubmit.Text == "Edit")
{
name.ReadOnly = false;
phone.ReadOnly = false;
contact.ReadOnly = false;
address.ReadOnly = false;
city.ReadOnly = false;
state.Enabled = true;
zip.ReadOnly = false;
notes.ReadOnly = false;

editSubmit.Text = "Submit";
}
else
{
name.ReadOnly = true;
phone.ReadOnly = true;
contact.ReadOnly = true;
address.ReadOnly = true;
city.ReadOnly = true;
state.Enabled = false;
zip.ReadOnly = true;
notes.ReadOnly = true;

editSubmit.Text = "Edit";
string s = name.Text;

Response.Redirect("CustomerDetails.aspx?Customer=" + s, false);
}
}
 
M

Matt Dinovo

You're resetting the textbox each time you load the page. Remember, the
clicking 'submit' still causes the Page_Load event to fire. You want to wrap
your load code with an IsPostBack check as follows:

protected void Page_Load(object sender, EventArgs e)
{
//Note the IsPostBack call
if(!this.IsPostBack)
{
DataSet ds = Customers.GetCustomer(Request["Customer"]);
if (ds.Tables[0].Rows.Count == 0)
return;

name.Text = ds.Tables[0].Rows[0][0].ToString();
phone.Text = ds.Tables[0].Rows[0][1].ToString();
contact.Text = ds.Tables[0].Rows[0][2].ToString();
address.Text = ds.Tables[0].Rows[0][3].ToString();
city.Text = ds.Tables[0].Rows[0][4].ToString();
state.SelectedValue = ds.Tables[0].Rows[0][5].ToString();
zip.Text = ds.Tables[0].Rows[0][6].ToString();
notes.Text = ds.Tables[0].Rows[0][7].ToString();
}
}

HTH,

Matt Dinovo
 
G

Guest

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DataSet ds = Customers.GetCustomer(Request["Customer"]);
if (ds.Tables[0].Rows.Count == 0)
return;

name.Text = ds.Tables[0].Rows[0][0].ToString();
phone.Text = ds.Tables[0].Rows[0][1].ToString();
contact.Text = ds.Tables[0].Rows[0][2].ToString();
address.Text = ds.Tables[0].Rows[0][3].ToString();
city.Text = ds.Tables[0].Rows[0][4].ToString();
state.SelectedValue = ds.Tables[0].Rows[0][5].ToString();
zip.Text = ds.Tables[0].Rows[0][6].ToString();
notes.Text = ds.Tables[0].Rows[0][7].ToString();
} // IsPostback==true means the page is posted back, and you DO NOT
// want to use the original values.
}


Peter
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top