蛙蛙推荐:第一个实际应用中开发的自定义web服务器控件

原创 2005年03月03日 16:02:00

自定义服务器控件ViewSuo的源文件

 

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.ComponentModel;

using System.Collections;

using System.Diagnostics;

using System.Data;

using System.Drawing;

 

namespace oa.cls

{

 

     /// <summary>

     /// 查看事务所详细信息的自定义服务器控件

     /// </summary>

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

     public class ViewSuo : System.Web.UI.WebControls.WebControl, INamingContainer

     {

         private DataSet dataSource;

 

         /// <summary>

         /// 设置数据源

         /// </summary>

         [Bindable(true),

         Category("Data"),

         DefaultValue(null),

         Description("设置数据源"),

         DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)

         ]

         public DataSet DataSource

         {

              get

              {

                   return dataSource;

              }

              set

              {

                   dataSource = value;

              }

         }

 

          protected override void Render(HtmlTextWriter output)

         {

              PrepareControlHierarchy();

              RenderContents(output);

         }

 

         private void PrepareControlHierarchy()

         {

              if (HasControls() == false)

                   return;

         }

 

         protected override void CreateChildControls() 

         {

              Controls.Clear();

              if (Page.IsPostBack)

                   CreateControlHierarchy(false);

         }

 

         public override void DataBind()

         {

              base.OnDataBinding(EventArgs.Empty);

              Controls.Clear();

              ClearChildViewState();

 

              CreateControlHierarchy(true);

              ChildControlsCreated = true;

         }

 

         protected override void TrackViewState()

         {

              base.TrackViewState();

         }

 

         /// <summary>

         /// 这里是主要实现输出的方法,DataList来完成输出,模板SuoTemplate是详细实现细节.

         /// 其它部分都可以套用,自定义的属性都在这里设置,比如说DataList显示的列数,,高等.

         /// </summary>

          /// <param name="useDataSource"></param>

         private void CreateControlHierarchy( bool useDataSource )

         {            

              DataList dl = new DataList();

              dl.RepeatColumns = 1;

              dl.RepeatDirection = RepeatDirection.Vertical;

              dl.ID = "productList";

              dl.ItemTemplate = new SuoTemplate();

              dl.ItemStyle.Width = new Unit(500, UnitType.Pixel);

              dl.Width = new Unit(650, UnitType.Pixel);

              Controls.Add( dl );             

 

              if (useDataSource == true )

              {

                   dl.DataSource = dataSource.Tables[0];

                   dl.DataBind();

              }

         }

     }

}

 

ViewSuo控件中用到的SuoTemplate模板的源文件

 

using System;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.ComponentModel;

using System.Collections;

using System.Diagnostics;

using System.Data;

using System.Drawing;

using System.Text;

 

namespace oa.cls

{

 

     public class SuoTemplate : ITemplate

     {

         public SuoTemplate() { }

 

         public void InstantiateIn( Control container )

         {

              Literal l = new Literal();

              l.DataBinding += new EventHandler(this.BindData);

              container.Controls.Add( l );

         }

 

         /// <summary>

         /// 在这个方法里控制模板的主要输出,其中用到了数据绑定的一些内容

         /// 要改变显示在这里修改

         /// </summary>

         /// <param name="sender"></param>

         /// <param name="e"></param>

         public void BindData( object sender, EventArgs e)

