2008年11月2日
今有应用程序 WindowsFormsApplication1 和WebService WebService1.asmx,WindowsFormsApplication1调用WebService1,引用名称为localhost。
WebService代码

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebApplication1
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
应用程序代码

Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
localhost.WebService1 service = new WindowsFormsApplication1.localhost.WebService1();
MessageBox.Show(service.HelloWorld());
}
}
}
编译后运行,正常。
使用Dotfuscator对该应用程序进行混淆后运行,调用WebService处引发 System.Configuration.SettingsPropertyNotFoundException 异常,错误信息如下

Code
System.Configuration.SettingsPropertyNotFoundException: 没有找到设置属性“WindowsFormsApplication1_localhost_WebService1”。
在 System.Configuration.SettingsBase.GetPropertyValueByName(String propertyName)
在 System.Configuration.SettingsBase.get_Item(String propertyName)
在 System.Configuration.ApplicationSettingsBase.GetPropertyValue(String propertyName)
在 System.Configuration.ApplicationSettingsBase.get_Item(String propertyName)
在 d.b()
在 a..ctor()
在 c.a(Object A_0, EventArgs A_1)
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
从错误信息中可以看出程序运行时找不到名为“WindowsFormsApplication1_localhost_WebService1”的属性,该属性被混淆器改了名称了,所以找不到。打开Dotfuscator查看“Rename”选项卡,如下图

可以看到有读取属性WindowsFormsApplication1_localhost_WebService1的方法get_WindowsFormsApplication1_localhost_WebService1。要使混淆后的应用程序正常运行,可以选择不重命名“Settings”(选中该项即可)。另外,一样不能重命名WebService的方法,不然会引发“System.ArgumentException: xxxxxx Web 服务方法名无效。”(xxxxxx为服务方法名称)的异常。
不知是否还有更好的方法?
2008年10月27日
请看下面的代码段,在IE7中执行后弹出的代码是什么样的呢?

Code
var url = "HTMLPage1.htm";
var features = "width=400,height=300,status=yes,toolbar=no,menubar=no,location=no";
window.open(url, null, features);
测试结果如下,可以发现窗口大小不能调整,并且没有显示滚动条。

记得IE6时,窗口的大小可以改变且滚动条也是默认显示的。看MSDN文档,resizable和scrollbars都是默认为yes的。(难道是未更新到IE7)
| resizable = { yes | no | 1 | 0 } |
Specifies whether to display resize handles at the corners of the window. The default is yes.
Internet Explorer 7. resizable = { no | 0 } disables tabs in a new window. |
| scrollbars = { yes | no | 1 | 0 } |
Specifies whether to display horizontal and vertical scroll bars. The default is yes. |
IE7中要调整窗口大小和显示滚动条,应该增加resizable=yes和scrollbars=yes,如下

Code
var url = "HTMLPage1.htm";
var features = "width=400,height=300,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes";
window.open(url, null, features);
结果如下图所示

2007年11月22日
此文转载自
http://www.dennydotnet.com/post/2007/09/A-DataTable-Serializer-for-ASPNET-AJAX.aspx,未作翻译。
A DataTable Serializer for ASP.NET AJAX, implements
JavaScriptConverter. Note that I did not implement a
Deserialize method since I am using this for read only data.
[code:c#]

/**//// <summary>
/// DataTable to JSON converter
/// </summary>

public class JavaScriptDataTableConverter : JavaScriptConverter
{

public override object Deserialize( IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer )
{
throw new NotImplementedException( "Deserialize is not implemented." );
}


public override IDictionary<string, object> Serialize( object obj, JavaScriptSerializer serializer )
{
DataTable dt = obj as DataTable;
Dictionary<string, object> result = new Dictionary<string, object>();


if( dt != null && dt.Rows.Count > 0 )
{
// List for row values
List<object> rowValues = new List<object>();


foreach( DataRow dr in dt.Rows )
{
// Dictionary for col name / col value
Dictionary<string, object> colValues = new Dictionary<string, object>();


foreach( DataColumn dc in dt.Columns )
{
colValues.Add( dc.ColumnName, // col name
( string.Empty == dr[dc].ToString() ) ? null : dr[dc] ); // col value
}

// Add values to row
rowValues.Add( colValues );
}

// Add rows to serialized object
result["rows"] = rowValues;
}

return result;
}


public override IEnumerable<Type> SupportedTypes
{
//Define the DataTable as a supported type.

get
{
return new System.Collections.ObjectModel.ReadOnlyCollection<Type>(
new List<Type>(

new Type[]
{ typeof( DataTable ) }
)
);
}
}
}


[/code]
And how do you implement this? In a web service...
That's all there is to it! Just deserialize to an object on the client-side and you're good to go!
Side Note: There is a DataTable serializer from Microsoft in the ASP.NET Futures package.
2007年11月21日
摘要: 如何拷贝对象?使用赋值运算符拷贝对象的误区。
阅读全文
2007年10月24日
默认情况下,Views的目录结构如下:
Views
----Home
--------Index.vm
----Login
--------Index.vm
----…
如果程序中提供多套模板,建议改为如下的目录结构:
Views
----Template1 //模板1
--------Home
------------Index.vm
------------…
----Template2 //模板2
--------Home
------------Index.vm
------------…
修改目录结构之后,需要修改Web.config中的配置,如下:
<viewEngines viewPathRoot="Views\Template1">
<add xhtml="false" type="Castle.MonoRail.Framework.Views.NVelocity.NVelocityViewEngine, Castle.MonoRail.Framework.Views.NVelocity" />
</viewEngines>
即通过修改viewPathRoot属性的值来实现更换模板。对于每个用户使用不同模板的情况不适用。
2007年10月16日
摘要: Web应用程序中,使用一个xml文件来保存配置信息,并放在了bin目录下,每当对xml文件进行了写操作后,下一次操作都感觉程序特别慢。发生这个情况是因为bin目录下xml文件的改变,造成了程序的重新编译。不要把在程序中可能发生变化的文件放在bin目录下。
阅读全文
2007年9月28日
摘要: SELECT*FROM(SELECTr.*,ROWNUMasrow_numberFROM表名rWHEREROWNUM<=结束行数)where开始行数<=row_number越靠前的页面显示越快,越靠后的页面显示越慢。SQL出处使用 PHP 和 Oracle 实施分页结果集作者:Harry Fuecks
阅读全文
2007年8月29日
摘要: jQuery官方网站:http://jquery.com/功能实现:用户在输入文字时,如果能高亮显示正在输入的那个文本框的话,会更人性化些,下面就使用jQuery来实现。实现原理:在document加载完成后(ready),添加input的focus和blur事件,并进行增加和删除样式的操作。代码示例:1<html>2<head><title></titl...
阅读全文
2007年8月28日
摘要: 实现原理:记录休眠开始时间,循环判断当前时间与开始时间相隔的秒数,如果超过了指定的秒数,则跳出循环。增加超时是解决换天(23点向0点过渡)的时候会出现的当前秒数比开始秒数小的问题;代码如下:1functionGetCurrentTimeSeconds()2{3//获取当前时间表示的秒数4vard=newDate();56varhour=d.getHours();78varminute=d.getM...
阅读全文
2006年8月11日
摘要: 在实际的应用中,往往需要一行显示多条记录,使用DataList控件可以实现本功能。 关键属性: RepeatColumns 设置一行显示多少列,即显示多少条记录。
阅读全文