簡體   English   中英

asp.net Web應用程序中的文本框控件出現問題。 沒有從文本框中獲取文本以進行更新

[英]Problem with Textboxes control in asp.net web application. Didn't get text from textboxes for update

我有一個表格AdminHome.aspx 當用戶登錄時,然后在AdminHome.aspx頁面的Load Event上,該用戶對從數據庫加載的數據進行配置並填充相應的Textboxes 我有一個用於Update User InformationButton控件。 User登錄后,這就是其Home Page外觀。 這是圖片。 在此處輸入圖片說明

現在,當我將名稱從INTERN更改為Trainee 這是圖片 在此處輸入圖片說明 然后點擊Update My Profile按鈕。 它不顯示任何ErrorsExceptions ,而是顯示一條消息, Record Update Successfully 但是當我在數據庫中檢查它時,它沒有被更新。 在將其置於debug mood ,我知道它采用了Textboxes的舊值,我的意思是我將Designation的值從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> 

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

在您的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值更新

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM