ASP.NET 使用笔记
XML:
读取相对目录下的XML文件:
1、DLL(需要将XML属性加入到工程内部)
System.Reflection.Assembly objAssembly = System.Reflection.Assembly.GetExecutingAssembly();
Stream objStream = objAssembly.GetManifestResourceStream("ProjectName." + strFileName+ ".xml");
XmlDocument m_xmlDocument = new XmlDocument();
m_xmlDocument.Load(objStream);
2、WEB
string strRefFullFileName = System.Web.HttpContext.Current.Server.MapPath(s_strDefaultFileName)
FileStream objFS = File.Open(m_strRefFullFileName , FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
xmlDocument.Load(objFS);
读取XML内容:
<?xml version="1.0" encoding="utf-8" ?>
<Entity name="ObjEmplyeeInfo">
<SqlString>
<Sql Name="SearchByEmployeeNo">
<context>UPDATE Affiliation SET Affiliation.UpdateUserID = @UserID,Affiliation.UpdateDateTime = GETDATE()
FROM Affiliation INNER JOIN Person ON Person.PersonID = Affiliation.PersonID
WHERE Person.EmployeeNo = @EmployeeNo and Affiliation.UpdateDateTime = @UpdateDateTime
<context>
<Param Name="@WithdrawDate" attribute="WithdrawDate"/>
<Param Name="@EmployeeNo" attribute="EmployeeNo" />
<Param Name="@SequenceNo" attribute="SequenceNo" />
<Param Name="@UserID" attribute="UserID"/>
<Param Name="@UpdateDateTime" attribute="UpdateDateTime"/>
</Sql>
</SqlString>
</Entity>
代码:
XmlNode xmlNode = m_xmlDocument.SelectSingleNode("Entity/SqlString/Sql[@Name='SearchByEmployeeNo']");
if (xmlNode != null)
{
// 内容(<context>)的读取
String strSql = xmlNode.InnerText.ToString();
// 其他多个平级节点内容(<Param> 该节点名称可自定义)的读取
XmlNodeList xmlNodelist = m_xmlDocument.SelectNodes("Entity/SqlString/Sql[@Name='SearchByEmployeeNo']/Param");
if (xmlNodelist.Count > 0)
{
string[] strParams = new string[xmlNodelist.Count];
string[] strValue = new string[xmlNodelist.Count];
this.m_DataProtoType = new DataProtoType[xmlNodelist.Count];
for (int i=0; i < xmlNodelist.Count; i++)
{
strParams[i] = xmlNodelist.Item(i).Attributes["Name"].Value.ToString();
strValue[i] = xmlNodelist.Item(i).Attributes["attribute"].Value.ToString();
}
}
}
读取web.config上的自定义配置项:
<configuration>
<configSections>……</configSections>
<appSettings>
<add key="isCheckXMLFileUpdateEverWhen" value="true"></add>
</appSettings>
</configuration>
string str = (string)System.Configuration.ConfigurationSettings.AppSettings["isCheckXMLFileUpdateEverWhen"];
判断文件是否更新
DateTime dtDateTimeLast = File.GetLastWriteTime(m_strFileName);
if (m_dtOldTime.Ticks.CompareTo(dtDateTimeLast .Ticks)!=0){
……
}
ASP.NET中调用Script激发按钮click事件
C#
namespace HK_WEB
{
……
public class ALG_SerchClient : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtUserID;
protected System.Web.UI.WebControls.Button btnQuery;
}
private void Page_Load(object sender, System.EventArgs e)
{
txtUserID.Attributes.Add("onblur","doQuery()");
}
……
}
ASP.NET
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM<title>
<script language="javascript">
document.form1.btnQuery.click();
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="form1" method="post" runat="server">
<table align="center" width="60%" height="100%" border="0">
<tr height="5">
<td width="20%" style="HEIGHT: 26px">
<asp:TextBox Runat="server" ID="txtUserID" MaxLength style="ime-mode:disabled"></asp:TextBox></td>
<td width="5%" style="HEIGHT: 26px"><asp:Button Runat="server" ID="btnQuery"></asp:Button></td>
</tr>
</table>
</form>
屏蔽IE回退按钮
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title></title>
<script language="javascript">
history.go(1);
</script>
设置IE缓存过期:
Response.Expires = -1;
反射读取page页面的字段信息
public void test(Page objPageCheck ){
Type objPageType = objPageCheck.GetType().BaseType;
FieldInfo objFieldInfo = objPageType.GetField("Field_Name",BindingFlags.Static | BindingFlags.Public);
if (null != objFieldInfo)
{
objPageCheck.Session["Sess_Global_PageCode"] = objFieldInfo.GetValue(objPageCheck);
}
}
判断程序多启动:
class AA0100
{
private const string strClassName = "AA0100";
static int Main(string[] args)
{
System.Diagnostics.Process[] objProcess = System.Diagnostics.Process.GetProcessesByName(strClassName);
if (objProcess.Length > 1)
{
return 1;
}
……
Return 2;
}
}
Nunit测试:
[Test]
public void MainTestCase1()
{
Process objProcess = Process.Start(@"H:/Documents/...../AA0100/bin/Debug/AA0100.exe");
objProcess.WaitForExit();
Assert.AreEqual(2, objProcess.ExitCode);
}
[Test]
public void MainTestCase2()
{
Process objProcess1 = Process.Start(@"H:/Documents/.....//AA0100/bin/Debug/AA0100.exe");
Thread.Sleep(100);
Process objProcess2 = Process.Start(@"H:/Documents/.....//AA0100/bin/Debug/AA0100.exe");
objProcess1.WaitForExit();
objProcess2.WaitForExit();
Assert.AreEqual(1, objProcess2.ExitCode);
}
自定义工具
更新页面HTML内容:
1、重写父类方法:
protected override void Render(HtmlTextWriter output)
{
output.Write(@"<table border='0' cellpadding='0' cellspacing='0' width='100%' height='1'>
<tr>
td height='1' bgcolor='#24377E' width='50%'>
<p><blink><strong>派遣勤務者登録システム</strong></blink>
</td>
</tr>
</table >”);
}
2、script方法:
页面端代码:
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="MessageBoard.ascx.cs" Inherits="MessageBoard" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<FONT face="MS UI Gothic"></FONT>
<div align="center">
<table border="0" id="AutoNumber1" width="75%" class="Description">
<%
foreach(string strMsg in m_objMessageArrayList)
{
%>
<tr>
<td class="Caution">
<p align="center"><FONT face="MS UI Gothic"></FONT><%=strMsg%></p>
</td>
</tr>
<%
}
%>
</table>
</div>
配置SQLServer会话状态
会话状态设置——SQLServer 模式:
1、 在安装有SQLServer的目标服务器上执行sql文件:installsqlstate.sql 或 InstallPersistSqlState.sql。两个脚本均创建一个名为 ASPState 的数据库,它包含若干存储过程。
两个脚本间的差异在于放置 ASPStateTempApplications 和 ASPStateTempSessions 表的位置。InstallSqlState.sql 脚本将这些表添加到 TempDB 数据库,该数据库在计算机重新启动时将丢失数据。相反,InstallPersistSqlState.sql 脚本将这些表添加到 ASPState 数据库,该数据库允许在计算机重新启动时保留会话数据。
默认情况下,两个脚本文件均安装在下面的位置
systemroot/Microsoft.NET/Framework/versionNumber
如:C:/WINDOWS/Microsoft.NET/Framework/v1.1.4322目录下
2、 启动服务器上的SQLSERVERAGENT服务。
3、 修改IIS的配置文件web.config
<sessionState
mode="SQLServer"
sqlConnectionString="data source=10.69.0.13;user id=sa;password=sa"
cookieless="true"
timeout="20"
/>