简体   繁体   English

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

[英]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 InformationButton控件。 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 . 它不显示任何ErrorsExceptions ,而是显示一条消息, 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.

相关问题 如何从ASP.NET中动态创建的文本框中获取文本 - How to get text from dynamically created textboxes in ASP.NET 如何使用C#获取运行时在ASP.NET Web应用程序中的按钮单击事件上创建的文本框的值 - How to get value of textboxes created at run time on button click event in asp.net web application using c# 如何在JavaScript中使用具有相同ID的两个文本框获取asp.net用户控件的值 - How to get the values of an asp.net user control with two textboxes with same id in javascript 更改下拉列表值时,ASP.NET不会更新文本框 - Textboxes dont update when dropdownlist value is changed ASP.NET 我发布应用程序后,asp.net:textboxes消失了 - asp.net: textboxes disappear after i publish application 回发后,ASP.NET文本框保留其text属性 - ASP.NET textboxes hold their text property after post back 如何仅从选定的文本框中更新数据库以获取选择性信息? (asp.net C#) - How to update database for selective information from selected textboxes only? (asp.net c#) 2 维随机值数组,其中包含 2 个用户从 asp.net 网络表单文本框输入的值 - 2 dimensional random values array with 2 user entered values from asp.net web form textboxes ASP.NET文本框在Bootstrap模板中不起作用 - ASP.NET textboxes doesn't work in Bootstrap template 如何使Google翻译在asp.net中适用于文本框? - How to get Google translate to work for textboxes in asp.net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM