尝试更新

时间:2015-12-30 11:55:34

标签: c# asp.net webforms

我有一个表单,其中有一个ListBox,一个文本框和四个名为SaveEditDeleteClear的按钮。我从数据库中检索数据并填充ListBox。当我选择ListBox中的一个项目时,它将填充到文本框中。现在,当我删除该项目时,没有任何错误,它工作正常。但是当我尝试更新该项目时,即更改文本框中的文本然后单击Edit按钮时,就会出现问题。在文本框中,我可以看到文本正在被更改,没有问题,但是当我调试时,我发现在后端,文本框仍然包含旧文本而不是修改过的文本。

我做错了什么?

这是我的用户界面代码:

<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    <h1>Employee Category</h1>
    <table>
        <td>
            <asp:ListBox ID="listEmployeeCategory" runat="server" Height="164px" Width="210px" AutoPostBack="true" />
        </td>
        <td>
            <table>
                <tr>
                    <td>
                        <asp:Label ID="lblMessage" runat="server" Text="" Visible="false" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="lblEmpCategoryName" runat="server" Text="Name: " Font-Bold="true" />
                    </td>
                    <td>
                        <asp:TextBox ID="txtEmpCategoryName" runat="server" />
                    </td>
                    <td>
                        <asp:RequiredFieldValidator ID="rqdEmpCategoryName" ControlToValidate="txtEmpCategoryName" ErrorMessage="Employee Category Name can't be empty!" Style="color:Red" runat="server" />
                    </td>
                </tr>
            </table>
            <asp:Button ID="btnSave" Text="Save" runat="server" OnClick="btnSave_Click" />
            <asp:Button ID="btnEdit" Text="Edit" runat="server" OnClick="btnEdit_Click" />
            <asp:Button ID="btnDelete" Text="Delete" runat="server" OnClick="btnDelete_Click" />
            <asp:Button ID="btnCancel" Text="Cancel" runat="server" />
        </td>
    </table>
</asp:Content> 

这是我的后端代码,为了每个人的理解,我发布了我的整个后端代码(BL,DAL和DAO代码除外):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Shelter.DAO.MasterEntry;
using Shelter.BLL.MasterEntry;

namespace Shelter.UI.MasterEntry
{
    public partial class EmployeeCategoryUI : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataIntoListBox();
            }

            else
            {
                if (listEmployeeCategory.SelectedIndex != -1)
                {
                    LoadDataIntoTextBox(Convert.ToInt32(listEmployeeCategory.SelectedValue));  
                }
            }
        }

        private void LoadDataIntoTextBox(int val)
        {
            EmployeeCategory objEmployeeCategory = new EmployeeCategory();
            EmployeeCategoryBLL empCategoryBLL = new EmployeeCategoryBLL();
            objEmployeeCategory.ID = Convert.ToInt32(val);
            DataTable EmpCategoryDt = new DataTable();
            EmpCategoryDt = empCategoryBLL.RetrieveById(objEmployeeCategory);
            txtEmpCategoryName.Text = EmpCategoryDt.Rows[0]["EmpCategoryName"].ToString();
        }

        private void LoadDataIntoListBox()
        {
            EmployeeCategory objEmployeeCategory = new EmployeeCategory();
            EmployeeCategoryBLL empCategoryBLL = new EmployeeCategoryBLL();
            DataSet EmployeeCategoryDs = new DataSet();
            EmployeeCategoryDs = empCategoryBLL.RetreiveFromTable();
            DataTable EmployeeCategoryDt = EmployeeCategoryDs.Tables[0];
            DataRow tempRow = null;
            foreach (DataRow tempRow_Variable in EmployeeCategoryDt.Rows)
            {
                tempRow = tempRow_Variable;
                string rowText = tempRow["EmpCategoryName"] + "(" + tempRow["ID"] + ")";
                string rowValue = tempRow["ID"].ToString();
                listEmployeeCategory.Items.Add(new ListItem(rowText, rowValue));
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            EmployeeCategory objEmployeeCategory = new EmployeeCategory();
            EmployeeCategoryBLL empCategoryBLL = new EmployeeCategoryBLL();
            objEmployeeCategory.EmpCategoryName = txtEmpCategoryName.Text;
            bool isSave = empCategoryBLL.SaveToTable(objEmployeeCategory);

            if (isSave)
            {
                int id = empCategoryBLL.ReturnLastInsertedId();
                lblMessage.Visible = true;
                lblMessage.Style.Add("Color", "Green");
                lblMessage.Text = "Data saved successfully!";
                string rowText = txtEmpCategoryName.Text + "(" +id.ToString()+ ")" ;
                string rowValue = id.ToString();
                listEmployeeCategory.Items.Add(new ListItem(rowText, rowValue));
                txtEmpCategoryName.Text = "";
            }

            else 
            {
                lblMessage.Visible = true;
                lblMessage.Style.Add("Color", "Red");
                lblMessage.Text = "Data saving failed!";
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                EmployeeCategory objEmployeeCategory = new EmployeeCategory();
                EmployeeCategoryBLL empCategoryBLL = new EmployeeCategoryBLL();
                objEmployeeCategory.EmpCategoryName = txtEmpCategoryName.Text;
                objEmployeeCategory.ID = Convert.ToInt32(listEmployeeCategory.SelectedValue);

                bool isEdit = empCategoryBLL.EditInTable(objEmployeeCategory);
                if (isEdit)
                {
                    int id = objEmployeeCategory.ID;
                    lblMessage.Visible = true;
                    lblMessage.Style.Add("Color", "Green");
                    lblMessage.Text = "Data edited successfully!";
                }
                else
                {
                    lblMessage.Visible = true;
                    lblMessage.Style.Add("Color", "Red");
                    lblMessage.Text = "Data editing failed!";
                }
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                EmployeeCategory objEmployeeCategory = new EmployeeCategory();
                EmployeeCategoryBLL empCategoryBLL = new EmployeeCategoryBLL();
                objEmployeeCategory.ID = Convert.ToInt32(listEmployeeCategory.SelectedValue);
                bool isDelete = empCategoryBLL.DeleteFromTable(objEmployeeCategory);

                if (isDelete)
                {
                    lblMessage.Visible = true;
                    lblMessage.Style.Add("Color", "Green");
                    lblMessage.Text = "Data deleted successfully!";
                    listEmployeeCategory.Items.Remove(new ListItem(listEmployeeCategory.SelectedItem.Text, listEmployeeCategory.SelectedValue));
                    txtEmpCategoryName.Text = "";
                }

                else
                {
                    lblMessage.Visible = true;
                    lblMessage.Style.Add("Color", "Red");
                    lblMessage.Text = "Data deleting failed!";
                }
            }
        }
    }
}

