本章并没有讲解
在Asp.net中创建自定义控件具体知识,只是给出自己以前做的一个自定义控件示例,方便大家在开发过程中做参考:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace CustomC
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:ServerControl1 runat=server ></{0}:ServerControl1 >")]
public class ServerControl1 :WebControl,INamingContainer
{
private string sqlconnectionstr=string.Empty;
private string cid;
[Category("Data")]
[DefaultValue("")]
[Description("默认取ConfigurationSettings.AppSettings[\"ConnectionString\"]值")]
[Browsable(true)]
public string SqlConnectionStr {
get
{
if (sqlconnectionstr == string.Empty)
return ConfigurationSettings.AppSettings["ConnectionString"];
return sqlconnectionstr;
}
set { this.sqlconnectionstr = value; }
}
[Bindable(true)] //是否支持绑定
[Category("Appearance")] //该属性的类别
[DefaultValue("")] //默认值
[Localizable(true)]
[Description("显示的值")] //说明
[Browsable(false)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? "" : s);
}
}
[Category("Appearance")]
[Description("要计算的值")]
[Browsable(false)]
public object Value
{
get
{
object o = (object)ViewState["Value"];
return (o == null) ? "" : o;
}
}
[Category("Appearance")]
[DefaultValue("")]
[Description("绑定数据的表名")]
[Browsable(true)]
public string CID
{
get { return cid; }
set { cid = value; }
}
[Category("Appearance")]
[Description("图片按钮的图片Url")]
[Browsable(true)]
public string ImageButtonImageUrl {
get
{
String i = (String)ViewState["ImageButtonImageUrl"];
return ((i == null) ? "" : i);
}
set { ViewState["ImageButtonImageUrl"] = value; }
}
[Category("Appearance")]
[Description("图片按钮的图片Css")]
[Browsable(true)]
public string ImageButtonStyle {
get {
String i = (String)ViewState["ImageButtonStyle"];
return ((i == null) ? "" : i);
}
set { ViewState["ImageButtonStyle"] = value; }
}
private TextBox TextBox = new TextBox();
private Button Button = new Button();
private GridView GridView=new GridView();
private Literal Literal = new Literal();
private Panel Panel = new Panel();
private ImageButton ImageButton = new ImageButton();
protected override void RenderContents(HtmlTextWriter output)
{
output.RenderBeginTag(HtmlTextWriterTag.Div);
this.TextBox.Text = this.Text;
RenderChildren(output);
output.RenderEndTag();
}
protected override void CreateChildControls()
{
base.CreateChildControls();
this.Controls.Add(this.TextBox);
this.Button.ID = "Btn";
this.Button.Text = "选择";
this.Button.Click += new EventHandler(Btn_Onclick);
this.Controls.Add(this.Button);
this.ImageButton.ImageUrl = ImageButtonImageUrl;
this.ImageButton.Attributes.Add("style", ImageButtonStyle);
this.ImageButton.Click +=new ImageClickEventHandler(ImageButton_Click);
this.GridView.RowCreated += new GridViewRowEventHandler(GridView_RowCreated);
this.GridView.PageIndexChanging += new GridViewPageEventHandler(GridView_PageIndexChanging);
this.GridView.SelectedIndexChanged += new EventHandler(GridView_SelectedIndexChanged);
this.GridView.RowDataBound += new GridViewRowEventHandler(GridView_RowDataBound);
this.Panel.Visible = false;
this.Panel.Attributes.Add("style", "border:1px solid #cccccc;position: absolute;z-index: 2000; background:#ffffff");
this.Panel.Controls.Add(this.Literal);
this.Panel.Controls.Add(this.ImageButton);
this.Panel.Controls.Add(this.GridView);
this.Controls.Add(this.Panel);
}
protected DataSet ReturnDataSet(string cmdText, params SqlParameter[] parameters)
{
DataSet DataSet = new DataSet();
using (SqlConnection conn = new SqlConnection(SqlConnectionStr))
{
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = cmdText;
cmd.Connection = conn;
foreach (SqlParameter para in parameters)
{
cmd.Parameters.Add(para);
}
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(DataSet, "ds");
cmd.Parameters.Clear();
cmd.Dispose();
}
return DataSet;
}
protected void Btn_Onclick(object sender, EventArgs e)
{
this.Panel.Visible = true;
string sqlstr = "select top 1 ID,Title,SQLText,KeyFieldName,Fields,DisplayFields,ReturnValueField,ReturnTextField from Public_QuerySet where ID=@ID";
SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID",SqlDbType.VarChar,50 ) };
parameters[0].Value = this.CID;
DataSet public_querysetDataSet = ReturnDataSet(sqlstr, parameters);
if (ViewState["Ispostback"]==null)
{
string[] Fields = public_querysetDataSet.Tables[0].Rows[0]["Fields"].ToString().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
string[] DisplayFields = public_querysetDataSet.Tables[0].Rows[0]["DisplayFields"].ToString().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < Fields.Length; i++)
{
BoundField namecolumn = new BoundField();
namecolumn.DataField = Fields[i];
namecolumn.HeaderText = DisplayFields[i];
GridView.Columns.Add(namecolumn);
}
GridView.Visible = true;
GridView.AutoGenerateColumns = false;
GridView.AllowPaging = true;
GridView.AutoGenerateSelectButton = true;
GridView.DataKeyNames = new string[] { public_querysetDataSet.Tables[0].Rows[0]["KeyFieldName"].ToString() };
}
this.Literal.Text =public_querysetDataSet.Tables[0].Rows[0]["Title"].ToString();
DataTable dt = ReturnDataSet(public_querysetDataSet.Tables[0].Rows[0]["SQLText"].ToString()).Tables[0];
for (int i = 0; i < dt.Columns.Count; i++) {
if (dt.Columns[i].ColumnName == public_querysetDataSet.Tables[0].Rows[0]["ReturnValueField"].ToString()) {
ViewState["ReturnValueFieldID"] = i.ToString();
}
if (dt.Columns[i].ColumnName == public_querysetDataSet.Tables[0].Rows[0]["ReturnTextField"].ToString()) {
ViewState["ReturnTextFieldID"] = i.ToString();
}
}
ViewState["Ispostback"] = "false";
GridView.DataSource = ReturnDataSet(public_querysetDataSet.Tables[0].Rows[0]["SQLText"].ToString());
GridView.DataBind();
}
protected void GridView_RowCreated(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor= '#cccccc';this.style.cursor= 'hand';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
//e.Row.Attributes.Add("onClick", "javascript:__doPostBack('ServerControl11$" + GridView.ID + "','Select$" + e.Row.RowIndex + "');");
}
}
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onClick", "javascript:__doPostBack('ServerControl11$" + GridView.ID + "','Select$" + e.Row.RowIndex + "');");
}
}
protected void GridView_PageIndexChanging(object sender,GridViewPageEventArgs e) {
Btn_Onclick(sender, e);
GridView.PageIndex = e.NewPageIndex;
GridView.DataBind();
}
protected void GridView_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView.SelectedRow;
ViewState["Text"] = row.Cells[int.Parse(ViewState["ReturnTextFieldID"].ToString()) + 1].Text;
ViewState["Value"] = row.Cells[int.Parse(ViewState["ReturnValueFieldID"].ToString()) + 1].Text;
}
protected void ImageButton_Click(object sender, EventArgs e)
{
this.Panel.Visible = false;
}
}
}
.