博文
动态创建控件时,Page.Load事件之后也需要恢复ViewState(2010-09-15 21:52:00)
摘要:我曾经发过一个帖子问这样的问题:
在下面的代码中,我想实现的是:
(1) 第一次打开网页,TextBox中的文本为"Good Morning."
(2) 单击Button,形成Postback,然后更改TextBox的文本为"Good Afternoon"
可是不管怎样,TextBox的文本总是 "Good Morning.", 请各位帮我看看是什么原因。
(我不想捕捉Button的Onclick事件)
C# code
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button button1 = new Button();
button1.Text = "Click";
button1.ID = "button1";
TextBox textBox1 = new TextBox();
te......
如何使用嵌入资源(2010-02-14 00:55:00)
摘要:假如我们要使用两幅图片,并把它们编译到DLL中去。
首先我们要把这些图片的属性进行修改,
然后我们还需要以下的代码,
[assembly: WebResource("WebApplication2.adobe.jpg", "image/jpg")]
[assembly: WebResource("WebApplication2.img.xx.gif", "image/gif")]
这些代码可以放在AssemblyInfo.cs中,也可以放到其他地方,比如像下面这样
[assembly: WebResource("WebApplication2.adobe.jpg", "image/jpg")]
[assembly: WebResource("WebApplication2.img.xx.gif", "image/gif")]
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Image1.ImageUrl = Page.ClientScript.GetWebResourceUrl(typeof(_Default), "WebApplication2.img.xx.gif");
}
}
}
最后在使用的时候要像上面代码中Page.ClientScript.GetWebResourceUrl那样使用。......
浅谈ViewState(2010-02-03 16:54:00)
摘要:参见《Pro ASP.NET 3.5 in C# 2008》
Once your page code(.aspx) has finished running(and just before the final HTML is rendered and sent to the client), ASP.NET examines all the properties(.aspx) of all the controls on your page. If any of these properties has been changed from its initial state, ASP.NET makes a note of this information in a name/value collection. Finally, ASP.NET takes all the information it has amassed and then serializes it as a Base64 string. (A Base64 string ensures that there aren’t any special characters that wouldn’t be valid HTML.) The final string is inserted in the <form> section of the page as a new hidden field.
The next time the page is posted back, ASP.NET follows these steps:
1. ASP.NET re-creates the page and control objects based on its default(as defined in the .aspx file). Thus, the page has the same state that it had when it was first requested.
2. Next, ASP.N......
