Tuesday, November 4, 2008

Create a discretional string(C#)

Here is a interview exam, I write it with RanDom class. OK Let's go!!

namespace StringBuildTest
{
class StringBuildT
{
string[] str = new string[] { "a", "b", "c", "d","e","f","g","h","i","j","k","l","m","n",
"o","p","q","r","s","t","u","v","w","x","y","z"};
StringBuilder SB = new StringBuilder();
Random rd = new Random();
string strOut = "";//length is 1000
public void DsiplayString()
{
for (int i = 0; i < 1000; i++)
{
SB.Append(str[(int)rd.Next(26)].ToString());
}
strOut = SB.ToString();
Console.WriteLine(strOut);
}
}

class Program
{
static void Main(string[] args)
{
StringBuildT sbt = new StringBuildT();
sbt.DsiplayString();
}
}
}

Post和Get的区别(兼谈页面间传值的方式)

I read a blog about transfer data between pages with post or get method, I think this is a good article, so past it on my blog. There's a very funny method maybe you want to focus--Context.Items
which is similar with Session.
OK,let's go!

从一个页面转向另一个页面的请求方式有两种,Post和Get.
如果从原理上来探究他们的区别,涉及到Http传输协议的细节,本文不加探究,只讨论一下表象。

所有的人都知道如下区别:
1.Post传输数据时,不需要在URL中显示出来,而Get方法要在URL中显示。
2.Post传输的数据量大,可以达到2M,而Get方法由于受到URL长度的限制,只能传递大约1024字节.
3.Post顾名思义,就是为了将数据传送到服务器段,Get就是为了从服务器段取得数据.而Get之所以也能传送数据,只是用来设计告诉服务器,你到底需要什么样的数据.Post的信息作为http请求的内容,而Get是在Http头部传输的。

我们的form表单的method方法,有两个,post,get.它在页面传值的时候的区别也就是上面提到的三点.

先来看一下post方法.
这个方法在asp时代应该跟程序员打交道很多的,因为那时候没有现在的ViewState,每个页面要恢复原来的状态,都要将页面Post给自身,然后挨个取值,重新赋值.现在这些琐碎的事情都让ViewState代劳了.所以将页面post给自身的动作,在某种程度上已经被Asp.net的程序员们忘却了,所以Post也就被大部分的忽视了,这就是技术进步的双刃剑,带来方便的同时,蒙上你的眼睛。

ViewState必须包含在<form runat="server">的窗体下,而只要包含了"runat="server""的标志,就甭想Post到其他页面中去,为什么?老盖说,我的ViewState是保存当前页面状态的,你要转到其他页面,他说,不行,**不认识的参数.如果想Post一个窗体,咋办?有四种方式可供选择.

1.在页面上新建一个form,不要加上runat="server"的标志,当然在这个窗体下的控件也就不用想用Viewstate来传值了.当在其他有runat="server"的窗体的中的按钮事件中,手动调用新建form的submit() 函数.

传送页面代码如下:

<!--html代码-->
<form id="Form1" method="post" runat="server">
<input id="btnTransfer" type="button" onclick="post();" runat="server">
<input type="text" runat="server" id="SourceData">
</form>
<form id="forPost" method="post">
<input type="text" runat="server" id="SourceData2">
</form>

<!--Script代码-->
<script language="javascript">
function post()
{
forPost.action="DestinationPage.aspx";
forPost.submit();
}
</script>


接收页面
string a=Request.Form["SourceData2"].ToString();


2.通过Session取值,在一个页面中赋值,在其他页面中共享,这个方式也被广泛应用,个人不倾向于用这种方式,我怕造成Session值的混乱无序,Session用来存一些公共的东西已经累得够呛了。

3.通过Context传值,在传送页面之前,将需要传递到其他页面的值存在Context中。示例代码如下:

传送页面

//点击某个button时触发
private void btnTransfer_ServerClick(object sender, EventArgs e)
{
Context.Items["SourceData"]=SourceData.Value;
Server.Transfer("DestinationPage.aspx");
}


接收页面

string a=Context.Items["SourceData"].ToString();


4.通过Server.Transfer的方式。
这个方式在方法三种已经用到了,不过可以在调用页面为要传递到被调用页面的值创建属性(当然可以直接将它设成public),这样就可以在其他页面访问了。

传送页面

//要传送的值
private
System.Web.UI.HtmlControls.HtmlInputText SourceData;

public string getSourceData
{
get
{
return SourceData.Value;
}
}

//传送页面
Server.Transfer("DestinationPage.aspx");


接收页面

private SourceClass sourcePage;

sourcePage=(SourceClass)Context.Handler;
string aa=sourcePage.getSourceData;


以上就是Post的在不同页面传递数据的方式了。
下面是get方法
我常用的是 传送页面

string aa=SourceData.Value;
string bb=SourceData.Value;

string url="DestinationPage.aspx?parameter1="+aa+"¶meter2="+bb;
Response.Redirect(url,false);


接收页面

string aa=Request.QueryString["parameter1"].ToString();
string bb=Request.QueryString["parameter2"].ToString();


至于 Response.Redirect(url,false)里的false都是Response.End()这个方法惹的祸,老盖说,写成false就好了,因为默认是true。我都转向其他页面了,还不让我终止原来页面的响应,BT!

方法继承

第一等的面向对象机制为C#的方法引入了virtual,override,sealed,abstract四种修饰符来提供不同的继承需求。类的虚方法是可以在该类的继承自类中改变其实现的方法,当然这种改变仅限于方法体的改变,而非方法头(方法声明)的改变。被子类改变的虚方法必须在方法头加上override来表示。当一个虚方法被调用时,该类的实例--亦即对象的运行时类型(run-time type)来决定哪个方法体被调用。我们看下面的例子:

using System;
class Parent
{
public void F() { Console.WriteLine("Parent.F"); }
public virtual void G() { Console.WriteLine("Parent.G"); }
}
class Child: Parent
{
new public void F() { Console.WriteLine("Child.F"); }
public override void G() { Console.WriteLine("Child.G"); }
}
class Test
{
static void Main()
{
Child b = new Child();
Parent a = b;
a.F();
b.F();
a.G();
b.G();
}
}

程序经编译后执行输出:

Parent.F
Child.F
Child.G
Child.G

我们可以看到class Child中F()方法的声明采取了重写(new)的办法来屏蔽class Parent中的非虚方法F()的声明。而G()方法就采用了覆盖(override)的办法来提供方法的多态机制。需要注意的是重写(new)方法和覆盖(override)方法的不同,从本质上讲重写方法是编译时绑定,而覆盖方法是运行时绑定。值得指出的是虚方法不可以是静态方法--也就是说不可以用static和virtual同时修饰一个方法,这由它的运行时类型辨析机制所决定。override必须和virtual配合使用,当然也不能和static同时使用。

那么我们如果在一个类的继承体系中不想再使一个虚方法被覆盖,我们该怎样做呢?答案是sealed override (密封覆盖),我们将sealed和override同时修饰一个虚方法便可以达到这种目的:sealed override public void F()。注意这里一定是sealed和override同时使用,也一定是密封覆盖一个虚方法,或者一个被覆盖(而不是密封覆盖)了的虚方法。密封一个非虚方法是没有意义的,也是错误的。看下面的例子:

//sealed.cs
// csc /t:library sealed.cs
using System;
class Parent
{
public virtual void F()
{
Console.WriteLine("Parent.F");
}
public virtual void G()
{
Console.WriteLine("Parent.G");
}
}
class Child: Parent
{
sealed override public void F()
{
Console.WriteLine("Child.F");
}
override public void G()
{
Console.WriteLine("Child.G");
}
}
class Grandson: Child
{
override public void G()
{
Console.WriteLine("Grandson.G");
}
}

抽象(abstract)方法在逻辑上类似于虚方法,只是不能像虚方法那样被调用,而只是一个接口的声明而非实现。抽象方法没有类似于{…}这样的方法实现,也不允许这样做。抽象方法同样不能是静态的。含有抽象方法的类一定是抽象类,也一定要加abstract类修饰符。但抽象类并不一定要含有抽象方法。继承含有抽象方法的抽象类的子类必须覆盖并实现(直接使用override)该方法,或者组合使用abstract override使之继续抽象,或者不提供任何覆盖和实现。后两者的行为是一样的。看下面的例子:

//abstract1.cs
// csc /t:library abstract1.cs
using System;
abstract class Parent
{
public abstract void F();
public abstract void G();
}
abstract class Child: Parent
{
public abstract override void F();
}
abstract class Grandson: Child
{
public override void F()
{
Console.WriteLine("Grandson.F");
}
public override void G()
{
Console.WriteLine("Grandson.G");
}
}

抽象方法可以抽象一个继承来的虚方法,我们看下面的例子:

//abstract2.cs
// csc /t:library abstract2.cs
using System;
class Parent
{
public virtual void Method()
{
Console.WriteLine("Parent.Method");
}
}
abstract class Child: Parent
{
public abstract override void Method();
}
abstract class Grandson: Child
{
public override void Method()
{
Console.WriteLine("Grandson.Method");
}
}

归根结底,我们抓住了运行时绑定和编译时绑定的基本机理,我们便能看透方法呈现出的种种overload,virtual,override,sealed,abstract等形态,我们才能运用好方法这一利器!

Use UpdatePanel to implement the ajax effect(C#)

this is a sample about how to use UpdatePanel and ScriptManager to implement ajax performance.
OK, let's go!

Client Code:
<body>
<form id="form1" runat="server">
<div>
<center>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering=true>
</asp:ScriptManager>
<asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true" Width="170px" OnSelectedIndexChanged="ddl1_SelectedIndexChanged">
<asp:ListItem>One</asp:ListItem>
<asp:ListItem>Two</asp:ListItem>
<asp:ListItem>Three</asp:ListItem>
<asp:ListItem>Four</asp:ListItem>
<asp:ListItem>Five</asp:ListItem>
</asp:DropDownList>
<br /><br /><br />

</center>
<asp:updatepanel runat="server" ID="updatepanel1">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddl1" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<asp:Label ID="lbl1" runat=server Text="" ></asp:Label>
</ContentTemplate>
</asp:updatepanel>
</div>
</form>
</body>

Code Behind:

protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
this.lbl1.Text = "You selected :" + ddl1.SelectedValue.ToString();
}

Attention: You have to put ScriptManager in the front of the all controls when you use it, or you will get a error, and you can try it to put it at last code.