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

自定义服务器控件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 "-//W 3C //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

     }

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值