public string name1
{
get
{
return
TextBox9.Text.Trim();
}
}
//定义TextBox9中的内容为公用变量,设置文本框的Visible属性为False
2、ASP.NET中使用Server.Transfer()方法在页间传值
ASP.NET Server.Transfer()是在两个页面之间进行传值的好方法,从A页面Transfer到B页面时,就可以在B页面通过Context.Handler获得A页面的一个类的实例,从而在B调用A的各个成员对象。
WebForm1上放置一个TextBox1和一个Button1,程序如下:
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
Context.Items.Add("Context","Context from Form1");
}
public string Time
{
get{return DateTime.Now.ToString();}
}
public string TestFun()
{
return "Function of WebForm1 Called";
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer("WebForm2.aspx", true);
}
在WebForm2上放置一个Literal1控件,程序如下:
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Literal Literal1;
private void Page_Load(object sender, System.EventArgs e)
{
string strTxt="";
WebForm1 oForm=(WebForm1)this.Context.Handler;
strTxt+="Value of Textbox:"+Request.Form["TextBox1"] +"<br>";
strTxt+="Time Property:"+oForm.Time +"<br>";
strTxt+="Context String:"+Context.Items["Context"].ToString() +"<br>";
strTxt+=oForm.TestFun() +"<br>";
Literal1.Text =strTxt;
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
补充说明,就是Transfer方法的第二个参数指示是否保留页面的Form和QuerryString的值,你可以试着把它设为False,则在WebForm2中将读不到TextBox1的值。
3、private void FillData()
{
try
{
string myConnectionString = ConfigurationManager.AppSettings["myConnectionString"];
Random myRandomNumber = new Random();
SqlConnection myConnection = new SqlConnection(myConnectionString);
myConnection.Open();
for (int i = 0; i < 100; i++)
{
int myMultiplier = 0;
myMultiplier = myRandomNumber.Next(1, 100);
string myQuery = "INSERT INTO Data1 (value1) VALUES(" + i * myMultiplier + ")";
SqlCommand myCommand = new SqlCommand(myQuery, myConnection);
myCommand.ExecuteNonQuery();
myMultiplier = myRandomNumber.Next(1, 50);
string myQuery2 = "INSERT INTO Data2 (value1) VALUES(" + i * myMultiplier + ")";
SqlCommand myCommand2 = new SqlCommand(myQuery2, myConnection);
myCommand2.ExecuteNonQuery();
}
myConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show("There was an error filling the tables. Please try again." + ex.Message.ToString(), "Alert");
}
}
4、public DataSet CreateMyTable(DataSet myDataSet)
{
int i = 0;
for (i = 0; i < myDataSet.Tables.Count; i++)
{
if (myDataSet.Tables[i].TableName == "MyTable")
{
myDataSet.Tables["MyTable"].Clear();
myDataSet.Tables.Remove("MyTable");
}
}
myDataSet.Tables.Add("MyTable");
myDataSet.Tables["MyTable"].Columns.Add("ID", Type.GetType("System.Int32"));
myDataSet.Tables["MyTable"].Columns["ID"].Unique = true;
myDataSet.Tables["MyTable"].Columns.Add("IntegerValue1", Type.GetType("System.Int32"));
return myDataSet;
}
public DataSet CreateMyDataViewTable(DataSet myDataSet)
{
bool tableExists = false;
int i = 0;
for (i = 0; i < myDataSet.Tables.Count; i++)
{
if (myDataSet.Tables[i].TableName == "MyDataViewTable")
{
tableExists = true;
}
}
if (!tableExists)
{
myDataSet.Tables.Add("MyDataViewTable");
myDataSet.Tables["MyDataViewTable"].Columns.Add("ID", Type.GetType("System.Int32"));
myDataSet.Tables["MyDataViewTable"].Columns["ID"].Unique = true;
myDataSet.Tables["MyDataViewTable"].Columns.Add("IntegerValue1", Type.GetType("System.Int32"));
myDataSet.Tables["MyDataViewTable"].Columns.Add("IntegerValue2", Type.GetType("System.Int32"));
myDataSet.Tables["MyDataViewTable"].Columns.Add("IntegerValue3", Type.GetType("System.Int32"));
}
else
{
myDataSet.Clear();
}
return myDataSet;
}
}
5、可视化调试器(Debugging Visualizers)
在调试的时候,我们遇到的一个很麻烦的问题,就是有的时候很难看到某些参数的值。比如当你设置了一个断点去监测一个dataset,并且想看dataset里的每个datatable的数据,这在Visual Studio 2003中,是十分麻烦的,必须在auto/locals的监视窗口中,一层层地展开去看其值。在Visual Studio 2005中,为解决这个问题,新增加了叫"debugging visualizers"(可视化调试器)的功能,该功能可以在调试状态时,很容易方便地以各类形式,比如文本,html,xml等方式查看各种参数。