第七章用户控件

***在web.config中进行注册用户控件

<system.web>

<pages>

<controls>

<add tagprefix="user" tagname="random" src=""、》

        
<controls>

</pages>

<system.web>

注意:采用此种方式注册,用户控件和使用控件的页面不要在一个文件夹。

***可以使用public属性,暴露用户控件的属性。

***暴露用户控件的事件

需要把信息传递给包含此控件的页面的时候。

即把用户的控件的事件包装然后在内容页再对此事件进行处理,在处理的同时可以得到这个控件点击动作时的一些状态。

public event EventHandler TabClick;

/// <summary>

    
/// This method executes when a user clicks a tab

    
/// </summary>

    
protected void dlstTabStrip_SelectedIndexChanged(object sender, EventArgs e)

    {

        
if (TabClick != null)

            TabClick(
this, EventArgs.Empty);

}

***

<fieldset>

<legend>

可以实现groupbox的效果。

***Ajax和用户控件

IE xmlhttp activex组件。

其他:xmlhttprequest

实现ajax的步骤:

1.创建一个客户端脚本来实现ajax调用。可以使用    Page.ClientScript.GetCallbackEventReference()来访问这个脚本,然后在页面中使用 Page.ClientScript.RegisterStartupScript()方法在页面中进行注册。

2.创建服务器端方法RaiseCallBackEvent()和GetCallBackResult(),它们用来从服务器端返回字符串。

3.创建客户端方法RaiseCallbackEvent()接受服务器端返回值并进行处理。

1.注意控件必须实现System.Web.UI.ICallbackEventHandler 接口。

<%@ Implements Interface="System.Web.UI.ICallbackEventHandler" %>

*** 动态加载用户控件,经常与PlaceHolder一起应用。

   

const string randomFolder = "~/FeaturedProducts";

    

    
protected void Page_Load(object sender, EventArgs e)

    {

        
string featuredProductPath = GetRandomProductPath();

        Control featuredProduct 
= Page.LoadControl(featuredProductPath);

        PlaceHolder1.Controls.Add(featuredProduct); 

    }


    
private string GetRandomProductPath()

    {

        Random rnd 
= new Random();

        
string[] files = Directory.GetFiles(MapPath(randomFolder), "*.ascx");

        
string featuredProductPath = Path.GetFileName(files[rnd.Next(files.Length)]);

        
return Path.Combine(randomFolder, featuredProductPath);

}

 
***创建多页面向导

可以使用动态加载不同的用户控件来实现。

原文地址:https://www.cnblogs.com/cpsing/p/1319498.html