|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
语言是不是不是最重要的?本章并没有解说在Asp.net中创立自界说控件详细常识,只是给出本人之前做的一个自界说控件示例,便利人人在开辟过程当中做参考:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Configuration;
namespaceCustomC
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:ServerControl1runat=server></{0}:ServerControl1>")]
publicclassServerControl1:WebControl,INamingContainer
{
privatestringsqlconnectionstr=string.Empty;
privatestringcid;
[Category("Data")]
[DefaultValue("")]
[Description("默许取ConfigurationSettings.AppSettings["ConnectionString"]值")]
[Browsable(true)]
publicstringSqlConnectionStr{
get
{
if(sqlconnectionstr==string.Empty)
returnConfigurationSettings.AppSettings["ConnectionString"];
returnsqlconnectionstr;
}
set{this.sqlconnectionstr=value;}
}
[Bindable(true)]//是不是撑持绑定
[Category("Appearance")]//该属性的种别
[DefaultValue("")]//默许值
[Localizable(true)]
[Description("显现的值")]//申明
[Browsable(false)]
publicstringText
{
get
{
Strings=(String)ViewState["Text"];
return((s==null)?"":s);
}
}
[Category("Appearance")]
[Description("要盘算的值")]
[Browsable(false)]
publicobjectValue
{
get
{
objecto=(object)ViewState["Value"];
return(o==null)?"":o;
}
}
[Category("Appearance")]
[DefaultValue("")]
[Description("绑定命据的表名")]
[Browsable(true)]
publicstringCID
{
get{returncid;}
set{cid=value;}
}
[Category("Appearance")]
[Description("图片按钮的图片Url")]
[Browsable(true)]
publicstringImageButtonImageUrl{
get
{
Stringi=(String)ViewState["ImageButtonImageUrl"];
return((i==null)?"":i);
}
set{ViewState["ImageButtonImageUrl"]=value;}
}
[Category("Appearance")]
[Description("图片按钮的图片CSS")]
[Browsable(true)]
publicstringImageButtonStyle{
get{
Stringi=(String)ViewState["ImageButtonStyle"];
return((i==null)?"":i);
}
set{ViewState["ImageButtonStyle"]=value;}
}
privateTextBoxTextBox=newTextBox();
privateButtonButton=newButton();
privateGridViewGridView=newGridView();
privateLiteralLiteral=newLiteral();
privatePanelPanel=newPanel();
privateImageButtonImageButton=newImageButton();
protectedoverridevoidRenderContents(HtmlTextWriteroutput)
{
output.RenderBeginTag(HtmlTextWriterTag.Div);
this.TextBox.Text=this.Text;
RenderChildren(output);
output.RenderEndTag();
}
protectedoverridevoidCreateChildControls()
{
base.CreateChildControls();
this.Controls.Add(this.TextBox);
this.Button.ID="Btn";
this.Button.Text="选择";
this.Button.Click+=newEventHandler(Btn_Onclick);
this.Controls.Add(this.Button);
this.ImageButton.ImageUrl=ImageButtonImageUrl;
this.ImageButton.Attributes.Add("style",ImageButtonStyle);
this.ImageButton.Click+=newImageClickEventHandler(ImageButton_Click);
this.GridView.RowCreated+=newGridViewRowEventHandler(GridView_RowCreated);
this.GridView.PageIndexChanging+=newGridViewPageEventHandler(GridView_PageIndexChanging);
this.GridView.SelectedIndexChanged+=newEventHandler(GridView_SelectedIndexChanged);
this.GridView.RowDataBound+=newGridViewRowEventHandler(GridView_RowDataBound);
this.Panel.Visible=false;
this.Panel.Attributes.Add("style","border:1pxsolid#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);
}
protectedDataSetReturnDataSet(stringcmdText,paramsSqlParameter[]parameters)
{
DataSetDataSet=newDataSet();
using(SqlConnectionconn=newSqlConnection(SqlConnectionStr))
{
if(conn.State==ConnectionState.Closed)
conn.Open();
SqlCommandcmd=newSqlCommand();
cmd.CommandType=CommandType.Text;
cmd.CommandText=cmdText;
cmd.Connection=conn;
foreach(SqlParameterparainparameters)
{
cmd.Parameters.Add(para);
}
SqlDataAdapterda=newSqlDataAdapter(cmd);
da.Fill(DataSet,"ds");
cmd.Parameters.Clear();
cmd.Dispose();
}
returnDataSet;
}
protectedvoidBtn_Onclick(objectsender,EventArgse)
{
this.Panel.Visible=true;
stringsqlstr="selecttop1ID,Title,SQLText,KeyFieldName,Fields,DisplayFields,ReturnValueField,ReturnTextFieldfromPublic_QuerySetwhereID=@ID";
SqlParameter[]parameters=newSqlParameter[]{newSqlParameter("@ID",SqlDbType.VarChar,50)};
parameters[0].Value=this.CID;
DataSetpublic_querysetDataSet=ReturnDataSet(sqlstr,parameters);
if(ViewState["Ispostback"]==null)
{
string[]Fields=public_querysetDataSet.Tables[0].Rows[0]["Fields"].ToString().Split(newchar[]{|},StringSplitOptions.RemoveEmptyEntries);
string[]DisplayFields=public_querysetDataSet.Tables[0].Rows[0]["DisplayFields"].ToString().Split(newchar[]{|},StringSplitOptions.RemoveEmptyEntries);
for(inti=0;i<Fields.Length;i++)
{
BoundFieldnamecolumn=newBoundField();
namecolumn.DataField=Fields;
namecolumn.HeaderText=DisplayFields;
GridView.Columns.Add(namecolumn);
}
GridView.Visible=true;
GridView.AutoGenerateColumns=false;
GridView.AllowPaging=true;
GridView.AutoGenerateSelectButton=true;
GridView.DataKeyNames=newstring[]{public_querysetDataSet.Tables[0].Rows[0]["KeyFieldName"].ToString()};
}
this.Literal.Text=public_querysetDataSet.Tables[0].Rows[0]["Title"].ToString();
DataTabledt=ReturnDataSet(public_querysetDataSet.Tables[0].Rows[0]["SQLText"].ToString()).Tables[0];
for(inti=0;i<dt.Columns.Count;i++){
if(dt.Columns.ColumnName==public_querysetDataSet.Tables[0].Rows[0]["ReturnValueField"].ToString()){
ViewState["ReturnValueFieldID"]=i.ToString();
}
if(dt.Columns.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();
}
protectedvoidGridView_RowCreated(objectsender,GridViewRowEventArgse){
if(e.Row.RowType==DataControlRowType.DataRow){
e.Row.Attributes.Add("onmou搜索引擎优化ver","this.style.backgroundColor=#cccccc;this.style.cursor=hand;");
e.Row.Attributes.Add("onmou搜索引擎优化ut","this.style.backgroundColor=");
//e.Row.Attributes.Add("onClick","javascript:__doPostBack(ServerControl11$"+GridView.ID+",Select$"+e.Row.RowIndex+");");
}
}
protectedvoidGridView_RowDataBound(objectsender,GridViewRowEventArgse)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onClick","javascript:__doPostBack(ServerControl11$"+GridView.ID+",Select$"+e.Row.RowIndex+");");
}
}
protectedvoidGridView_PageIndexChanging(objectsender,GridViewPageEventArgse){
Btn_Onclick(sender,e);
GridView.PageIndex=e.NewPageIndex;
GridView.DataBind();
}
protectedvoidGridView_SelectedIndexChanged(objectsender,EventArgse)
{
GridViewRowrow=GridView.SelectedRow;
ViewState["Text"]=row.Cells[int.Parse(ViewState["ReturnTextFieldID"].ToString())+1].Text;
ViewState["Value"]=row.Cells[int.Parse(ViewState["ReturnValueFieldID"].ToString())+1].Text;
}
protectedvoidImageButton_Click(objectsender,EventArgse)
{
this.Panel.Visible=false;
}
}
}
中间码是基于一个虚拟机器。源代码是最高层的,理论上从源代码开始直接编译成本地码能提供最大优化的。而中间码只能是转译成本地码,效率上难免受到损耗。根据虚拟机器所设定的体系结构的特点,和本地机器的差异的多少。 |
|