本示例在运行时将文本框和按钮添加到 Web 窗体页。它还将事件处理程序动态绑定到按钮的 Click 事件。该处理程序会显示动态生成的文本框的值。
这些控件被添加到 Panel Web 服务器控件中,该控件是一个占位符。在面板中使用分行符(HTML
元素)来分隔这些控件,它们被添加到使用 LiteralControl 控件的面板中。
示例:
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
AddControls();
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if (ViewState["controsladded"] == null)
AddControls();
}
private void AddControls()
{
TextBox dynamictextbox = new TextBox();
dynamictextbox.Text = "(Enter some text)";
dynamictextbox.ID = "dynamictextbox";
Button dynamicbutton = new Button();
dynamicbutton.Click += new System.EventHandler(dynamicbutton_Click);
dynamicbutton.Text = "Dynamic Button";
Panel1.Controls.Add(dynamictextbox);
Panel1.Controls.Add(new LiteralControl("
"));
Panel1.Controls.Add(new LiteralControl("
"));
Panel1.Controls.Add(dynamicbutton);
ViewState["controlsadded"] = true;
}
private void dynamicbutton_Click(Object sender, System.EventArgs e)
{
TextBox tb = new TextBox();
tb = (TextBox) (Panel1.FindControl("dynamictextbox"));
Label1.Text = tb.Text;
}
编译代码-----》本示例需要: Web 窗体页。 称作 Panel1 的 Panel 控件。 称作 Label1 的 Label 控件。
2.代码:将项添加到列表控件 (Visual C#)
本示例使用 ListBox.Items 属性的 Add 方法向列表中添加项。
示例:
// Create a string array and assign items.
string[] ListContents = {"Enju", "Sue", "Mary"};
// Add the array items to the list box.
for (int i = 0; i <= ListContents.Length - 1; i++)
ListBox1.Items.Add(ListContents[i]);
编译代码------》启动新的 ASP.NET Web 应用程序并执行以下操作:
------》添加名为 ListBox1
的列表框。
------》复制代码并将其粘贴到 Page_Load 方法中。
3.代码:检测浏览器的版本 (Visual C#)
本示例将检查 Request.Browser 属性以确定客户端 Web 浏览器的类型和版本。通过 Request.Browser 属性还可以访问浏览器的其他许多属性,如 Url、ServerVariables 和 User。
示例:
HttpBrowserCapabilities brObject = Request.Browser;
// Display the type and version.
Response.Write("Browser Type: "+ brObject.Type);
Response.Write("
"+"Browser Version: "+ brObject.Version);
编译代码-----》启动新的 ASP.NET Web 应用程序并将代码粘贴到 Page_Load 方法中。
4.代码:确定列表控件中所选的项 (Visual C#)
本示例使用列表控件的 SelectedIndex 和 SelectedItem 属性访问有关所选项的信息。
示例:
// The index:
int ListItemIndex;
// Value of the item:
string ListItemValue;
ListItemIndex = ListBox1.SelectedIndex;
ListItemValue = ListBox1.SelectedItem.Value.ToString();
编译代码-----》启动新的 ASP.NET Web 应用程序并执行以下操作:
添加名为 ListBox1
的列表框并在其中填充项。
复制代码并将其粘贴到 Page_Load 方法中。
5.代码:显示 Web 窗体 DataGrid 控件中的数据 (Visual C#)
本示例使用 DataBind 方法将数据集的内容绑定到 DataGrid 控件。
示例:
if (!IsPostBack)
{
sqlDataAdapter1.Fill(dataSet11);
DataGrid1.DataBind();
}
编译代码
本示例需要:
- Web 窗体页。
- 名为
DataGrid1
的 DataGrid 控件。 - 名为
dataSet11
的类型化数据集,该数据集是类型化数据集类DataSet1
的实例。 - 正确配置的名为
sqlDataAdapter1
的数据适配器。可以将 DataGrid 控件绑定到任何 Web 窗体页数据源。 - 应用程序必须具有访问数据库的权限。
6.代码:将数据从一个 Web 窗体页传递到另一个 Web 窗体页 (Visual C#)
本示例保存会话状态中的信息并定位到同一应用程序中的另一页,然后在第二页中检索存储的信息。
示例:
// First part: Saves info on first page. string firstName = "John"; string lastName = "Smith"; string city = "Seattle"; Session.Add("First", firstName); Session.Add("Last", lastName); Session.Add("City", city); // Redirect to second page. Server.Transfer("WebForm2.aspx"); // Second part: Reads info into variables on the second page. string firstName = (string)(Session["First"]); string lastName = (string)(Session["Last"]); string city = (string)(Session["City"]);
编译代
- 启动具有两个 Web 窗体页(WebForm1.aspx 和 WebForm2.aspx)的 ASP.NET 应用程序。
- 将代码的第一部分粘贴到 WebForm1.aspx 的 Page_Load 方法(或 Click 方法)中。
- 将代码的第二部分粘贴到 WebForm2.aspx Page_Load 方法中。
7.代码:读取 Cookie (Visual C#)
下面示例使用 HttpCookie 类及其属性读取具有特定名称的 Cookie。
示例:
HttpCookie myCookie = new HttpCookie("MyTestCookie"); myCookie = Request.Cookies["MyTestCookie"]; // Read the cookie information and display it. if (myCookie != null) Response.Write("
"+ myCookie.Name + "
"+ myCookie.Value); else Response.Write("not found");
编译代 -----》启动新的 ASP.NET Web 应用程序并将代 粘贴到 Page_Load 方法中。
8.代 :读取应用程序状态中的值 (Visual C#)
本示例从 HttpApplicationState 对象检索值。
示例:
string appMsg; appMsg = (string)(Application["Message"]);
编译代 -----》启动新的 ASP.NET 应用程序并将代 粘贴到 Page_Load 方法中。
可 编程-----》
必须将数据转换成适当的类型(如此例中所示)。
如果尝试从不存在的应用程序状态中获取值,则不会引发任何异常。若要确保所需的值在应用程序状态中,请首先使用测试(例如以下测试)检查该对象是否存在:
if (Application["Message"] == null) Response.Write("No such value in application state.");
9.代 :读取会话状态中的值 (Visual C#)
本示例访问 HttpSessionState.Item 属性来检索处于会话状态的值。
示例:
string firstName = (string)(Session["First"]); string lastName = (string)(Session["Last"]); string city = (string)(Session["City"]);
编译代 -----》启动新的 ASP.NET 应用程序并将代 粘贴到 Page_Load 方法中。
可 编程-----》
会话值的类型为 Object。读取它们时应将其转换成适当的类型。
如果尝试从不存在的会话状态中获取值,则不会引发任何异常。若要确保所需的值在会话状态中,请首先使用测试(例如以下测试)检查该对象是否存在:
if (Session["City"] == null) Response.Write("No such value in session state.");
10.代 :从视图状态中读取值 (Visual C#)
本示例将值 "yellow"
保存到 Control.ViewState 属性中,然后当该页回发到服务器时检索该属性的值。有关 ViewState 的更多信息,请参见使用视图状态保存 Web 窗体页值。
示例:
string strColor; if (Page.IsPostBack) { // Retrieve and display the property value. strColor = (string)ViewState["color"]; Response.Write(strColor); } else // Save the property value. ViewState["color"] = "yellow";
编译代 -----》复制代 并将其粘贴至 ASP.NET Web 应用程序的 Page_Load 方法中。
来源:http://msdn.microsoft.com/zh-cn/library/aa287564(VS.71).aspx