1

I set my textBox Text property with data loaded from the Database. Later the User enter a new value and click on Save to update this info into the database. My problem is that the textBox.Text never change its value.

My TextBox

<asp:TextBox ID="firstNameModal" runat="server" Width="300px" />

When I load the data:

private void loadMyAccount(int id)
{
    var query =  (from u in db.Users
                 where u.Id == id
                 select u).FirstOrDefault();


    // Modal data
    firstNameModal.Text = query.FirstName;

}

My save button call the method to update the FirstName value:

protected void saveProfile_Click(object sender, EventArgs e)
{
    try
    {
        int id = (int)HttpContext.Current.Session["userId"];

        var query = (from u in db.Users
                     where u.Id == id
                     select u).FirstOrDefault();

        query.FirstName = firstNameModal.Text;

        db.SaveChanges();
    } catch(Exception ex)
    {
        Console.Write(ex.ToString());
    }
}

I'm new in webforms, but what am I doing wrong?

Here is my form:

<form id="form1" runat="server">
    <uc:Header ID="homeHeader" runat="server" />

    <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xs-offset-0 col-sm-offset-0 col-md-offset-3 col-lg-offset-3 toppad" >
        <div class="panel panel-info">
            <div class="panel-heading">
                <h3 class="panel-title"><asp:Label ID="UserName" runat="server"/></h3>
            </div>
            <div class="panel-body">
                <div class="row">
                    <div class="col-md-3 col-lg-3 " align="center"> <img alt="User Pic" src="../img/myAccount.png" class="img-circle img-responsive" /> </div>

                    <div class=" col-md-9 col-lg-9 ">
                        <table class="table table-user-information">
                            <tbody>
                                <tr>
                                    <td>Full Name:</td>
                                    <td><asp:Label ID="fullName" runat="server" /></td>
                                </tr>
                                <tr>
                                    <td>Date of Birth</td>
                                    <td><asp:Label ID="dob" runat="server" /></td>
                                </tr>
                                <tr>
                                    <td>Gender</td>
                                    <td><asp:Label ID="gender" runat="server" /></td>
                                </tr>
                                <tr>
                                    <td>Phone</td>
                                    <td><asp:Label ID="phone" runat="server" /></td>
                                </tr>
                                <tr>
                                    <td>Home Address</td>
                                    <td><asp:Label ID="homeAddress" runat="server" /></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <div class="panel-footer">
                <asp:Button ID="editProfile" runat="server" CssClass="btn btn-primary" Text="Edit Profile" OnClientClick="$('#editProfileModal').modal(); return false;" />
                <asp:Button ID="myBooks" runat="server" CssClass="btn btn-primary" Text="View my books" OnClick="myBooks_Click" /> 
            </div>
        </div>
    </div>

    <!--- Modal Edit Profile --->
    <div class="modal fade" id="editProfileModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button aria-hidden="true" data-dismiss="modal" class="close" type="button">X</button>
                    <h4 class="modal-title"><strong><asp:Label ID="modalFullName" runat="server" /></strong></h4>
                </div>
                <div class="modal-body">
                    <div class="alert alert-info fade in">
                        <asp:Label runat="server" Text="First Name:" Width="90px" />
                        <asp:TextBox ID="firstNameModal" runat="server" Width="300px" /> <br /> <br />  

                        <asp:Label runat="server" Text="Last Name:" Width="90px" />
                        <asp:TextBox ID="lastNameModal" runat="server" Width="300px" />  <br /> <br />    

                        <asp:Label runat="server" Text="Date of Birth:" Width="90px" />
                        <asp:TextBox ID="dobModal" runat="server" Width="300px" />  <br /> <br />    

                        <asp:Label runat="server" Text="Gender:" Width="90px" />
                        <asp:TextBox ID="genderModal" runat="server" Width="300px" />  <br /> <br />    

                        <asp:Label runat="server" Text="Phone:" Width="90px" />
                        <asp:TextBox ID="phoneModal" runat="server" Width="300px" />  <br /> <br />    

                        <asp:Label runat="server" Text="Address:" Width="90px" />
                        <asp:TextBox ID="homeAddressModal" runat="server" Width="300px" />  <br /> <br />    
                    </div>
                </div>
                <div class="modal-footer">
                    <asp:Button ID="saveProfile" OnClick="saveProfile_Click" runat="server"
                        CssClass="btn btn-primary" Text="Save" />
                    <button data-dismiss="modal" class="btn btn-default" type="button">Close</button>
                </div>
            </div>
        </div>
    </div>


