asp.net Web应用程序中的文本框控件出现问题。没有从文本框中获取文本以进行更新

时间:2019-01-25 07:10:33

标签: c# asp.net webforms textbox

我有一个AdminHome.aspx表格。用户登录后,在AdminHome.aspx页的Load事件中,该用户分析了从数据库加载的数据并填充了相应的Textboxes。我有一个Button的{​​{1}}控件。 Update User Information登录后,其外观User如下。这是图片。 enter image description here

现在,当我将指定从Home Page更改为INTERN时。这是图片 enter image description here 并点击Trainee按钮。它不显示Update My ProfileErrors,而显示一条消息Exceptions。但是当我在数据库中检查它时,它没有被更新。将其放在Record Update Successfully上后,我知道它采用了debug mood的旧值,我的意思是我将Textboxes的值从Designation更改为{{1} },但仍采用INTERN。这是图片 enter image description here 以下是我的TRAINEE代码

INTERN

这是update Button代码。

        protected void btnUpdateProfile_Click(object sender, EventArgs e)
    {
        try
        {
                UpdateUser();
        }
        catch (Exception ex)
        {
            ShowNotification("Error: " + ex + "", WarningType.Danger);
        }
    }

    private void UpdateUser()
    {
        using (SqlConnection con = new SqlConnection(Base.GetConnection))
        {

            using (SqlCommand cmd = new SqlCommand("UPDATE TableUserProfile SET UserName=@UserName,UserContact=@UserContact,UserDesignation=@UserDesignation,UserDepartment=UserDepartment WHERE UserEmpNum=@UserEmpNum", con))
            {
                string Uname, UContact, UDesignation, UDepartment, UEmployeeNo;
                Uname = tbName.Value.ToUpper();
                UContact = tbMobileNo.Value.ToUpper();
                UDesignation = tbDesignation.Value.ToUpper();
                UDepartment = tbDepartment.Value.ToUpper();
                UEmployeeNo = tbEmployeeNo.Value.ToUpper();
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@UserName", Uname);
                cmd.Parameters.AddWithValue("@UserContact", UContact);
                cmd.Parameters.AddWithValue("@UserDesignation", UDesignation);
                cmd.Parameters.AddWithValue("@UserDepartment", UDepartment);
                cmd.Parameters.AddWithValue("@UserEmpNum", UEmployeeNo);
                con.Open();
                cmd.ExecuteNonQuery();
                ShowNotification("Succes: Record Saved Succesfully!", WarningType.Success);
            }
        }
    }

这是.aspx代码。

<asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
        <div class="container">
        <div class="row">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-10">
                                <h4>Your Profile</h4>
                                <hr />
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">

                                <div class="form-group row">
                                    <label for="username" class="col-4 col-form-label">Name*</label>
                                    <div class="col-6">
                                        <input runat="server" id="tbName" class="form-control here" required="required" type="text" />
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="name" class="col-4 col-form-label">Mobile Number</label>
                                    <div class="col-6">
                                        <input runat="server" id="tbMobileNo" class="form-control here" type="text" />
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="lastname" class="col-4 col-form-label">Employee Number</label>
                                    <div class="col-6">
                                        <input runat="server" id="tbEmployeeNo" class="form-control here" readonly="True" type="text" aria-readonly="True" aria-disabled="True" />
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="text" class="col-4 col-form-label">Designation</label>
                                    <div class="col-6">
                                        <input runat="server" id="tbDesignation" class="form-control here" required="required" type="text" />
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="text" class="col-4 col-form-label">Department</label>
                                    <div class="col-6">
                                        <input runat="server" id="tbDepartment" class="form-control here" required="required" type="text" />
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <div class="offset-4 col-8">
                                        <asp:Button runat="server" ID="btnUpdateProfile" Text="Update My Profile" class="btn btn-primary" OnClick="btnUpdateProfile_Click"></asp:Button>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
            </div>

        </div>
    </div>

</asp:Content>

1 个答案:

答案 0 :(得分:3)

在您的Page_Load中,您必须检查IsPostBack

 protected void Page_Load(object sender, EventArgs e)
 {
       if(!Page.IsPostBack)   
       {
        if (Session["UserEmployee"] != null)
        {
            userEmployeeNumber = Convert.ToString(Session["UserEmployee"]);
            GetUserData();
            ShowNotification("Welcome! Mr/Mrs " + EmployeeID.UserName.ToString() + "", WarningType.Success);
        }
      }
 }

否则,在每个页面加载中,您的文本框数据都会使用DB值进行更新