user6824563
user6824563

Reputation: 815

asp.net TextBox change Text property

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());
    }
}

}

Upvotes: 1

Views: 3706

Answers (1)

stackuser83
stackuser83

Reputation: 2240

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.

Upvotes: 1

Related Questions