记录下学习过程中的Asp.Net知识点,非原创纯属笔记
1.小数据量简单分页类PagedDataSource

PagedDataSource pds = new PagedDataSource(); pds.AllowPaging = true;//启用分页 pds.DataSource = lst;//设置数据源 pds.PageSize = 4;//每页显示个数 pds.CurrentPageIndex = 2;//设置当前页 csRep.DataSource = pds;//绑定数据 csRep.DataBind();
2.文件上传类HttpPostedFile
1)获取:FileUpload.PostedFile(PostedFile控件) Request.Files["上传标签Name"]
2)常用属性:ContentLength获取上载文件的大小(以字节为单位) FileName客户端的文件的名称,包含目录路径。InputStream指向文件的 System.IO.Stream。
3)方法:SaveAs(string filename)保存文件
4)上传文件格式过滤:通过判断BinaryReader读取上传文件流的前两个字节码:doc:208207 txt:5155 swf:6787 gif:7173 jpg:255216 xls:208207 xlsx、docx:8075 zip\rar:8297
3.xml操作
1)XmlDocument类

StringBuilder sb = new StringBuilder(); XmlDocument xd = new XmlDocument(); xd.Load(Server.MapPath("/Config/Test.xml"));//加载xml文件 XmlNodeList xnl = xd.GetElementsByTagName("Ts"); //获取xmlNode集合 foreach (XmlNode xn in xnl) { sb.Append(string.Format("Name:{0} ", xn.Attributes["Name"].Value));//遍历操作,获取节点属性值 } //获取某个xmlNode XmlNode xnn = xd.SelectSingleNode("/Cs/Ts");//xmlPath sb.Append(xnn.Attributes["Name"].Value); //修改节点 XmlElement xel = xnn as XmlElement; xel.SetAttribute("Name", "New"); xd.Save(Server.MapPath("/Config/Test.xml"));
2)XPathDocument类

