Tuesday, September 9, 2014

User Control

  • Custom, reusable controls.
  • Contain HTML and Server that allow other pages to use it. The extension is (.ascx)
Advantages of User Control:
  • Code Reuse.
  • Reduce Development and Testing Time.
  • Cache the output of the control using fragment caching
  • Can program against any properties declared in the control, just like ASP.NET web controls.
Disadvantages of User Control:
  • User Controls can use within application only. 
  • Can't hide code of user controls like server controls by compiling into an assembly.
When to use User Control ?
  1. Seperate in different section. (Divide Page)
  2. Mutiple Page is using the samething.
Compare  between Web Form and User Control 


Web Form
User Control
Extension
.aspx
.ascx
Beginning
@Page
@Control
<html>,<head>,<body>
No
Yes
Code Behind
Yes
Yes

Header

TagPrefix is the infront and TagName is behind for body 

<%@ Register Src="ucPackageAddEdit.ascx" TagName="ucPackageAddEdit" 
TagPrefix="ucPackageAddEdit" %>



Can Accept more than 1 method or properties, 1 and 2 is run the code across from child page to parent page.

1) Accept Param from Parent Page 

Parent Page (asp.net)

<ucPackageAddEdit:ucPackageAddEdit runat="server" ID="ucPackageAddEdit"
OnAddPackageClicked="AddPackageClicked"></ucPackageAddEdit:ucPackageAddEdit>

protected void AddPackageClicked(ServicesProfile serviceProfile)
{
   run code;
}

Child Page (ascx)

public delegate void AddPackageClickedDelegate(ServicesProfile serviceProfile);
public event AddPackageClickedDelegate AddPackageClicked;

protected void btnAdd_Click(object sender, EventArgs e)
{
    AddPackageClicked(serviceProfile);
}



2) No Accept Param  from Parent Page 

Parent Page (asp.net)

<td>

   <ucClientPackage:ucClientPackage ID="ucClientPackage" runat="server"                           OnMESProfileLoad="MESProfileSection_Load">
   </ucClientPackage:ucClientPackage>
</td>

protected void MESProfileLoad(object sender, EventArgs e)
{
   run code;
}

Child Page (ascx)

public event EventHandler MESProfileLoad;

protected void btnAdd_Click(object sender, EventArgs e)
{
   MESProfileLoad(new object(), new EventArgs());

}



3) Get Value  from Parent Page 

Parent Page (asp.net)

<td>

  <uc:action ID="ucAction" runat="server" NewPageURL="client.aspx" VisibleNewButton="true" />
</td>

Child Page (ascx)

public bool VisibleNewButton
{
    get { return pnlNew.Visible;  }
    set { pnlNew.Visible = value}
}

----------------------------------------------------------------------------

string newPageURL;

public string NewPageURL
{
  get { return newPageURL;  }
  set { newPageURL = value}
}

protected void btnAdd_Click(object sender, EventArgs e)
{
  string url = newPageURL;
}

No comments:

Post a Comment