若天明 发表于 2015-1-16 14:22:22

NET网页编程之在Asp.Net创立自界说控件实例

语言是不是不是最重要的?本章并没有解说在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
{


publicclassServerControl1:WebControl,INamingContainer
{
privatestringsqlconnectionstr=string.Empty;
privatestringcid;



值")]

publicstringSqlConnectionStr{
get
{
if(sqlconnectionstr==string.Empty)
returnConfigurationSettings.AppSettings["ConnectionString"];
returnsqlconnectionstr;
}
set{this.sqlconnectionstr=value;}
}

//是不是撑持绑定
//该属性的种别
//默许值

//申明

publicstringText
{
get
{
Strings=(String)ViewState["Text"];
return((s==null)?"":s);
}
}




publicobjectValue
{
get
{
objecto=(object)ViewState["Value"];
return(o==null)?"":o;
}
}





publicstringCID
{
get{returncid;}
set{cid=value;}
}




publicstringImageButtonImageUrl{
get
{
Stringi=(String)ViewState["ImageButtonImageUrl"];
return((i==null)?"":i);
}
set{ViewState["ImageButtonImageUrl"]=value;}
}




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.Value=this.CID;
DataSetpublic_querysetDataSet=ReturnDataSet(sqlstr,parameters);

if(ViewState["Ispostback"]==null)
{
string[]Fields=public_querysetDataSet.Tables.Rows["Fields"].ToString().Split(newchar[]{|},StringSplitOptions.RemoveEmptyEntries);
string[]DisplayFields=public_querysetDataSet.Tables.Rows["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.Rows["KeyFieldName"].ToString()};
}
this.Literal.Text=public_querysetDataSet.Tables.Rows["Title"].ToString();
DataTabledt=ReturnDataSet(public_querysetDataSet.Tables.Rows["SQLText"].ToString()).Tables;
for(inti=0;i<dt.Columns.Count;i++){
if(dt.Columns.ColumnName==public_querysetDataSet.Tables.Rows["ReturnValueField"].ToString()){
ViewState["ReturnValueFieldID"]=i.ToString();
}
if(dt.Columns.ColumnName==public_querysetDataSet.Tables.Rows["ReturnTextField"].ToString()){
ViewState["ReturnTextFieldID"]=i.ToString();
}
}
ViewState["Ispostback"]="false";
GridView.DataSource=ReturnDataSet(public_querysetDataSet.Tables.Rows["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.ToString())+1].Text;
ViewState["Value"]=row.Cells.ToString())+1].Text;
}

protectedvoidImageButton_Click(objectsender,EventArgse)
{
this.Panel.Visible=false;
}
}
}
中间码是基于一个虚拟机器。源代码是最高层的,理论上从源代码开始直接编译成本地码能提供最大优化的。而中间码只能是转译成本地码,效率上难免受到损耗。根据虚拟机器所设定的体系结构的特点,和本地机器的差异的多少。

仓酷云 发表于 2015-1-18 13:38:58

目前在微软的.net战略中新推出的ASP.net借鉴了Java技术的优点,使用CSharp(C#)语言作为ASP.net的推荐语言,同时改进了以前ASP的安全性差等缺点。但是,使用ASP/ASP.net仍有一定的局限性,因为从某种角度来说它们只能在微软的WindowsNT/2000/XP+IIS的服务器平台上良好运行(虽然像ChilliSoft提供了在UNIX/Linux上运行ASP的解决方案.

海妖 发表于 2015-1-26 16:43:29

那么,ASP.Net有哪些改进呢?

再现理想 发表于 2015-2-4 20:42:58

Asp.net:首先来说,Asp.net和Asp没什么关系,看着像是升级版本什么的,其实没什么联系。Asp是脚本编程,用的是ASP语言,而ASP.net用的是C#语言,完全不同的东西。

小魔女 发表于 2015-2-10 09:33:58

是目前ASP在UNIX/Linux上的应用可以说几乎为0)。所以平台的局限性和ASP自身的安全性限制了ASP的广泛应用。

金色的骷髅 发表于 2015-3-1 09:38:38

现在的ASP.net分为两个版本:1.1和2.0Asp.net1.1用VS2003(visualstudio2003)编程。Asp.net2.0用VS2005(visualstudio2005)编程。现在一般开发用的是VS2003。

兰色精灵 发表于 2015-3-10 16:51:44

CGI程序在运行的时候,首先是客户向服务器上的CGI程序发送一个请求,服务器接收到客户的请求后,就会打开一个新的Process(进程)来执行CGI程序,处理客户的请求。CGI程序最后将执行的结果(HTML页面代码)传回给客户。

愤怒的大鸟 发表于 2015-3-17 09:11:21

业务逻辑代码都不必做任何改动;继承性和多态性使得代码的可重用性大大提高,你可以通过继承已有的对象最大限度保护你以前的投资。并且C#和C++、Java一样提供了完善的调试/纠错体系。

飘飘悠悠 发表于 2015-3-24 05:47:03

现在的ASP.net分为两个版本:1.1和2.0Asp.net1.1用VS2003(visualstudio2003)编程。Asp.net2.0用VS2005(visualstudio2005)编程。现在一般开发用的是VS2003。
页: [1]
查看完整版本: NET网页编程之在Asp.Net创立自界说控件实例