【asp源码栏目提醒】:网学会员在asp源码频道为大家收集整理了“ASP总结 - 计算机等级“提供大家参考,希望对大家有所帮助!
1 跳转页面的指令是:response.redirectquota.aspxquot 2 dataset 是 ado.net 的断开式空间,对 dataset 的任何操作,都是在 web 服务器缓存中进行。
3 executenonquery()执行一个 SQL 语句; Read()查询数据库返回一个数据流,一次读一条记录。
4 如何读取 web.config 信息: 通过引用 system.configuration适用于特定计算机、 应用程序或资源的配置文件 5 如何判断页面是否首次加载还是重发:判断 Page.IsPostBack,如果为 false 则为首次加载,否则为重发。
首次加载页面可表示为:Page.IsPostBack Load:加载页面时发生。
protected void Page_Loadobject sender EventArgs e 6 page 类表示从
ASP.NET Web 应用程序的宿主服务器请求的 .aspx 文件。
lt Page LanguagequotCquot AutoEventWireupquottruequot CodeFilequotDefault6.aspx.csquot InheritsquotDefault6quot gt LanguagequotCquot -- 所用的编程语言 AutoEventWireup -- 表示控制 TextBox 控件中自动完成功能的行为的值。
CodeFile -- 源代码文件名 inherits -- 所属的类 7 验证控制控件: 1》comparevalidator 将由用户输入到输入控件的值与输入到其他输入控件的值或常数值进行比较 2》customvalidator 提供可以对另一个控件执行自定义验证的控件。
3》rangevalidator 验证另一控件值是否在允许的范围内 4》regularexpressionvalidator 提供用于验证另一控件的值是否与所提供的正则表达式相匹配的控件功能。
5》requiredfiledvalidator 提供控件功能以验证关联的输入控件的值是否不同于其初始值。
6》validationsummary 显示窗体上所发生的所有验证错误的摘要。
7》EnableClientScript:false关闭客户端验证。
8 导航控件:1》站点地图:用来描述网站中包含的页面情况的文件,该文件中包含网站中每个web页的名字及它们之间的层次关系。
是一个名为Web.sitemap的XML文件。
2》sitemappath 在相应的web窗体上显示一个导航路径,此路径以主页的位置为起始位置,当前页的位置为终止位置的一个路径。
3》menu控件 4》treeview控件 10 sqldatareader的作用和特点:提供一种从 SQL Server 数据库读取行的只进流的方式。
特点是:无法继承此类,属于长连接。
用来从数据库中读取数据, 实现对数据源的只读访问, 它是面向连接的、只向前的、游标样式的类。
由于datareader不需要以随机的方式访问数据库, 所以不会增加系统的额外开销。
与dataset相比, 如果执行纯粹的读操作,datareader的速度要快。
11 运行机制:web应用程序的运行是由客户端发出request请求,服务器响应response的CS应用程序。
首先,来自客户端的请求交个iis,iis查看请求类型,判断为
asp.net类型,于是交给
asp.net,由dll对请求项处理后转变为html代码交给客户端。
12 HTML控件和Web服务控件的区别: HTML控件是在客户端浏览器解释的HTML元素,并不是控件,不能编写代码。
ASP.NET服务器控件是运行在服务器端并且安装了用户界面和其它相关功能的组件,能够编写程序进行控制。
13 母版和普通页面的区别与联系: 1》母版页的扩展名为.master普通页面的扩展名为.page 2》母版页继承的是masterpage类,而普通页面继承的是page类。
模板也至少包含一个ContentPlaceHolder。
关系:普通也可以从母版页生成也可以从空页面生成。
(先建一个母版页,把共同的东西都建好,通过添加ContentPlaceHolder控件预留可变化的区域。
然后在各个内容页里使用母版页(通过在声明里引用母版页) ,原来的ContentPlaceHolder控件会被Content控件替换,然后你可以在Content控件里添加每个内容页个性化的东西。
) 14 页面的传值方式: 1》request.querystring 利用 url 传递参数,并获取参数的内容。
Request.querystringquot字段名称quot 举例: lta href“a.aspxname张三ampage22”gta.aspxlt/agtusing system public partial calss_6_3reques_url:system.web.ui.page protected void page_loadobject sendereventargs e label1.text request.querystringquotUsernamequotquot欢迎您!quot protected void textbox1_textchangedobjectsendereventargse hyperlink1.navigateurlquotrequest-url.aspxUsernamequottextbox1.text 2》利用 session 传值:Session 的特点为:的数据保存在服务器端、可以保存任意类型的数据、默认的生命周期是20分钟,可以手动设置更长或更短的时间,使用 Session 保存数据后调用该数据时返回值是 object;举例:protectedvoidbutton1_clickobjectsendereventargsesessionquotnamequottextbox1.textresponse.redirectquotweb.aspxquotSession“name”Text.Box1.Text 指将 TextBox.Text 的值写入 Session,在 Session 失效之前默认为20分钟可通过 Session“name”获取他的值protected void page_loadobject sendereventargs e label1.text sessionquotnamequot.tostring session.removequotnamequotLabel1.TextSession“name”.ToString表示将Session 中 name 的值获取并赋给 Label1.Text,由于 Session 返回类型为 object 类型,所以需要转换成字符串,即.TostringSession.Remove“name”清除掉 name 的 Session 的值,并释放其空间. 3》使用 Application 传值:Application 对象的作用范围是整个全局,也就是说对所有用户都有效,此方法不常使用,因为 Application 在一个应用程序域范围共享,所有用户可以改变及设置其值,故一般应用前需要application.lock使用后 application.unlock. Portected void button1_clickobject sendereventargse applicationquotnamequot textbox1.textresponse.redirectquotweb.aspxquotApplication“name”TextBox1.Text将在 Text Box1.Text 中输入的值赋给 Application 对象,其保存数据的变量名为 nameprotected voidpage_loadobject sendereventargs e labe1.text applicationquotnamequot.tostringLabel1.TextApplication“name”.ToString取出 Application 中的 name 的值,将其转换为字符串并赋给 Label1.Text. Server 对象 ☆MapPath 方法 实现将虚拟路径转化为物理路径。
Server.MapPath文件的虚拟路径 Execute 方法 中断执行当前页面,转到新的页面执行相应的应用程序,执行完毕之后返回当前页面,继续执行Execute 方法后面的语句。
Transfer 方法 执行完新的页面程序之后并不返回当前页面,而是终止执行。
例:上传图片protected void Button1_Clickobject sender EventArgs e if FileUpload1.HasFile FileUpload1.SaveAsServer.MapPathRequest.ApplicationPath quotServerpic/quot FileUpload1.FileName Image1.ImageUrl quot/Server/pic/quot FileUpload1.PostedFile.FileName 15 ADO.NET 的几个重要组件及实现步骤:1》connection :连接数据库2》command 3》datareader 读取数据库 4》dataadpter 用于表示一个 SQL 命令和一个数据库连接。
16 xml与dataset对象,读取与写入xml文档: protected void bind DataSet ds new DataSet SqlConnection conn new SqlConnectionquotserver.database10uidsapasswordwater99quot string sql quotselect from stusquotSqlDataAdapter da new SqlDataAdaptersql conn da.Fillds quottbquot //ds.ReadXmlquotstu.xmlquot ds.WriteXmlquotstu.xmlquot 17 checkboxlist控件的使用: void Check_ClickedObject sender EventArgs e // Retrieve the CheckBoxList control from the Controls collection of the PlaceHolder control. CheckBoxList checklist CheckBoxListPlace.FindControlquotcheckboxlist1quot // Make sure a control was found. ifchecklist null Message.Text quotSelected Items:ltbrgtltbrgtquot // Iterate through the Items collection of the CheckBoxList // control and display the selected items. for int i0 iltchecklist.Items.Count i if checklist.Itemsi.Selected Message.Text checklist.Itemsi.Text quotltbrgtquot else // Display an error message. Message.Text quotUnable to find CheckBoxList control.quot 爱好:FOREACH protected void Button1_Clickobject sender EventArgs e foreach ListItem i in CheckBoxList1.Items if i.Selected true Label1.Text i.Text quot quot 18 如何验证登陆:通过连接后台SQL数据库,通过获取页面用户名和密码,执行相应的select语句,是否存在查询结果验证登陆。
strsqlquotselect from user where userquotampusernameampquot and passwdquotamppasswdampquotquot 数据库增删查改 1.用户信息显示界面代码为: protected void Page_Loadobject sender EventArgs e Stubinding private void Stubinding string connstr quotserver.uidsapwdsqldatabasestudentquot SqlConnection conn new SqlConnectionconnstr conn.Open string str quotselect from View_stuquot SqlDataAdapter da new SqlDataAdapterstr conn DataSet ds new DataSet da.Fillds quotuserquot GridView1.DataSource ds GridView1.DataMember quotuserquot GridView1.DataBind protected void Button1_Click1object sender EventArgs e Response.Redirectquotadduser.aspxquot 2添加用户信息界面代码为: protected void Page_Loadobject sender EventArgs e string connstr quotserver.uidsapwdsqldatabasestudentquot SqlConnection conn new SqlConnectionconnstr conn.Open protected void Button1_Clickobject sender EventArgs e string constr quotserver.uidsapwdsqldatabasestudentquot SqlConnection conn new SqlConnectionconstr conn.Open string stuName TextBox1.Text.Trim string stuSex RadioButtonList1.SelectedItem.Text string proId DropDownList1.SelectedItem.Value string sql quotinsert into stustuNameproIdstuSex values quot stuName quotquot proId quotquot stuSex quotquot SqlCommand cmd new SqlCommandsql conn if cmd.ExecuteNonQuery -1 !quot Label1.Text quot添加成功! protected void Button2_Clickobject sender EventArgs e Response.Redirectquotuser.aspxquot 19 用gridview显示数据库,添加“详细”和“删除”模板列: ltasp:TemplateField HeaderTextquot详细quotgt ltItemTemplategt ltasp:LinkButton IDquotLinkButton1quot runatquotserverquot CommandArgumentltEvalquotStuIDquot gt CommandNamequotEDITquot OnClickquoteditClickquotgt详细lt/
asp:LinkButtongt lt/ItemTemplategt lt/
asp:TemplateFieldgt ltasp:TemplateField HeaderTextquot删除quotgt ltItemTemplategt ltasp:LinkButton IDquotLinkButton2quot runatquotserverquot CommandArgumentltEvalquotStuIDquot gt CommandNamequotdelquot OnClientClickquotreturn confirm是否真的删除?quot OnClickquotdelClickquotgt删除lt/
asp:LinkButtongt lt/ItemTemplategt 20静态网页和动态网页的区别:静态页面指只有HTML标记的网页。
静态页面的属性在设置后不可以更改,运行在客户端;动态页面是网页文件中不仅含有HTML代码,而且还有程序代码,可以根据客户端输入的不同代码显示不同内容,一般运行在服务器端,可以修改其属性。
21 global.asax和web.config:1》 Web.config 包含站点的配置设置; 2》 如标准的自定义错误页面。
Global.aspx包含 由整个站点上任何页面所引起的事件的 处理代码。
如:每当用户第一次访问站点(一次会话的开始) 时运行的代码。
22 如何使用dataset读取xml文件,并显示在gridview中: protected void bind DataSet ds new DataSet SqlConnection conn new SqlConnectionquotserver.database10uidsapasswordwater99quot string sql quotselect from stusquotSqlDataAdapter da new SqlDataAdaptersql conn da.Fillds quottbquot //ds.ReadXmlquotstu.xmlquot ds.WriteXmlquotstu.xmlquot 23 上传控件的使用: protected void btnAdd_Clickobject sender EventArgs e string filename quotquot if this.FileUpload1.HasFile string path Server.MapPathquotuploadquot quot//quot this.FileUpload1.FileName filename this.FileUpload1.FileName this.FileUpload1.SaveAspath SqlConnection conn new SqlConnectionquotserverWIN-QDAULH0P0EVDBdatabase10uidsapasswordwater99quot string sex quotquot ifthis.rbMale.Checked sex quot男Dquot else sex quot女quot string sql quotinsert into stusStuIDStuNameSexDepClassPic valuesquot this.tbStuID.Text quotquotthis.tbStuName.Text quotquot sex quotquot this.ddDep.SelectedItem.Text quotquot this.tbClass.Text quotquot filename quotquot conn.Open SqlCommand cmd new SqlCommandsql conn int k cmd.ExecuteNonQuery ifkgt0 Response.Writequotltscriptgtalert操作成功lt/scriptgtquot else Response.Writequotltscriptgtalert操作失败lt/scriptgtquot 1 .NET的主要组成部分:公共语言运行库CRL,.NET架构类库FCL 2 web窗体由HTML、代码和控件联合组成。
客户端界面和服务器端代码分离是web窗体和传统web页的关键区别。
3 控件属性:
asp.net服务器控件具有大量的属性,这些属性是绝大多数服务器控件都具备的,共分为五大类:布局、数据、外观、行为和杂项。
24、web窗体的运行机制:1、链接文件,.aspx页必须与代码隐藏页关联2、JIT即时编译,代码隐藏页在客户端第一次请求.aspx页时进行编译3、执行,当用户请求.aspx时动态链接库文件处理到达的请求并创建适应的HTML和脚本返回给发出请求的浏览器,以作出响应。
25、WEB窗体关联: Codefile”MyWebForm.aspx.cs”—文件名; Inherrits”myWebForm”—类名 26、用静态页面源代码编写用户登陆页面(表单): ltTABELgt ltTRgt ltTDgt用户名:lt/TDgt ltTDgtltinput name”Text1” type”text”/gtlt/TDgt lt/TRgt ltTRgt ltTDgt密码:lt/TDgt ltTDgtltinput name”password1” type”password”/gtlt/TDgt lt/TRgt ltTRgt ltTD colspan“2” align“center”gt ltinput name”Botton1” type”button” value“登录”/gtlt/TDgt lt/TRgt lt/TABELgt 27、HTML服务器控件 runat“server”在服务器端运行 Web服务器控件 ltasp:Label idquot控件名称quot Textquot显示的文本quot runatquotserverquot /gt ltasp:TextBox idquot控件名称quot TextModequotSingleMultilinePassword“runatquotserverquot /gt ltasp:Button idquot控件名称quot Textquot按钮上的文字quot runatquotserverquot /gt 例:Label1.Text DropDownList1.SelectedItem.Text 例:protected void Button1_Clickobject sender EventArgs e Label1.Text quotxtBox1.Textquotltbrgtquot Label1.Text quot密码:quot TextBox2.Textquotltbrgtquot Label1.Text TextBox3.Text.Replacequotnquotquotltbrgtquot 获取单选按钮选项的代码:Label1.Text RadioButtonList1.SelectedItem.Text 例:复选框 爱好 protected void Button1_Clickobject sender EventArgs e foreach ListItem i in CheckBoxList1.Items if i.Selected true Label1.Text i.Text quot quot 28.ADO.NET主要包括五个组件:Connection Command DataReader DataAdapter DataSet ☆29.连接方法(SQL Server)string str quotserver127.0.0.1uidsapwdsqldatabasesjquotSqlConnection conn new SqlConnectionstr 30.Command对象 ExecuteReader:执行查询并生成一个DataReader对象。
ExecuteNonQuery:执行删除、更新、插入等操作,返回执行后在数据库中所影响的行数。
DataReader对象 从数据库中读取数据,实现对数据源的只读访问。
面向连接的、只向前的、游标式(每次只能取一条记录)的访问。
执行纯粹的读操作,速度较快,开销较少。
31.数据库的访问方法 利用SqlConnection、 SqlCommand和SqlDataReader访问数据库:只能读数据,不能增删改数据;单纯进行查询效率较高。
利用SqlConnection、 SqlCommand、 SqlDataAdapter和DataSet访问数据库:比较灵活,不仅可以对数据库进行查询操作,还可以进行增删改等操作。
创建数据库连接 导入命名空间System.Data和 System.Data.SqlClient。
创建数据库连接 string str quotserver127.0.0.1uidsapwdsqldatabasesjquot 32. GridView控件是用网格形式来显示数据的数据绑定控件。
网格中的一行对应一条数据记录, 列表示记录的字段。
语法格式: GridView控件名称.DataSource 数据对象 GridView控件名称.DataBind 连接数据库: protected void Page_Loadobject sender EventArgs e string str quotserver.uidsapwdsqldatabasemytestquot SqlConnection conn new SqlConnectionstr SqlCommand cmd conn.CreateCommand cmd.CommandText quotselect from userinfoquot select string sqlquotselect from userinfoquot SqlCommand cmd new SqlCommandsql conn conn.Open SqlDataReader dr cmd.ExecuteReader while dr.Read Response.Writedrquotusernamequot quot quotdrquotsexquotquotltbrgtquot conn.Close Insert string sql quotinsert into userinfousernameprovincesex valuesquot user quotquot pro quotquot sex quotquot Update string sql quotupdate userinfo set usernamequot user quotprovincequot pro quotsexquot sex quot where useridquotid 用SqlDataReader对象在GridView控件中填充数据。
protected void Page_Loadobject sender EventArgs e string constr quotserver.uidsapwdsqldatabasemytestquot SqlConnection conn new SqlConnectionconstr SqlCommand cmd conn.CreateCommand cmd.CommandText quotselect from userinfoquot conn.Open SqlDataReader dr cmd.ExecuteReader GridView_UserInfo.DataSource dr GridView_UserInfo.DataBind conn.Close 在DropDownList控件中绑定籍贯信息 private void ProDataBind string con.