一切福田,不離方寸,從心而覓,感無不通。

Category Archives: Asp.net

.net连接MYSQL字符串

<add key="MySqlString" value="server=localhost;port=3306;user id=userid;password=123456;database=dbname;CharSet=utf8;Allow Zero Datetime=true"/>

龙生   22 Apr 2012
View Details

ScriptManager.RegisterStartupScript失效的解决方案

今天在项目中一个页面使用 System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "js", "alert('OK');", true);的时候发现没用,检查发现脚本没用注册到页面, check页面发现了问题,<form method="post"> 没用ruanat,更详细的信息请参看MSDN关于这个方法参数的介绍

龙生   11 Apr 2012
View Details

VS2010安装项目的系统必备中添加.NET 2.0

  VS2010安装项目的系统必备中没有.NET 2.0,不过我们可以从VS2008的程序文件中找到 .NET 2.0 的系统必备安装包。     安装了VS2008 的 C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages 下的 DotNetFX 文件夹,就是 .NET 2.0 的系统必备安装包。把 DotNetFX 文件夹复制到安装了 VS2010 的 C:\Program Files\Microsoft SDKs\Windows\v7.0A\Bootstrapper\Packages下,然后在VS2010中就可以选择 .NET 2.0 的系统必备了。   原文:http://www.cnblogs.com/anjou/archive/2011/05/08/2040675.html

龙生   31 Mar 2012
View Details

asp.net的checkboxlist绑定数据

