1.Asp.net 中不再支持 include file= 的方式调用函数和定义的变量;
但仍然可以使用 include file的方式来包含文件名位.aspx,内容为
html的文件。
解决办法:使用<%@ import Namespace="Namespace" > 的方式来调用
?? 已经编译了的dll
?? 编译的dll应该放在bin目录现;
?? 定义文件Global.asax,并在内部 使用<%@ import Namespace="Namespace" >
?? 来引用类
?? 在aspx文件中 必须通过方法的方式来使用;
?? aspx还是支持原来的客户端脚本,如JavaScript;
?? aspx可以和asp共存于一个站点
?2.Ado.net
?? 每个数据连接对应其相应的组件
???如 OdbcConnection->OdbcDataAdapter
????? OleConnection->OleDataAdapter
???但是可以使用一个表现层
???如:都可以用 DataAdapter.fill 来添冲 DataSet
??
???DataSet 可以返回DataTable 也可以返回DataRow
???DataTable 类似于一个表
???row就是行
???Rows[][]就是具体某个单元格的值
3. cookie
?? cookie 更象一个集合
???HttpCookie("cookiename") cookie名称
???cookie.Values.Add("WebSite","www.ChinaPT.com"); 增加一个子项
???对应的值为www.ChinaPT.com
???cookie.Values("WebSite") -->Cookie的值
4.aspx
? 一个aspx文件 对应一个.cs文件 ,代码可以封装在.cs文件中
??一个cs(aspx)文件 可以引用多个dll
??一个aspx文件也可以引用多构aspx文件( )
?? <%@ Import
????? Namespace="System.Data"
??%>
<%@ Register TagPrefix="jav"
???????????? TagName="Search" src="t4.ascx"
???????????????????????? %>
dll aspx dll
|___________________| |
aspx<------------------------------------------------------->cs
( <%@ Page language="c#" Debug="true"
???????????src="t2.cs" AutoEventWireup="false"
???????????Inherits="TwebA.WebForm1" %>
)
5.
命令行
c:/csharp>csc /t:library SqlData.cs
c:/csharp>csc /t:library /r:SqlData.dll DateTime.cs
6.Request
String strV=Request.QueryString.Get("QueryStringName")
7.在自定义类中使用page类的Request,Response
使用 HttpContext.Current.Response.Write
HttpContext.Current.Request
例如:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
编辑,/WINNT/Microsoft.NET/Framework/v1.1.4322/CONFIG/machine.config文件,将processModel节中userName的值设为SYSTEM, 最后重起计算机。
namespace Tresponse
{
///
public void PrintInfo()
{
HttpContext.Current.Response.Write("eewqewqewq尼弗毒素发");
//System.Web.HttpContext.Current.Response("ddddddd");
}
}
}
8.Asp和Asp.net 可以共享Cookie;
Asp: (写入cookie)
dim cookieValue
Response.Cookies("AspCookie")="UsernameLoged"
Response.Cookies("AspCookie").Expires=Date+1
cookieValue=Request.cookies("AspCookie")
Response.Write("cookie:"+cookieValue)
Asp.net(读入cookie)
String strV=Request.Cookies["AspCookie"].Value;
Response.Write("AspCookies"+strV);
如果asp写入的cookie中包含有中文
[记得引入 System.Text ]
String strV=Request.Cookies["AspCookie"].Value;
System.Text.Encoding theEncoding = System.Text.Encoding.GetEncoding("gb2312");
String decodeValue = HttpUtility.UrlDecode(strV,theEncoding );
Response.Write("AspCookies:"+decodeValue);
=================================================
Asp.net(写cookie)
String StrV="www.chinapt.com";
//如果有中文则需要以下语句
//System.Text.Encoding theEncoding = System.Text.Encoding.GetEncoding("gb2312");
//String cookieValue = HttpUtility.UrlEncode(StrV,theEncoding );
HttpCookie TCookie=new HttpCookie("AspNetCookie");
//TCookie.Values.Add("Tcookie",cookieValue);
TCookie.Values.Add("Tcookie",StrV);
Response.AppendCookie(TCookie);
Asp:(读取cookie)
Response.Write(Request.Cookies("AspNetCookie")("Tcookie"))
9.OleDbConnection
string myConnectionStr = "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=pratice;User Id=登陆号;Password=密码;";
10.SqlConnection
"Data Source=Bender/NETSDK;Initial " & _
"Catalog=Contacts;User Id=sa"
11.GBT2Big5 的思路(http)
.繁简对找表
.把 http输入字节流转换为str
[ Encoding e=Encoding.GetEncoding(936);
string str=e.GetString(buffer,offset,count);
]
.替换对应的字符
.把字符串转换为字节流
[e=Encoding.GetEncoding(System.Web.HttpContext.Current.Response.Charset);
_sink.Write(e.GetBytes(str), 0, e.GetByteCount(str));
]
12.Page_Load
.方法:isPostBack==true 页面重载
13
mode="SQLServer"
stateConnectionString="tcpip=192.168.127.100:42424"
sqlConnectionString="data source=192.168.127.100;
user id=admSession;password=adm85597214"
14 Asp.net Xml
System.Xml.XmlDocument t=new System.Xml.XmlDocument();
String xmlFilePath="F://Web_Program//ChinaPTNet2003//pubXml//ChinaPTXml.xml";
//String xmlFilePath="F://Web_Program//ChinaPTNet2003//ChinaPTXml.xml";
.Load(xmlFilePath);
MessageBox.Show(t.SelectSingleNode("//Prod46Class").ChildNodes.Count.ToString());
MessageBox.Show(t.SelectSingleNode("//Prod46Class").ChildNodes.Item(0).InnerText);
15. //使用asp.net服务端控件和客户端脚本
//
16.Asp服务控件发送脚本
.客户端脚本块;
.客户端html属性
System.Web.UI.Page 类中的两个方法
。RegisterStarUpScript(key,script);
. RegisterClientScriptBlock(key,script);
key:客户断js脚本的唯一标识符号;
script:脚本
不同点:
。RegisterClientScriptBlock(key,script)发送的脚本
在
.IsStartupScriptRegistered(key)
判断是否存在key对应的脚本
.IsClientScriptBlockRegistered(key)
判断是否存在key对应的脚本
System.Web.UI.Control 中应用
.this.Page.RegisterClientScriptBlock(key, script);
control的方法
.OnPreRender() 负责处理写入脚本的任务
Page.IsPostBack
System.Web.UI.WebControls.WebControl
。发送客户端html
方法
.AddAttribute()
.AddStyleAttribute()
AddAttribute() 方法用于将 title、class、style 和 onclick 等 HTML
属性添加到 HTML 元素。
AddStyleAttribute() 用于将样式设置添加到 HTML 元素,
如 background-color、color 和 font-size 等。
17.数据库查询,更新,Sql,SqlCommand,Parameter,
<%//@ Page language="c#" src="file.cs" AutoEventWireup="false" Inherits="MyNameSpace.MyClass" %>
<%@Import Namespace="System.Data"%>
<%@import Namespace="System.Data.SqlClient"%>
<%@import Namespace="System.Data.OleDb" %>
18.Other
SqlDataAdapter dataAdapter = new SqlDataAdapter ("SELECT userId,username FROM users ORDER BY username", sqlConn);
// create the DataSet
DataSet dataSet = new DataSet();
// fill the DataSet using our DataAdapter
dataAdapter.Fill (dataSet);
foreach(DataRow dataRow in dataSet.Tables["users"].Rows) {
Debug.WriteLine(dataRow["username"] + "(" + dataRow["userid"] + ")");
}
DataRow[] matchingRows = dataSet.Tables["users"].Select("username like 'Bob%'","dateJoined DESC");
[C#]
DataView dataView = dataSet.Tables["users"].DefaultView;
dataView.RowFilter = "username like 'Bob%'";
dataView.Sort = "dateJoined DESC";
myDataGrid.DataSource = dataView;
//Call to DataBind needed in ASP.NET
//myDataGrid.DataBind();
[C#]
// create the data adapter
SqlDataAdapter dataAdapter = new SqlDataAdapter ("SELECT userId,username FROM users ORDER BY username", sqlConn);
// create an SqlCommandBuilder - this will automatically generate the
// commands, and set the appropriate properties in the dataAdapter
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// create the DataSet
DataSet dataSet = new DataSet();
// fill the DataSet using our DataAdapter into a table called users
dataAdapter.Fill (dataSet,"users");
// set the DataGrid source to the one table in our dataset
myDataGrid.DataSource = dataSet.Tables[0];
// create the data adapter - we'll specify the connection through our SqlCommand object
SqlDataAdapter dataAdapter = new SqlDataAdapter();
SqlCommand sqlSelectCommand = new SqlCommand("SELECT userid,username FROM users",sqlConn);
// assign to the SelectCommand property
dataAdapter.SelectCommand = sqlSelectCommand;
SqlCommand sqlUpdateCommand = new SqlCommand("UPDATE users SET username=@username WHERE userid=@id",sqlConn);
// add parameter to this command for @username, and bind it to the column "username"
sqlUpdateCommand.Parameters.Add("@username",SqlDbType.VarChar, 30, "username");
// create another parameter
SqlParameter sqlParam = new SqlParameter("@id",SqlDbType.Integer, 8, "userid");
sqlParam.SourceVersion = DataRowVersion.Original;
// add parameter
sqlUpdateCommand.Parameters.Add(sqlParam);
// assign to UpdateCommand
dataAdapter.UpdateCommand = sqlUpdateCommand;
[C#]
// get a new row
DataRow newRow = myDataSet.Tables["users"].NewRow();
// set the appropriate fields here... for example
newRow["username"] = "myNewUsername";
// add to the DataTable:
myDataSet.Tables["users"].Rows.Add(newRow);
To update a row in the DataTable, you can simply modify a columns value:
myDataSet.Tables["users"].Rows[rowIndex]["realName"] = "James Crowley";
And finally, to delete one, use the Remove or RemoveAt method:
myDataSet.Tables["users"].Rows.RemoveAt(rowIndex)
or
myDataSet.Tables["users"].Rows.Remove(dataRowObject)
19.Cache
.引入
<%@ OutputCache Duration="60" VarByParam="none"%>
//本页Cache保持60秒,不响应get,pos等动作
//如果VarByParam 不为 none 则Cache随参数变化而变化
//文件名.aspx?VarByParamName=Value
.适用范围
//每天固定从数据库提取数据,不需要时时变化
.不适用范围
//动态查询
20.字符集
//16进制得到中文
//a必须是 a=0x ; b=0x 开头的16进制
//与byte a = 0xB0 ; byte b = 0xEA 类似
private string GetCnStr(byte a,byte b)
{
Encoding enc = Encoding.GetEncoding("gb2312") ;
byte[] c= new byte[]{a,b};
string strV=enc.GetString(c);
return strV;
}
//判断是否位中文
private bool isENChar(string strV)
{
int nValue;
for(int i=0;i {
nValue=(int)strV[i];
if (!(nValue>=1 &&nValue<=128))
return false;
}
return true;
}
//实型转换位字节数组
byte[] my=BitConverter.GetBytes(nValue);
//字符转为16进制
string a="0xB0";string b="0xEA";
int c1 = Convert.ToByte(a,16);
int c2=Convert.ToByte(b,16);
//繁体,简体转换,gb,big5;BiG5Order 和GBOrder的定义在code.txt中
// BIG5Order[6] := $A3BB;BIG5Order[6,0]=0xA3;BIG5Order[6,0]=0xBB
private ushort[,] BIG5Order=new ushort[14758,2] ;
private ushort[,] GBOrder =new ushort[8178,2];
private int GBOffset(string value)
{
byte[] a=System.Text.Encoding.Default.GetBytes(value);
if (!isENChar(value))
return ((a[0]-161)*94+(a[1]-161));
else
return -1;
}
private int BIG5Offset(string value)
{
byte[] a=System.Text.Encoding.Default.GetBytes(value);
if (!isENChar(value))
{
if (a[1]>=64 && a[1]<=126)
return ((a[0]-161)*157+(a[1]-64));
if (a[1]>=161 && a[1]<=254)
return ((a[0]-161)*157+63+(a[1]-161));
}
return -1;
}
private bool isBIG5(string value)
{
byte[] a=System.Text.Encoding.Default.GetBytes(value);
if (!isENChar(value))
//if (value.Length>=2)
{
if (a[0]<162)
return false;
else
if((a[1]>64 && a[1]<=126) ||(a[1]>=161 && a[1]<=254) )
return true;
else
return false;
}
else
return false;
}
private bool isGB(string value)
{
byte[] a=System.Text.Encoding.Default.GetBytes(value);
if (!isENChar(value))
//if (value.Length>=2)
{ if(a[0]<=161 && a[0]>=247)
return false;
else
{
if (a[1]<= 161 && a[1]>=254)
return false;
else
return true;
}
}
else
return false;}
private string GBtoBIG5(string value)
{int leng,idx; //string []tempStr=new string[2];
int Offset; string output,tempStr;
output=""; leng=value.Length;idx=0;
while (idx {//byte[] a=Encoding.Default.GetBytes(value[idx].ToString());
//MessageBox.Show(value[idx].ToString());
tempStr=value[idx].ToString();
//tempStr=value[idx]+value[idx+1];
if (isGB(tempStr))
{
Offset=GBOffset(tempStr);
if (Offset>=0 && Offset<=8177)
{
output=output+WordToString((byte)GBOrder[Offset,0],(byte)GBOrder[Offset,1]);
idx++;
}
else
output=output+value[idx].ToString() ;
}
else
{
output=output+value[idx].ToString();
}
idx++;
}
return output;
}
private string BIG5toGB(string value)
{
int leng,idx;string tempStr="";int Offset;
string output="";leng=value.Length;idx=0;
while (idx {
//tempStr=value[idx]+value[idx+1];
tempStr=value[idx].ToString();
if (isBIG5(tempStr))
{
Offset=BIG5Offset(tempStr);
if (Offset>=0 && Offset<=14757)
{
output=output+WordToString((byte)GBOrder[Offset,0],(byte)GBOrder[Offset,1]);
idx++;}
else
output=output+value[idx].ToString();
}
else
{
output=output+value[idx].ToString();
}
idx++;}
return output;
}
private string GetCnStr(byte a,byte b)
{
Encoding enc = Encoding.GetEncoding("gb2312") ;
byte[] c= new byte[]{a,b};
string strV=enc.GetString(c);
return strV;
}
private bool isENChar(string strV)
{int nValue;
for(int i=0;i {nValue=(int)strV[i];
if (!(nValue>=1 &&nValue<=128)) return false;}
return true;}
private string WordToString(byte a,byte b)
{return GetCnStr(a,b);}
21.Asp.net 中的数据插入(以sqlClient为例子)
1。用SqlCommand
//建立SqlCommand
//strSql="Insert un(message)values(@Message)";
SqlCommand sqlCmd=new SqlCommand(strSql,sqlconnection);
sqlCmd.Parameters.Add("@Message",SqlDbType.VarChar,8000);
sqlCmd.Parameters["@Message"].Value="dddd";
sqlCmd.ExecuteNonQuery();
2.用SqlAdapter
a.//先建一个DataTable
DataTable myTable=new DataTable();
DataRow myRow=myTable.NewRow();
myTable.Columns.Add(new DataColumn("Message"));
myRow[0]=strV;
myTable.Rows.Add(myRow);
//建立Adapter对象
SqlDataAdapter MyAdapter = new SqlDataAdapter();
MyAdapter.InsertCommand = new SqlCommand();
MyAdapter.InsertCommand.CommandText ="Insert Into Un(Message) values(@Message)";
MyAdapter.InsertCommand.Connection =sqlCon;
SqlParameter p1 = new SqlParameter("@Message", SqlDbType.VarChar, 8000);
p1.SourceVersion =DataRowVersion.Current;
p1.SourceColumn = "Message"; // or p1.SourceColumn = Dt.Columns[0].ColumnName;
MyAdapter.InsertCommand.Parameters.Add(p1);
MyAdapter.Update(Dt);
b.
String strSql="Select top 1 * from Un ";
SqlDataAdapter sqlDA=new SqlDataAdapter(strSql,sqlCon);
DataSet DS=new DataSet();
sqlDA.Fill(DS,"Un");
DataTable myTable=DS.Tables[0];
DataRow myRow=myTable.NewRow();
myRow["Message"]=AryNews[0,2];
myTable.Rows.Add(myRow);
sqlDA.InsertCommand=new SqlCommand("Insert Into Un(Message) values(@Message)",sqlCon);
SqlParameter p1=new SqlParameter("@Message", SqlDbType.VarChar, 8000);
p1.SourceVersion=DataRowVersion.Current;
p1.SourceColumn ="Message";
sqlDA.InsertCommand.Parameters.Add(p1);
sqlDA.Update(DS,"Un") ;
产生随机数字的例子
private void GetRndNum(ref int[] AryNum)
{
Random a=new Random();
int k=0,m=2;
string strText="";
while(m>1)
{
int num=a.Next(1,50);
if (m==0)
{
strText=strText+num.ToString()+ ",";
AryNum[0]=num;
}
else
{
if(strText.IndexOf(num.ToString()+",")<0)
{
strText=strText+num.ToString()+",";
AryNum[k]=num;
k++;
}
if (k>4)
m=-1;
}
}
}
Asp.net DataGrid 分页 帮定后
加
private void DBGrid_PageIndexChanged(object source, System.Web.UI.WebControls.
DataGridPageChangedEventArgs e)
{
DBGrid.CurrentPageIndex=e.NewPageIndex+1;
}