仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 880|回复: 8
打印 上一主题 下一主题

[学习教程] NET网页编程之在Asp.Net创立自界说控件实例

[复制链接]
若天明 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 14:22:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

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;
}
}
}
中间码是基于一个虚拟机器。源代码是最高层的,理论上从源代码开始直接编译成本地码能提供最大优化的。而中间码只能是转译成本地码,效率上难免受到损耗。根据虚拟机器所设定的体系结构的特点,和本地机器的差异的多少。
沙发
发表于 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#语言,完全不同的东西。
小魔女 该用户已被删除
5#
发表于 2015-2-10 09:33:58 | 只看该作者
是目前ASP在UNIX/Linux上的应用可以说几乎为0)。所以平台的局限性和ASP自身的安全性限制了ASP的广泛应用。
金色的骷髅 该用户已被删除
6#
发表于 2015-3-1 09:38:38 | 只看该作者
现在的ASP.net分为两个版本:1.1和2.0Asp.net1.1用VS2003(visualstudio2003)编程。Asp.net2.0用VS2005(visualstudio2005)编程。现在一般开发用的是VS2003。
兰色精灵 该用户已被删除
7#
发表于 2015-3-10 16:51:44 | 只看该作者
CGI程序在运行的时候,首先是客户向服务器上的CGI程序发送一个请求,服务器接收到客户的请求后,就会打开一个新的Process(进程)来执行CGI程序,处理客户的请求。CGI程序最后将执行的结果(HTML页面代码)传回给客户。
愤怒的大鸟 该用户已被删除
8#
发表于 2015-3-17 09:11:21 | 只看该作者
业务逻辑代码都不必做任何改动;继承性和多态性使得代码的可重用性大大提高,你可以通过继承已有的对象最大限度保护你以前的投资。并且C#和C++、Java一样提供了完善的调试/纠错体系。
飘飘悠悠 该用户已被删除
9#
发表于 2015-3-24 05:47:03 | 只看该作者
现在的ASP.net分为两个版本:1.1和2.0Asp.net1.1用VS2003(visualstudio2003)编程。Asp.net2.0用VS2005(visualstudio2005)编程。现在一般开发用的是VS2003。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-16 03:43

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表