txtEmpCategoryName.Text仍然包含旧值,因此每当我尝试更新时,它只会使用旧值而不是文本框中的修改值。似乎TextChanged事件无法正常工作。这个问题的解决方法是什么?

1 个答案:

答案 0 :(得分:2)

您可以在Visual Studio中使用实体框架;

  1. 右键单击您的解决方案 - 选择新的Add - 然后选择New Project - C#-Class Library为其指定项目名称,然后单击Ok将新项目添加到现有项目中。
  2. 将您的类重命名为BusinessLogic等含义名称,并将其公开。
  3. 右键单击新添加的项目并选择添加新项目,在弹出的窗口中选择右侧面板上的数据,然后选择ADO.NET实体数据模型将您的edmx重命名为有意义的名称,然后单击添加。默认选项并单击下一步,通过单击新连接指向SQL数据库。
  4. 选择用于填充列表框并更新说明的商店程序,然后单击“完成”。

    CREATE PROCEDURE Proc_name AS BEGIN SET NOCOUNT ON;

    SELECT ID,描述FROM Table_Name END

    CREATE PROCEDURE [dbo]。[UpdateEmpCategoryBLL]     @ID int,@ Description varchar(50)AS BEGIN     设置NOCOUNT ON;     更新问题1 SET描述= @Description WHERE ID = @ID  END

  5. 在您的班级中实施填充列表框和更新说明的方法

    公共类包装器     {         public static List EmployeeCategory()         {             尝试             {                 返回新的LOOKUPEntities()。empCategoryBLL()。ToList();             }             catch(例外)             {                 扔;             }         }

        public static void UpdateEmpCategory(int id, string description)
        {
            try
            {
                using (LOOKUPEntities client = new LOOKUPEntities())
                {
                    client.UpdateEmpCategoryBLL(id, description);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
    
    1. 将app.config中的连接字符串复制到web.config。然后通过右键单击引用并添加引用,选择解决方案并单击“添加”,将类库添加到引用中。添加System.Data,Entity并单击“确定”。
    2. 现在;
  6. protected void Page_Load(object sender,EventArgs e)         {             if(!Page.IsPostBack)             {                 BindListBox();             }         }

    protected void BindListBox()         {             尝试             {                 ListItem lstBox;                 listEmployeeCategory.Items.Clear();                 var query = Wrapper.EmployeeCategory();                 foreach(查询中的var项)                 {                     lstBox = new ListItem(items.Description,items.ID.ToString());                     listEmployeeCategory.Items.Add(lstBox);                 }             }             catch(Exception ex)             {                 lblMessage.Visible = true;                 lblMessage.Style.Add(“Color”,“Red”);                 lblMessage.Text =“数据加载失败”+ ex +“!”;             }         }

            protected void listEmployeeCategory_SelectedIndexChanged(object sender, EventArgs e)
            {
                lblMessage.Visible = false;
                try
                {
                    Session["ID"] = listEmployeeCategory.SelectedValue.ToString();
                    txtEmpCategoryName.Text = listEmployeeCategory.SelectedItem.ToString();
    
                }
                catch (Exception ex)
                {
                    lblMessage.Visible = true;
                    lblMessage.Style.Add("Color", "Red");
                    lblMessage.Text = "Loading session data failed " + ex + " !";
                }
            }
    
            protected void btnEdit_Click(object sender, EventArgs e)
            {
                try
                {
                    int id = int.Parse(Session["ID"].ToString());
                    Wrapper.UpdateEmpCategory(id, txtEmpCategoryName.Text.Trim());
                    lblMessage.Visible = true;
                    lblMessage.Style.Add("Color", "Green");
                    lblMessage.Text = "Data edited successfully!";
    
                    BindListBox();
                    txtEmpCategoryName.Text = string.Empty;
                }
                catch
                {
                    lblMessage.Visible = true;
                    lblMessage.Style.Add("Color", "Red");
                    lblMessage.Text = "Data editing failed!";
                }
            }
    

    完成!