XPathDocument xpd = new XPathDocument(Server.MapPath("/Config/Test.xml")); XPathNavigator xpn = xpd.CreateNavigator(); XPathNodeIterator xpni = xpn.Select("//Ts"); string str = string.Empty; while (xpni.MoveNext()) { str += xpni.Current.GetAttribute("Name","")+ "=="; }
3)路径表达式:nodename 选取此节点的所有子节点。/ 从根节点选取。// 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。 . 选取当前节点。.. 选取当前节点的父节点。@ 选取属性
谓语:被嵌套在方括号中[last()|1]
4.iis-http头-MiMe类型
.asf video/x-ms-asf .avi video/x-msvideo .flv flv-application/octet-stream .rm application/vnd.rn-realmedia .rmvb application .wmv video/x-ms-wmv .license application/octet-stream(任意二进制文件)
iis6.0下xlsx无法下载要增加:.docx,application/vnd.openxmlformats-officedocument.wordprocessingml.document; .xlsx,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
5.后台程序获取前台控件值:Request["控件name属性值"];控件必须为成功控件(不能为禁用、或visible="false")
6.常用服务器控件渲染关系表(手动渲染:Control.RenderControl(HtmlTextWriter):将服务器控件内容输出到指定流中)
<asp:Label ID="Label" runat="server" Text="Label"></asp:Label>=><span></span>标签 <asp:Label ID="Label" runat="server" Text="Label" AssociatedControlID="TextBox"></asp:Label>=><label for=""></label>标签 <asp:Button ID="Button" runat="server" Text="Button" />=><input type="submit"/>标签 <asp:TextBox ID="TextBox" runat="server"></asp:TextBox>=><input type="text" />标签 <asp:TextBox ID="TextBox" runat="server" TextMode="MultiLine"></asp:TextBox>=><textarea></textarea>标签 <asp:TextBox ID="TextBox" runat="server" TextMode="Password"></asp:TextBox>=><input type="password"/>标签 <asp:CheckBox ID="CheckBox" runat="server" Text="checkbox" />=><input type="checkbox" /><label for="CheckBox"></label>标签 <asp:DropDownList ID="DropDownList" runat="server"></asp:DropDownList>=><select ><option ></option></select>标签 <asp:FileUpload ID="FileUpload" runat="server" />=><input type="file" />标签 <asp:Literal ID="Literal" runat="server" Text="<h1>Literal</h1>" Mode="Encode"></asp:Literal>//纯文本,不会渲染为任何HTML标签。如果将Mode设置为"Encode"会自动的对"<"、">" 等符号进行转义 <asp:HiddenField ID="HiddenField" runat="server" />=><input type="hidden" />标签 <asp:Image ID="Image" runat="server" ImageUrl="" />=><img src="" />标签 <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="" />/=><input type="image" src="" />标签 <asp:RadioButton ID="RadioButton" runat="server" />=><input type="radio" />标签
7.前台js调用后台方法:__doPostBack(evnetTarget(服务器控件ID,eventArgument(相应的参数)):常用模式为:隐藏服务器端控件(常用以向后台传递参数值)+隐藏LinkButton
<script type="text/javascript"> function postCs() { setTimeout(function(){ __doPostBack('LinkButton1','');},5); } </script> <input type="hidden" id="CsId" runat="server" value="Cs" /> <asp:LinkButton ID="LinkButton1" runat="server" style="display:none;" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <p><a href="javascript:void(0)" onclick="postCs()">调用后台方法</a></p> protected void LinkButton1_Click(object sender, EventArgs e) { TextBox1.Text = CsId.Value; }
8.include<!--#include file="站内文件"-->将站内程序文件嵌入到引用位置
9.常见css调整:
1)密码框与文本框size相同,ie显示不一致:增加style="px;height:px;" 文本域:height
2)<label for="对应id'>说明</label>对齐表单控件
10.系统取默认编码顺序为:http请求头-globalization配置节点-》默认UTF-8;在Url直接输入中文时,不同浏览器处理方式可能不同如:ie不进行编码直接提交,firefox对url进行gb2312编码后提交。Request.QueryString时内部自动解码,若出现乱码,用HttpUtility.ParseQueryString(Request.Url.Query, 编码类型)["参数名"]获取;
11.HtmlAgilityPack简单应用:仿xml路径查询进行数据采集:
HtmlDocument hd = new HtmlDocument(); hd.LoadHtml("content"); HtmlNodeCollection hnc = hd.DocumentNode.SelectNodes("//a[@class='newsquery']"); str = string.Empty; foreach (HtmlNode node in hnc) { str += node.InnerText + node.GetAttributeValue("href", "href").Replace("..", "") + "</br>"; }
12.加载类库中的js资源文件:
1)js文件属性中生成选择嵌入的资源
2)程序集信息:[assembly: System.Web.UI.WebResource("指定js名", "text/js")]
3)程序加载:
HtmlGenericControl autoCompleteScript = new HtmlGenericControl("script"); autoCompleteScript.Attributes.Add("type", "text/javascript"); autoCompleteScript.Attributes.Add("src", this.ClientScript.GetWebResourceUrl(type, "程序集js指定名")); this.Header.Controls.Add(autoCompleteScript);
13.简单作业调度任务的实现框架:Quartz.NET;简单使用:引入Quartz.dll、Common.Logging.dll
ISchedulerFactory sf = new StdSchedulerFactory(); IScheduler sched = sf.GetScheduler(); sched.Start(); SimpleJob myjob = new SimpleJob(); JobDetail jobDetail = new JobDetail("job1", "my", myjob.GetType()); CronTrigger simpleTrigger = new CronTrigger("simpleTrigger", "my"); simpleTrigger.CronExpressionString = "0/20 * * * * ?"; simpleTrigger.StartTimeUtc = DateTime.UtcNow; sched.ScheduleJob(jobDetail, simpleTrigger);
SimpleJob:继承自IJob,将要执行的任务代码写入实现方法中;
CronExpressionString:采用Cron表达式,是一个由7个子表达式组成的字条串,每个子表达式都描述了一个单独的日程细节,这些子表达式用空格隔开,分别表示:秒(0-59)、分钟、小时(0-23)、月中的天、月、周中的天(1-7)、年(可选),单个子表达式可包含范围或列表,其中:*:所有,?:用于月中的天或周中的天中指未指定;a/b:指隔b多久从a开始执行,#用于周中天:1#3:表示本月第三周的周日
14