asp.net TextBox更改Text属性

时间:2017-04-06 17:34:38

标签: c# asp.net webforms

我使用从数据库加载的数据设置我的textBox Text属性。稍后,用户输入新值并单击“保存”将此信息更新到数据库中。我的问题是textBox.Text永远不会改变它的值。

我的文字框

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

加载数据时:

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;

}

我的保存按钮调用方法来更新FirstName值:

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

我是webforms的新手,但我做错了什么?

这是我的表格:

<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>

代码背后:

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

}

1 个答案:

答案 0 :(得分:1)

仔细检查文本框和保存配置文件按钮是否在同一个<form runat="server"> HTML标记中。通常,当页面使用site.master文件时就是这种情况。

之后,尝试使用调试器检查页面生命周期的不同事件中文本框的值。

编辑:

我认为您需要将loadMyAccount方法包装在IsPostBack条件中,如下所示:

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"]);
        }
    }
}

这会阻止您的Page_Load覆盖用户的输入。