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);
}
}
Matt Dinovo - 31 Jan 2006 18:46 GMT
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
>I have a textbox on a form that is populated from the database when the
>form
[quoted text clipped - 51 lines]
> }
> }
Peter Bromberg [C# MVP] - 31 Jan 2006 18:46 GMT
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

Signature
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
> 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
[quoted text clipped - 48 lines]
> }
> }