漫漫技术人生路

C#

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

<%@ Reference Control = "Header.ascx" %>

Next we need to do some coding to load the user control on the page. First place a placeholder control so that you can load the user control on that place holder control.

private void Page_Load(object sender, System.EventArgs e)
{

if(!Page.IsPostBack)
{

Header uc = (Header) Page.LoadControl("Header.ascx");

PlaceHolder1.Controls.Add(uc);

}

}
 

Passing Values from User Controls on the form
The UserControl used for this example is: UserControlTextBox.ascx and the webform used is TextBoxTester.aspx.

Let's see the code behind file for the User Control Button click event:

private void Button1_Click(object sender, System.EventArgs e)
{

  // Simply sends the contents of the textbox to the page

  Response.Redirect("TextBoxTester.aspx?Name="+txtName.Text);

}
 

Let's see the code where the page receives the name from the user control and prints it on the page.

private void Page_Load(object sender, System.EventArgs e)
{

if(!Page.IsPostBack)
{

if(Request.QueryString["Name"] != null)
{

string name = Request.QueryString["Name"].ToString();

Response.Write(name);

}

}

}
 
Well the point I was making is that user controls and page are very similar in action even page is also a control. So transferring values from the user control to the page is same as transferring values from one page to another.


SingleTextBox.ascx
private void Page_Load(object sender, System.EventArgs e)
  {
   if(!Page.IsPostBack)
   {
 
   }
  }

  public string Text
  {
   get { return txtName.Text; }

   set { txtName.Text = value; }
  }

TesterSingleTextBox.aspx
  private void Page_Load(object sender, System.EventArgs e)
  {
   if(!Page.IsPostBack)
   {
    
    SingleTextBox s = new SingleTextBox();
    s.Text = "aaa"; // sets the value

    Response.Write(s.Text); // writes the value
    
    
 
   }
  }


Advantages of a Web User Control:
   The biggest advantage of the Web User controls is that they can be created as a site template and used through out the site. For example they can be made to contain the Menu/Link structure of a site and can be used at all the other aspx pages.

This means the following :

If the website introduces a new site-wide link within the current layout/structure, it is enough if we put it on the user control once. All pages will be updated once if the web user control is used in them.
If there is any link to be corrected, it can be done once at the server side.  
The .ascx files can either be used as a simple alternative to the plain HTML or they can also be used to respond to events: This means even custom code can be created against them and put in the code behind files.
Drawbacks / Disadvantages:
   Though the User controls offer a flexibility of having a site wide modifications, if the whole structure of the site changes, the HTML/aspx code of all the pages should be modified. But as long as the site maintains a same layout, then Web User controls are the number one choice for maintaining the generic layout of the site.

   Another disadvantage is It can not be just be simply referenced for using in a different project. If we want to use this User Control in a different project, we have to copy the file and modify the namespace to the host namespace name.


Composite Control
implement the INamingContainer interface which can create the controls with a unique id. INamingContainer interface achieves this by creating a unique namespace for the Custom server control placed on an aspx page.
 The CreateChildControls function should also be overridden


  [DefaultProperty("Text")]
    [ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]

http://msdn.microsoft.com/en-us/library/bb386420.aspx

 Managed-code modules that implement the IHttpModule interface have access to all events in the request pipeline. For example, a managed-code module can be used for ASP.NET forms authentication for both ASP.NET Web pages (.aspx files) and HTML pages (.htm or .html files). This is true even though HTML pages are treated as static resources by IIS and ASP.NET.


wcf:
            host.AddServiceEndpoint(typeof(IMetadataExchange),
                MetadataExchangeBindings.CreateMexTcpBinding(),
                "net.tcp://" + strLocal + ":" +
                (int.Parse(strPort) - 1).ToString() + "/WCFHostService/mex");
http://video.techrepublic.com.com/

http://whitepapers.techrepublic.com.com/abstract.aspx?docid=278949

posted on 2009-08-21 17:56  javaca88  阅读(241)  评论(0编辑  收藏  举报