1.把数据绑定到CheckBoxList中 protected void Page_Load(object sender, EventArgs e){if (!Page.IsPostBack){SqlConnection con = GetDBCon.GetCon();con.Open();SqlDataAdapter sda = new SqlDataAdapter("select * from admin", con);DataSet ds = new DataSet();sda.Fill(ds,"admin");this.CheckBoxList1.DataSource = ds.Tables[0];this.CheckBoxList1.DataTextField = "username";//绑定的字段名this.CheckBoxList1.DataValueField = "userid";//绑定的值this.CheckBoxList1.DataBind(); }} 2.循环读取出来 protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e){this.Lab2.Text = "";for (int i = 0; i < CheckBoxList1.Items.Count; i++){if (this.CheckBoxList1.Items[i].Selected){this.Lab2.Text = this.Lab2.Text+CheckBoxList1.Items[i].Text+".";}}} #region 设置或者得到CheckBoxList选中了的值 /**//// <summary> /// 初始化CheckBoxList中哪些是选中了的 /// </summary> /// <param name="checkList">CheckBoxList</param> /// <param name="selval">选中了的值串例如:"0,1,1,2,1"</param> /// <param name="separator">值串中使用的分割符例如"0,1,1,2,1"中的逗号</param> public static string SetChecked(CheckBoxList checkList,string selval,string separator) …{ selval = separator + selval + separator; //例如:"0,1,1,2,1"->",0,1,1,2,1," for(int i=0; i<checkList.Items.Count; i++) …{ […]

龙生   21 Nov 2011
View Details

关于session超时问题

接管负责了公司的一个项目网站后台管理,客服部要求会话间隔时间能长点,于是在web.config里改了outtime设置,设成了8个小时,一个工作日的时间,可是修改后居然不起作用,依旧是20分钟不操作就得重登录。于是把服务器上的IIS超时设置也改了,会话超时设置成480分钟,但是问题仍然存在(关于outtime的设置,一般web.config的优先级别高于machine.config高于IIS设置。)。仔细查看了代码,是用session保存信息而不是cookie,代码中没有有关超时的设置了。搞了半天问题才解决。 session本来是个不稳定的东西,经常会被丢失,本来用cookie不错,但又不想对程序做改动。查了查资料,找到了下面一段: 由于Asp.net程序是默认配置,所以Web.Config文件中关于Session的设定如下: <sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data   source=127.0.0.1;Trusted_Connection=yes" cookieless="true" timeout="20"/> 我们会发现sessionState标签中有个属性mode,它可以有3种取值:InProc、StateServer、SQLServer(大小写敏感)。默认情况下是InProc,也就是将Session保存在进程内(IIS5是aspnet_wp.exe,而IIS6是W3wp.exe),这个进程不稳定,在某些事件发生时,进程会重起,所以造成了存储在该进程内的Session丢失。 哪些情况下该进程会重起呢?微软的一篇文章告诉了我们: 1、配置文件中processModel标签的memoryLimit属性 2、Global.asax或者Web.config文件被更改 3、Bin文件夹中的Web程序(DLL)被修改 4、杀毒软件扫描了一些.config文件。 更多的信息请参考PRB:   Session   variables   are   lost   intermittently   in   ASP.NET   applications 解决办法: 前面说到的sessionState标签中mode属性可以有三个取值,除了InProc之外,还可以为StateServer、SQLServer。这两种存Session的方法都是进程外的,所以当aspnet_wp.exe重起的时候,不会影响到Session。 现在请将mode设定为StateServer。StateServer是本机的一个服务,可以在系统服务里看到服务名为ASP.NET   State   Service的服务,默认情况是不启动的。当我们设定mode为StateServer之后,请手工将该服务启动。 这样,我们就能利用本机的StateService来存储Session了,除非电脑重启或者StateService崩掉,否则Session是不会丢的(因Session超时被丢弃是正常的)。 除此之外,我们还可以将Session通过其他电脑的StateService来保存。具体的修改是这样的。同样还在sessionState标签中,有个stateConnectionString= "tcpip=127.0.0.1:42424 "属性,其中有个ip地址,默认为本机(127.0.0.1),你可以将其改成你所知的运行了StateService服务的电脑IP,这样就可以实现位于不同电脑上的Asp.net程序互通Session了。 如果你有更高的要求,需要在服务期重启时Session也不丢失,可以考虑将mode设定成SQLServer,同样需要修改sqlConnectionString属性。 在使用StateServer或者SQLServer存储Session时,所有需要保存到Session的对象除了基本数据类型(默认的数据类型,如int、string等)外,都必须序列化。只需将[Serializable]标签放到要序列化的类前就可以了。 如: [Serializable] public   class   MyClass { …… } stateConnectionString和sqlConnectionString是当设置mode的方式是stateServer和sqlServer的时候,必须的选项;但是当mode配置为InProc时,并不是必须的。 转自:http://fus53.blog.163.com/blog/static/735886152008476627520/   以下由【龙生时代】补充:

龙生   07 Nov 2011
View Details

DataSet to excel

/// <summary>/// DataSet导出Excel/// </summary>/// <param name="arrTitle">列标题,若为null,则直接取dataset列标题</param>/// <param name="ds">要导出的DataSet</param>/// <param name="fileName">Excel文件名,不需要传入扩展名</param>protected void CreateExcel(string[] arrTitle, DataSet ds, string fileName){StringBuilder strb = new StringBuilder();strb.Append(" <html xmlns:o=\"urn:schemas-microsoft-com:office:office\"");strb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");strb.Append("xmlns=\"http://www.w3.org/TR/REC-html40\"");strb.Append(" <head> <meta http-equiv=’Content-Type' content=’text/html; charset=gb2312′>");strb.Append(" <style>");strb.Append(".xl26");strb.Append(" {mso-style-parent:style0;");strb.Append(" font-family:\"Times New Roman\", serif;");strb.Append(" mso-font-charset:0;");strb.Append(" mso-number-format:\"@\";}");strb.Append(" </style>");strb.Append(" <xml>");strb.Append(" <x:ExcelWorkbook>");strb.Append("  <x:ExcelWorksheets>");strb.Append("  <x:ExcelWorksheet>");strb.Append("    <x:Name>Sheet1 </x:Name>");strb.Append("    <x:WorksheetOptions>");strb.Append("    <x:DefaultRowHeight>285 </x:DefaultRowHeight>");strb.Append("    <x:Selected/>");strb.Append("    <x:Panes>");strb.Append("      <x:Pane>");strb.Append("      <x:Number>3 </x:Number>");strb.Append("      <x:ActiveCol>1 </x:ActiveCol>");strb.Append("      </x:Pane>");strb.Append("    </x:Panes>");strb.Append("    <x:ProtectContents>False </x:ProtectContents>");strb.Append("    <x:ProtectObjects>False </x:ProtectObjects>");strb.Append("    <x:ProtectScenarios>False </x:ProtectScenarios>");strb.Append("    </x:WorksheetOptions>");strb.Append("  </x:ExcelWorksheet>");strb.Append("  <x:WindowHeight>6750 </x:WindowHeight>");strb.Append("  <x:WindowWidth>10620 </x:WindowWidth>");strb.Append("  <x:WindowTopX>480 </x:WindowTopX>");strb.Append("  <x:WindowTopY>75 </x:WindowTopY>");strb.Append("  <x:ProtectStructure>False </x:ProtectStructure>");strb.Append("  <x:ProtectWindows>False </x:ProtectWindows>");strb.Append(" </x:ExcelWorkbook>");strb.Append(" </xml>");strb.Append("");strb.Append(" </head> <body> <table align=\"center\" style=’border-collapse:collapse;table-layout:fixed'> <tr>"); if (ds.Tables.Count > 0){//写列标题  if […]

龙生   13 Oct 2011
View Details

dataset to excel

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.OleDb;using System.Xml;using System.Xml.Xsl;using System.IO; namespace Amei.Data{    partial class DataSet    {        #region ExcelHelper         #region 读取Excel        public System.Data.DataSet ReadExcel(string path)        {            System.Data.DataSet ds = new System.Data.DataSet();            string extension = System.IO.Path.GetExtension(path.ToLower());            string connString = string.Empty;            if (extension == ".xls")            {                connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";            }            else if (extension == ".xlsx")            {                connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";            }            string query = "SELECT * FROM [Sheet1$]";            try            {                OleDbConnection conn = new OleDbConnection();                OleDbCommand cmd […]

龙生   13 Oct 2011
View Details

repeater控件的ItemCreated事件的代码示例

public   void   repeater1_ItemCreated(object   sender,   RepeaterItemEventArgs   e)     {     DataRowView   row   =   (DataRowView)e.Item.DataItem;     string   url   =   "id="+row["zyid"].ToString();     string   text   =   row["职业名称"].ToString();     string   cat_num   =   row["数量"].ToString();         HyperLink   link   =   new   HyperLink();     link.NavigateUrl   =   "upPage.aspx?"   +   url;     link.Text   =   text;         e.Item.Controls.Add(link);     […]

龙生   09 Oct 2011
View Details

文件管理系统

一、准备知识点: 1.对文件操作,先引用两个命名空间:using System.IO;(操作文件)、using Sysetem.Text;(操作文本) 2.创建文本文件:(1)创建文件名和文件内容(相当于新建文本文档)(2)创建StreamWriter对象,创建一个某某格式的文件(3)将内容写入数据流WriteLine (4)关闭StreamWrite对象.Close() 方法一:直接创建一个StreamWriter对象 string filename = TextBox1.Text string filecontent= TextBox2.Text StreamWriter sw =File.CreatText(Server.MapPath("~/txt/"+ filename +".txt")); sw.WriteLine(filecontent); sw.Close(); Response.Write("<script>alert('已经成功新建了一个’+ filename +'.txt,并添加了数据')</script>") 方法二:在创建StreamWriter对象之前先创建一个FileStream(文件流)对象,并在最后关闭它。 string filename = TextBox1.Text string filecontent= TextBox2.Text FileStream fs = new FileStream(Server.MapPath("~/txt/"+ filename + ".txt")),FileMode.Create,FileAccess.Write);  //文件流fs的路径、文件打开方式:创建和写操作 StreamWriter sw = new StreamWriter(fs,Encoding.Default);   //文件流fs和浏览器默认编码类型 sw.WriteLine(filecontent); sw.Close(); fs.Close(); Response.Write("<script>alert('成功新建了一个’+ filename +'.txt,并添加了数据')</script>")   3.读取文本文本: 方法一:直接使用File.ReadAllText(文件路径,编码方式) TextBox1.Text=File.ReadAllText(Server.MapPath("~/txt/**.txt"),Encoding.Default); 方法二:使用StreamReader对象以File.OpenText(文件路径)读取文件数据以及使用StreamBuilder对象的Append属性来添加读取文件的数据(UTF-8规范读取的数据) StreamReader sr = File.OpenText(Server.MapPath("~/txt/**.txt")); StreamBuilder sb = new StreamBuilder(); while(!sr.EndOfStream)               //如果数据不到最后一行,继续添加(循环语句) { sb.Append(sr.ReadLine() +"<br>");} sr.Close(); 方法三:使用StreamBuilder对象的Append属性添加从StreamReader对象那里读取的数据流,与前一种方法不同的是,这次用到了FileStream的File.Open方法取代了File.OpenText(文件路径)的方法(GB2312规范读取数据) StreamBuilder sb = new StreamBuilder(); FileStream fs = File.Open(Server.MapPath("~/txt/**.txt"),FileMode.Open,FileAccess.Read); StreamReader sr = new StreamReader(fs,Encoding.Default); […]

龙生   05 Jul 2011
View Details