1

我正在研究网络用户控件。我创建了两个简单的 Web 用户控件。第一个是在数据库中保存数据,第二个是检索数据。他们工作得很好。

但是现在我试图在单个页面上添加这些控件,用户可以在其中输入他的数据,并在不加载页面的情况下从数据库中更新最新数据。

这是在 Web 用户控件中使用存储过程插入数据的代码

protected void BtnSave_Click(object sender, EventArgs e)
{
        UserBO userBO = new UserBO();
        userBO.Name = txtname.Text;
        userBO.address = txAddress.Text;
        userBO.EmailID = txtEmailid.Text;
        userBO.Mobilenumber = txtmobile.Text;

        UserBL userBL = new UserBL();
        userBL.SaveUserregisrationBL(userBO);

        txtEmailid.Text = null;
        txAddress.Text = null;
        txtmobile.Text = null;
        txtname.Text = null;
}

这是从 Web 用户控件中的数据库获取用户详细信息的代码

protected void Page_Load(object sender, EventArgs e)
{
        Bussinesslogic.UserBL bl = new Bussinesslogic.UserBL();
        GridView1.DataSource = bl.getUserDetails();
        GridView1.DataBind();
}

这是我的业务逻辑

public class UserBL
{
   public int SaveUserregisrationBL(UserBO objUserBL) // passing Business object here
   {
       try
       {
           UserDA objUserda = new UserDA(); // Creating object of Dataccess

           return objUserda.AddUserDetails(objUserBL); // calling Method of DataAccess
       }
       catch
       {
           throw;
       }
   }

   public DataSet getUserDetails() // passing Business object Here
   {
       try
       {
           UserDA da = new UserDA();

           return da.getUserDetail();
       }
       catch
       {
           throw;
       }
   }
}

我的数据访问层是

public class UserDA
{
    SqlConnection con = new
     SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());

    public int AddUserDetails(UserBO ObjBO) // passing Business object here
    {
        try
        {
            /* Because we will put all out values from our (UserRegistration.aspx)
             To in Business object and then Pass it to Business logic and then to
             DataAcess
             this way the flow carry on*/
            SqlCommand cmd = new SqlCommand("sprocUserinfoInsertUpdateSingleItem", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@Name", ObjBO.Name);
            cmd.Parameters.AddWithValue("@Address", ObjBO.address);
            cmd.Parameters.AddWithValue("@EmailID", ObjBO.EmailID);
            cmd.Parameters.AddWithValue("@Mobilenumber", ObjBO.Mobilenumber);

            con.Open();
            int Result = cmd.ExecuteNonQuery();
            cmd.Dispose();

            return Result;
        }
        catch
        {
            throw;
        }
    }

    public DataSet getUserDetail()
    {
        string query = "SPGetUserInfo";
        SqlDataAdapter adp = new SqlDataAdapter(query, con);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        return ds;
    }
}
4

1 回答 1

0

基本上,您编辑/保存数据的用户控件不应该知道检索数据和显示结果的用户控件的逻辑。这可能也是您想要实现的目标。

为了让它们一起工作,您需要在页面中添加逻辑,这基本上会消耗两个用户控件并知道如何调用每个用户控件。为此,两个用户控件都可以公开页面可以订阅的事件处理程序,一旦事件发生就会触发这些事件处理程序,因此页面可以触发后续操作。

在您的情况下,您的编辑/保存用户控件需要一个事件处理程序(由头部执行此操作,因此可能包含拼写错误):

public event EventHandler Saved = null;

protected void BtnSave_Click(object sender, EventArgs e)
{
    UserBO userBO = new UserBO();
    userBO.Name = txtname.Text;
    userBO.address = txAddress.Text;
    userBO.EmailID = txtEmailid.Text;
    userBO.Mobilenumber = txtmobile.Text;

    UserBL userBL = new UserBL();
    userBL.SaveUserregisrationBL(userBO);

    txtEmailid.Text = null;
    txAddress.Text = null;
    txtmobile.Text = null;
    txtname.Text = null;

    //Check whether a control has subscribed to the Saved event
    EventHandler tmp = Saved;
    if (tmp != null) {
        //Trigger the Saved event
        tmp(this, args);
    }
}

现在在您的页面中的 Page_Load 添加以下内容:

yourSaveEditUserControlInstance.Saved += ShowResults;

并在您的页面中添加方法 ShowResults:

private void ShowResults(object sender, EventArgs args) {
    //Show your results by calling the RetrieveAndShow method in your other user-control
}

我使用了 EventHandler,因为没有任何数据被传递给任何已更新的内容。如果您需要知道这一点,可以使用 CommandEventHandler 并将保存的对象/id 作为 CommandArgument 传递,或者如果您不喜欢使用 CommandEventHandler,可以创建自定义 EventHandler。您的 ShowResults 将收到 CommandEventArgs 而不是 EventArgs,您可以在其中从 CommandArgument 重铸您的对象/ID。

于 2015-06-23T10:04:02.630 回答