How to use two web user control on one page in asp.net

386 views Asked by At

I am working on web user controls. I have created two simple web user controls. The first on is saving data in database and second one is retrieving data. They are working perfectly fine.

But now I am trying to add those both control on single page where user can input his data and to update the latest data from database without page load.

This is code for insert data using a stored procedure in web user control

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;
}

and this is code for get user detail from database in web user control

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

This is my business logic

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;
       }
   }
}

and my Data Access Layer is

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;
    }
}
1

There are 1 answers

0
Dacker On

In basic your user control to edit/save data should not know the logic of your user-control that retrieves data and shows results. That's probably also what you are trying to achieve.

To get them working together you need to add the logic in your page, that basically consumes both user-controls and knows how to call each user-control. Todo this both user-controls can expose EventHandlers that the page can subscribe to which get triggered once the event happens, so the page can trigger the follow up.

In your case your edit/save user-control needs an event handler (doing this by head, so may contain typos):

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);
    }
}

Now in your page in the Page_Load add the following:

yourSaveEditUserControlInstance.Saved += ShowResults;

And add the method ShowResults in your page:

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

I have used EventHandler since no data is passed about whatever has been updated. If you need to know that, you can use CommandEventHandler and pass your saved object/id as CommandArgument or you can create a custom EventHandler if you don't like using CommandEventHandler. Your ShowResults will receive CommandEventArgs then instead of EventArgs, where you can recast your object/id from CommandArgument.