ASP自定义控件的两种实现方法

类别:.NET开发 点击:0 评论:0 推荐:

1、在HTML中直接调用后台CS代码中的protected成员变量

自定义控件类的HTML代码:

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="MyWebUserControl.ascx.cs" Inherits="WebTestRollback.MyWebUserControl" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<table width =100%>
 <tr bgcolor=#0099ff>
 <%
  IDictionaryEnumerator objEnumerator = m_htMenu.GetEnumerator();
  while (objEnumerator.MoveNext())
  {
 %>
  <td width=10%><a href="<%=(string)objEnumerator.Value%>"><%=(string)objEnumerator.Key%></a>
  </td>
 <%
  }
 %>
 <td></td>
 </tr>
</table>

自定义控件类CS代码:

namespace WebTestRollback
{
 using System;
 using System.Data;
 using System.Drawing;
 using System.Web;
 using System.Web.UI.WebControls;
 using System.Web.UI.HtmlControls;
 using System.Collections;

 /// <summary>
 /// </summary>
 public class MyWebUserControl : System.Web.UI.UserControl
 {
  protected Hashtable m_htMenu = new Hashtable();

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

  public void AddMenu(string strName,string strLink)
  {
   m_htMenu.Add(strName,strLink);
  }

 }
}

2、在自定义控件类中重写Render方法(),此方法不需要控件的HTML代码,只需在Render方法中变相的添加HTML代码,控件的CS代码如下:

namespace WebTestRollback
{
 using System;
 using System.Data;
 using System.Drawing;
 using System.Web;
 using System.Web.UI.WebControls;
 using System.Web.UI.HtmlControls;
 using System.Collections;

 /// <summary>
 /// </summary>
 public class SubWebUserControl : System.Web.UI.UserControl
 {
  protected Hashtable m_htMenu = new Hashtable();

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

  public void AddMenu(string strName,string strLink)
  {
   m_htMenu.Add(strName,strLink);
  }

  protected override void Render(System.Web.UI.HtmlTextWriter writer)
  {
   base.Render (writer);
   writer.Write( @"<table>
        <tr>");
   IDictionaryEnumerator objEnumerator = m_htMenu.GetEnumerator();
   while (objEnumerator.MoveNext())
   {
    writer.Write( "<td><a href=\""
                + (string)objEnumerator.Value + "\">" 
                + (string)objEnumerator.Key
                + "</a></td>");
   }
   writer.Write(@"  <tr> <table>");   
  }
 }
}

两种方法对于自定义控件用户是透明的,使用方法上没有任何区别:

自定义控件用户HTML代码:

<%@ Page language="c#" Codebehind="TestLogin.aspx.cs" AutoEventWireup="false" Inherits="WebTestRollback.TestLogin" %>
<%@ Register TagPrefix="TT" TagName="CC" Src="MyWebUserControl.ascx"%> 
<%@ Register TagPrefix="T2" TagName="C2" Src="SubWebUserControl.ascx"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>TestLogin</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body bgColor="#33cccc" leftMargin="0" topMargin="0" rightMargin="0" MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" Runat="server">
   <TT:CC id="m_TTCC" runat="server"></TT:CC>
   <T2:C2 id="m_T2C2" runat="server"></T2:C2>
</form>
</body>
</HTML>

自定义控件用户CS代码:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebTestRollback
{
 /// <summary>
 /// </summary>
 public class TestLogin : System.Web.UI.Page
 {
  public MyWebUserControl m_TTCC = new MyWebUserControl();
  public SubWebUserControl m_T2C2 = new SubWebUserControl();

  private void Page_Load(object sender, System.EventArgs e)
  {
   m_TTCC.AddMenu("File","news.sohu.com");
   m_TTCC.AddMenu("Edit","hotmail.com");

   m_T2C2.AddMenu("Project","project.com");
   m_T2C2.AddMenu("Built","built.com");
  }

 }
}

本文地址:http://com.8s8s.com/it/it40969.htm