</form>

Code behind:

public partial class Views_MyAccount : System.Web.UI.Page
 {
LibraryEntities db = new LibraryEntities();

protected void Page_Load(object sender, EventArgs e)
{
    if(HttpContext.Current.Session["userId"].Equals(0))
    {
        Response.Redirect("Login.aspx");
    }
    else
    {
        loadMyAccount((int) HttpContext.Current.Session["userId"]);
    }
}

private void loadMyAccount(int id)
{
    var query =  (from u in db.Users
                 where u.Id == id
                 select u).FirstOrDefault();

    UserName.Text = query.FirstName + " " + query.LastName;
    fullName.Text = query.FirstName + query.LastName;
    DateTime dt = (DateTime) query.Dob;
    dob.Text = dt.ToString("MM/dd/yyyy");
    gender.Text = (query.Gender == false ? "Female" : "Male");
    phone.Text = query.Phone;
    homeAddress.Text = query.Address;

    // Modal data
    modalFullName.Text = query.FirstName + " " + query.LastName;
    firstNameModal.Text = query.FirstName;
    lastNameModal.Text = query.LastName;
    dobModal.Text = dt.ToString("MM/dd/yyyy");
    genderModal.Text = (query.Gender == false ? "Female" : "Male");
    phoneModal.Text = query.Phone;
    homeAddressModal.Text = query.Address;
}

protected void myBooks_Click(object sender, EventArgs e)
{

}

protected void saveProfile_Click(object sender, EventArgs e)
{
    try
    {
        int id = (int)HttpContext.Current.Session["userId"];

        var query = (from u in db.Users
                     where u.Id == id
                     select u).FirstOrDefault();

        query.FirstName = firstNameModal.Text;

        db.SaveChanges();
    } catch(Exception ex)
    {
        Console.Write(ex.ToString());
    }
}

}

5
  • how you are expecting it to change? Commented Apr 6, 2017 at 17:49
  • I mean, the user type a new value in the TextBox. It should change the value. Right? Commented Apr 6, 2017 at 17:58
  • Where are you expecting it to change the value? In the Database? On the screen? I'm assuming in the DB, but you weren't clear in your question. Commented Apr 6, 2017 at 18:06
  • @user6824563 Your saveProfile() is taking the value of firstNameModal.Text. If it is done correctly, it should save in your database as you expected. Check your table in the database if it changed. If it does, all you need to do is call loadMyAccount() again to refresh the textbox value. Commented Apr 6, 2017 at 18:16
  • I want to change in the DB. First I load the page and get the FirstName from the DB. firstNameModal.Text = query.FirstName; query.FirstName = "Test1". Then, running the app I type "Test2" in the TexBox and click on Save. The save method get the firstNameModal.Text. The problem is that the value still "Test1" instead of "Test2" that I typed. Commented Apr 6, 2017 at 18:17

1 Answer 1

1

Double check that the text box and the save profile button are in the same <form runat="server"> HTML tag. Usually this is the case when the page is using a site.master file.

After that, try to check the value of the textbox in the different events of the page life cycle using the debugger.

EDIT:

I think you need to wrap the loadMyAccount method in a IsPostBack conditional, something like this:

protected void Page_Load(object sender, EventArgs e)
{
    if(HttpContext.Current.Session["userId"].Equals(0))
    {
        Response.Redirect("Login.aspx");
    }
    else
    {
        if(!IsPostBack)
        {
            loadMyAccount((int) HttpContext.Current.Session["userId"]);
        }
    }
}

That will prevent your Page_Load from overwriting the user's input.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, both are in the same form
Can you debug the code to see if the new value is set to the textbox when the page is reloaded?
I debug the code and in this line. query.FirstName = firstNameModal.Text; The firstNameModal.Text should have the new value that I entered, but it still have the old value
Thank you. Works like a champ! BTW, what the !IsPostBack do?
IsPostBack indicates that the page generation is a page reload (in response to the user doing something, for example), !IsPostBack indicates the page generation might have user input included with the session state data. You should do some googling and reading on the "ASP.NET webforms page lifecycle" to understand more details about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.