         {

              Literal l = (Literal)sender;

              DataListItem container = (DataListItem)l.NamingContainer;

              DataRowView currentRowView = (DataRowView) container.DataItem;

 

              //HttpContext ht = HttpContext.Current;

 

              StringBuilder sb = new StringBuilder();

              sb.Append("<table width=/"100%/"  border=/"0/" align=/"center/">");

              sb.Append("  <tr>");

              sb.Append("    <td width=/"17%/" align=/"right/"><strong>事务所名称:</strong></td>");

              sb.Append("    <td width=/"26%/">"+currentRowView["name"]+"</td>");

              sb.Append("    <td width=/"19%/" align=/"right/"><strong>主任:</strong></td>");

              sb.Append("    <td width=/"38%/">"+currentRowView["zhuren"]+"</td>");

              sb.Append("  </tr>");

              sb.Append("  <tr>");

              sb.Append("    <td align=/"right/"><strong>许可证号:</strong></td>");

              sb.Append("    <td>"+currentRowView["zhiye"]+"</td>");

              sb.Append("    <td align=/"right/"><strong>&nbsp;&nbsp;&nbsp;&nbsp;:</strong></td>");

              sb.Append("    <td>"+currentRowView["bianzhi"]+"</td>");

              sb.Append("  </tr>");

              sb.Append("  <tr>");

              sb.Append("    <td align=/"right/"><strong>组织形式:</strong></td>");

              sb.Append("    <td>"+currentRowView["zuzhi"]+"</td>");

              sb.Append("    <td align=/"right/"><strong>隶属关系:</strong></td>");

              sb.Append("    <td>"+currentRowView["lishu"]+"</td>");

              sb.Append("  </tr>");

              sb.Append("  <tr>");

              sb.Append("    <td align=/"right/"><strong>主管机关:</strong></td>");

              sb.Append("    <td>"+currentRowView["zhuguan"]+"</td>");

              sb.Append("    <td align=/"right/"><strong>&nbsp;&nbsp;&nbsp;&nbsp;:</strong></td>");

              sb.Append("    <td>"+currentRowView["nianshen"]+"</td>");

              sb.Append("  </tr>");

              sb.Append("</table>");

              sb.Append("<table width=/"100%/"  border=/"0/" align=/"center/">");

              sb.Append("  <tr>");

              sb.Append("    <td width=/"17%/" align=/"right/"><strong>设立时间:</strong></td>");

              sb.Append("    <td width=/"26%/">"+currentRowView["sheli"]+"</td>");

              sb.Append("    <td width=/"19%/" align=/"right/"><strong>&nbsp;&nbsp;&nbsp;&nbsp;:</strong></td>");

              sb.Append("    <td width=/"38%/">"+currentRowView["quyu"]+"</td>");

              sb.Append("  </tr>");

              sb.Append("  <tr>");

              sb.Append("    <td height=/"16/" align=/"right/"><strong>&nbsp;&nbsp;&nbsp;&nbsp;:</strong></td>");

              sb.Append("    <td>"+currentRowView["lvshi"]+"</td>");

              sb.Append("    <td align=/"right/"><strong>&nbsp;&nbsp;&nbsp;&nbsp;:</strong></td>");

              sb.Append("    <td>"+currentRowView["phone"]+"</td>");

              sb.Append("  </tr>");

              sb.Append("  <tr>");

              sb.Append("    <td align=/"right/"><strong>律师助理:</strong></td>");

              sb.Append("    <td>&nbsp;</td>");

              sb.Append("    <td align=/"right/"><strong>&nbsp;&nbsp;&nbsp;&nbsp;:</strong></td>");

              sb.Append("    <td>"+currentRowView["zhuli"]+"</td>");

              sb.Append("  </tr>");

              sb.Append("  <tr>");

              sb.Append("    <td align=/"right/"><strong>行政人员:</strong></td>");

              sb.Append("    <td>"+currentRowView["xingzheng"]+"</td>");

              sb.Append("    <td align=/"right/"><strong>&nbsp;&nbsp;&nbsp;&nbsp;:</strong></td>");

              sb.Append("    <td>"+currentRowView["huji"]+"</td>");

              sb.Append("  </tr>");

              sb.Append("  <tr>");

              sb.Append("    <td align=/"right/"><strong>专业资格:</strong></td>");

              sb.Append("    <td>"+currentRowView["zige"]+"</td>");

              sb.Append("    <td align=/"right/"><strong>主任手机:</strong></td>");

              sb.Append("    <td>"+currentRowView["shouji"]+"</td>");

              sb.Append("  </tr>");

              sb.Append("  <tr>");

              sb.Append("    <td align=/"right/"><strong>固定资产:</strong></td>");

              sb.Append("    <td>"+currentRowView["zichan"]+"</td>");

              sb.Append("    <td align=/"right/"><strong>&nbsp;&nbsp;&nbsp;&nbsp;:</strong></td>");

              sb.Append("    <td>"+currentRowView["zip"]+"</td>");

              sb.Append("  </tr>");

              sb.Append("  <tr>");

              sb.Append("    <td align=/"right/"><strong>&nbsp;&nbsp;&nbsp;&nbsp;:</strong></td>");

              sb.Append("    <td>"+currentRowView["jingfei"]+"</td>");

              sb.Append("    <td align=/"right/"><strong>&nbsp;&nbsp;&nbsp;&nbsp;:</strong></td>");

              sb.Append("    <td>"+currentRowView["dizhi"]+"</td>");

              sb.Append("  </tr>");

              sb.Append("  <tr>");

              sb.Append("   <td align=/"right/"><strong>分所情况:</strong></td>");

              sb.Append("   <td colspan=/"3/">"+currentRowView["fensuo"]+"</td>");

              sb.Append("  </tr>");

              sb.Append("</table>");

              l.Text = sb.ToString();

         }

     }

    

}

 

