如何编写自定义的Web控件
建一個自定義的Web控件MyContro的步驟:
1)引用
using System;
using System.IO;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
2)保證MyContro是從WebControl繼承的。
public class MyControl:WebControl
3)重寫下面兩個函數,這是必須的
protected override void Render(HtmlTextWriter writer)
{
this.CreateChildControls();
base.Render (writer);
}
protected override void CreateChildControls()
{
// 清除現有的子控件及其 ViewState
this.Controls.Clear();
this.ClearChildViewState();
// 生成控件樹
// 生成環境表格(一行,兩個單元格)
Table myTable = new Table();
//build the table row生成表格中的行
TableRow row = new TableRow();
myTable.Rows.Add(row);
// 生成單元格
TableCell myCell = new TableCell();
//用來生成鏈接按鈕導航條的代碼。每個按鈕都顯示有一個 Webdings 字符,可以根據需//禁用,并被綁定到內部的 Click 事件處理程序。
LinkButton myLinkButton = new LinkButton();
myLinkButton.ID = "MyLinkButton";
myLinkButton.Click += new EventHandler(myLinkButton_Click);
myLinkButton.Font.Name = "宋體";
myLinkButton.ToolTip = "好玄啊!";
myLinkButton.Text = "請點我";
myCell.Controls.Add(myLinkButton);
row.Cells.Add(myCell);
Controls.Add(t);
}
4)自定義的事件的方法
private void myLinkButton_Click(object sender,System.EventArgs e)
{
Page.Response.Write("想干點什么就寫點什么吧,就這么簡單!");
}
5)編譯一下,然后點工具—》添加/移除工具箱項
瀏覽到你編譯生成的dll
6)使用
?
/Files/hayden/自定義控件示例.rar
轉載于:https://www.cnblogs.com/hayden/archive/2009/12/23/1630996.html
總結
以上是生活随笔為你收集整理的如何编写自定义的Web控件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在shell中全局变量与局部变量的区别及
- 下一篇: 如何选择国外虚拟主机空间服务商