[英]Problem with Textboxes control in asp.net web application. Didn't get text from textboxes for update
I have a form AdminHome.aspx
. 我有一个表格
AdminHome.aspx
。 When a user signed in, then on AdminHome.aspx
page Load
Event, That users profile data loaded from database and populate corresponding Textboxes
. 当用户登录时,然后在
AdminHome.aspx
页面的Load
Event上,该用户对从数据库加载的数据进行配置并填充相应的Textboxes
。 I have a Button
control for Update User Information
. 我有一个用于
Update User Information
的Button
控件。 When a User
logged in, this is how its Home Page
looks like. User
登录后,这就是其Home Page
外观。 Here is the picture . 这是图片。
Now when i change the Designation from INTERN
to Trainee
. 现在,当我将名称从
INTERN
更改为Trainee
。 Here is the picture 这是图片
and click on
Update My Profile
Button. 然后点击
Update My Profile
按钮。 It shows no Errors
or Exceptions
, instead it display a message Record Update Successfully
. 它不显示任何
Errors
或Exceptions
,而是显示一条消息, Record Update Successfully
。 But when i check it in the database, it wasn't updated. 但是当我在数据库中检查它时,它没有被更新。 After putting it on
debug mood
, i came to know that its taking the older values from Textboxes
, I mean i change the value of Designation
from INTERN
to TRAINEE
but still its taking INTERN
. 在将其置于
debug mood
,我知道它采用了Textboxes
的旧值,我的意思是我将Designation
的值从INTERN
更改为TRAINEE
但仍然采用了INTERN
。 Here is the picture 这是图片
Following is my
update Button
Code 以下是我的
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);
}
}
}
and here is the .aspx
code. 这是
.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>
Here is the PageLoad
code. 这是
PageLoad
代码。
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserEmployee"] != null)
{
userEmployeeNumber = Convert.ToString(Session["UserEmployee"]);
GetUserData();
ShowNotification("Welcome! Mr/Mrs " + EmployeeID.UserName.ToString() + "", WarningType.Success);
}
}
private void GetUserData()
{
using (SqlConnection con = new SqlConnection(Base.GetConnection))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT [UserName],[UserContact],[UserEmpNum],[UserDesignation],[UserDepartment] FROM TableUserProfile WHERE UserEmpNum=@UserEmpNum", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("UserEmpNum", userEmployeeNumber);
SqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
tbName.Value = r["UserName"].ToString();
EmployeeID.UserName = tbName.Value.ToString();
tbMobileNo.Value = r["UserContact"].ToString();
tbEmployeeNo.Value = r["UserEmpNum"].ToString();
tbDesignation.Value = r["UserDesignation"].ToString();
tbDepartment.Value = r["UserDepartment"].ToString();
}
}
}
}
In your Page_Load, you have to check IsPostBack 在您的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);
}
}
}
Otherwise in each pageload, your textbox data gets updated with DB value 否则,在每个页面加载中,您的文本框数据将使用DB值更新
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.