测试自定义控件的前台页代码

 

<%@ Page language="c#" Codebehind="0.aspx.cs" AutoEventWireup="false" Inherits="oa._0" %>

<%@ Register TagPrefix="wawa" Namespace="oa.cls" Assembly="oa"%>

<!--在使用自定义控件前需要先用Register关键字注册,Assembly属性设置应用程序的名字就可以,就是BIN目录下的那个主DLL的名字->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

       <HEAD>

              <title>0</title>

              <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">

              <meta name="CODE_LANGUAGE" Content="C#">

              <meta name="vs_defaultClientScript" content="JavaScript">

              <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">

       </HEAD>

       <body MS_POSITIONING="FlowLayout">

              <form id="Form1" method="post" runat="server">

              <!--在这里放置一个自定义控件-->

                     <wawa:ViewSuo id="suo1" runat="server" />

              </form>

       </body>

</HTML>

 

页面后台代码

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 oa

{

 

     public class _0 : System.Web.UI.Page

     {

         protected oa.cls.ViewSuo suo1;

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

         {

              if(!this.IsPostBack)

              {

                   //为自定义web控件设置数据源并绑定数据

                   suo1.DataSource=oa.cls.globalstate.GetDataSetByStr("select top 1 * from suo",null);

                   suo1.DataBind();

              }

         }

 

         #region Web 窗体设计器生成的代码

         override protected void OnInit(EventArgs e)

         {

 

              InitializeComponent();

              base.OnInit(e);

         }

        

         private void InitializeComponent()

         {   

              this.Load += new System.EventHandler(this.Page_Load);

 

         }

         #endregion

     }

}

 

 

相关文章推荐

ASP.NET 部署和使用自定义 Web 服务器控件

本演练演示如何创建和测试自定义 ASP.NET Web 服务器控件。 在本演练中,您将学会如何执行以下任务: 创建一个 ASP.NET Web 服务器控件。 通过向该控件添加元数据来...

ASP.NET自定义Web服务器控件-Button

using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin...

ASP.NET自定义Web服务器控件-login登录控件

using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin...

ASP.NET自定义Web服务器控件-TextBox文本框控件

using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin...

ASP.NET自定义Web服务器控件-DropDownList/Select下拉列表控件

using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin...

ASP.NET自定义Web服务器控件-Button控件

using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin...

ASP.NET自定义Web服务器控件-登录控件

using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; ...

ASP.NET自定义Web服务器控件-Div控件

using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin...

.ASP.NET自定义服务器控件 第4章 组合控件开发CompositeControl

大家好,今天我们来实现一个自定义的控件,之前我们已经知道了,要开发自定义的控件一般继承三个基 类:Control,WebControl,还有一个就是今天要说的CompositeControl。   ...
  • yl_99
  • yl_99
  • 2012年09月12日 16:15
  • 662

ASP.NET 自定义服务器控件的使用

最近在做一个网站后台,想要用 这种形式来直接读取数据库的数据,于是研究了一下服务器控件。 首先,先新建一个WebCustomControl1类(WEB窗体服务器控件) VS默认生成的代...
  • zxq0610
  • zxq0610
  • 2016年01月23日 10:45
  • 89
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:蛙蛙推荐:第一个实际应用中开发的自定义web服务器控件
举报原因:
原因补充:

(最多只允许输入30个字)