2015-06-23 25 views
1

我正在開發Web用戶控件。我創建了兩個簡單的Web用戶控件。第一個是將數據保存在數據庫中,第二個是檢索數據。他們工作得很好。如何在asp.net中的一個頁面上使用兩個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; 
    } 
} 

回答

0

基本上,編輯/保存數據的用戶控件不應該知道檢索數據並顯示結果的用戶控件的邏輯。這可能也是你想要實現的。

爲了讓他們一起工作,您需要在頁面中添加邏輯,基本上會同時使用用戶控件並知道如何調用每個用戶控件。 Todo這兩個用戶控件都可以公開EventHandlers,該頁面可以訂閱事件發生時觸發的事件,因此頁面可以觸發後續操作。

在你的情況你編輯/保存用戶控件需要一個事件處理程序(由頭部這樣做,可能會包含錯別字):

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 
} 

我已經使用事件處理程序因爲沒有數據傳遞關於更新的任何內容。如果您需要知道這一點,可以使用CommandEventHandler並將保存的對象/ ID作爲CommandArgument傳遞,或者如果您不喜歡使用CommandEventHandler,則可以創建自定義EventHandler。您的ShowResults將接收CommandEventArgs,而不是EventArgs,您可以在CommandArgument中重新創建對象/標識。

相關問題