我有一个AdminHome.aspx
表格。用户登录后,在AdminHome.aspx
页的Load
事件中,该用户分析了从数据库加载的数据并填充了相应的Textboxes
。我有一个Button
的{{1}}控件。 Update User Information
登录后,其外观User
如下。这是图片。
现在,当我将指定从Home Page
更改为INTERN
时。这是图片
并点击Trainee
按钮。它不显示Update My Profile
或Errors
,而显示一条消息Exceptions
。但是当我在数据库中检查它时,它没有被更新。将其放在Record Update Successfully
上后,我知道它采用了debug mood
的旧值,我的意思是我将Textboxes
的值从Designation
更改为{{1} },但仍采用INTERN
。这是图片
以下是我的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>
答案 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值进行更新