Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / January 2006

Tip: Looking for answers? Try searching our database.

Newb textbox question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Eric - 31 Jan 2006 18:11 GMT
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]
>      }
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.