谈谈用户控件的用法:
简单说,一个用户控件的获得最简单的方式是把一个aspx文件后缀改为ascx并把<%@Page....%>中Page改为Control 指令即可.
具体举个例子:
<%@Control Language="C#"%>//注意此处
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="C#" runat="server">
public void Page_Load(Object Source, EventArgs E)
{
if (!IsPostBack)
{
SqlConnection myConnection;
SqlCommand myCommand;
SqlDataReader myReader ;
string SQL;
string ConnStr;
SQL = "select * from Shippers";
ConnStr = ConfigurationSettings.AppSettings["DsnNorthwind"];
myConnection = new SqlConnection(ConnStr);
myConnection.Open();
myCommand = new SqlCommand(SQL, myConnection);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
ShipMethod.Items.Add(new ListItem(myReader["CompanyName"].ToString(), myReader["ShipperID"].ToString()));
}
}
</script>
<asp:DropDownList id="ShipMethod" runat="server"/>
接下来是有关用户控件属性的问题:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="C#" runat="server">
string ConnStr;
public string ConnectionString
{
get {return ConnStr;}
set {ConnStr = value;}
}
//这里定义了属性ConnectionString ,具体用法见后面
public void Page_Load(Object Source, EventArgs E)
{
if (!IsPostBack)
{
SqlConnection myConnection;
SqlCommand myCommand;
SqlDataReader myReader;
string SQL;
SQL = "select * from Shippers";
if (ConnStr == "")//设置默认连接字符串
ConnStr = ConfigurationSettings.AppSettings["DsnNorthwind"];
myConnection = new SqlConnection(ConnStr);
myConnection.Open();
myCommand = new SqlCommand(SQL, myConnection);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
ShipMethod.Items.Add(new ListItem(myReader["CompanyName"].ToString(), myReader["ShipperID"].ToString()));
}
}
</script>
<asp:DropDownList id="ShipMethod" runat="server"/>
在aspx文件中用法:
<%@ Page Language="C#" %>
<%@ Register tagprefix="wrox" Tagname="shipment" Src="shipmethod_prop.ascx" %>
<html>
<head>
<title>Testing our User Control - Part 2</title>
</head>
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
ShipMethod.ConnectionString = ConfigurationSettings.AppSettings["DsnNorthwind"];
}//注意这儿
</script>
<body>
<form runat="server">
<strong>Here is some body text.</strong>
<P>Please choose a shipping method:
<wrox:shipment id="ShipMethod" runat="server" /></P>
</form>
</body>
</html>
还有种方法:
<wrox:shipment ConnectionString="server=localhost;uid=sa;pwd=;database=Northwind" runat="server" />
即:把ConnectionString和值放入wrox:shipment用户控件中作为属性
再说说事件的设置:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="C#" runat="server">
string ConnStr;
public string ConnectionString
{
get {return ConnStr; }
set {ConnStr = value;}
}
public void Page_Load(Object Source, EventArgs E)
{
if (!IsPostBack)
{
SqlConnection myConnection;
SqlCommand myCommand;
SqlDataReader myReader;
string SQL;
SQL= "select * from Shippers";
if (ConnStr == "")
ConnStr = ConfigurationSettings.AppSettings["DsnNorthwind"];
myConnection = new SqlConnection(ConnStr);
myConnection.Open();
myCommand = new SqlCommand(SQL, myConnection);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
ShipMethod.Items.Add(new ListItem(myReader["CompanyName"].ToString(), myReader["ShipperID"].ToString()));
}
}
public void ShipMethod_Change(Object Source, EventArgs E)
{
SelectedMethod.Text = "You have selected " + ShipMethod.SelectedItem.Text + " as your shipping method.";
} //这儿定义了事件处理函数
</script>
<asp:DropDownList AutoPostBack="true" OnSelectedIndexChanged = "ShipMethod_Change" id="ShipMethod" runat="server"/>
//这儿给asp:DropDownList添加了事件OnSelectedIndexChanged并且把事件处理函数指向前面设置的ShipMethod_Change
<BR><asp:Label id="SelectedMethod" runat="server"/>
aspx文件中调用该用户控件时不考虑事件,因为在用户控件中已经处理了.
最后是代码后置问题:(以下代码存贮为ShipMethod.cs)
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
public class ShipMethodClass : UserControl
{
// public variables to match the server controls
public DropDownList ShipMethod;
public Label YouSelected;
public Button PlaceOrder;
public void Page_Load(Object Source, EventArgs E)
{
if (!Page.IsPostBack)
{
SqlConnection myConnection;
SqlCommand myCommand;
SqlDataReader myReader;
String SQL;
String ConnStr;
SQL = "select * from Shippers";
ConnStr = ConfigurationSettings.AppSettings["DsnNorthwind"];
myConnection = new SqlConnection(ConnStr);
myConnection.Open();
myCommand = new SqlCommand(SQL, myConnection);
myReader = myCommand.ExecuteReader();
ShipMethod.DataTextField = "CompanyName";
ShipMethod.DataSource = myReader;
ShipMethod.DataBind();
}
}
public void ShipMethod_Change(Object Source, EventArgs E)
{
YouSelected.Text = "Your order will be delivered via " +
ShipMethod.SelectedItem.Text;
}
}
而相应的ascx控件文件为:
<%@ Control inherits="ShipMethodClass" src="ShipMethod.cs" %>//注意此处的Control
<asp:DropDownList AutoPostBack="true" OnSelectedIndexChanged="ShipMethod_Change" id="ShipMethod" runat="server"/>
<BR><asp:Label id="YouSelected" runat="server"/>
好了,以上就是今天学习的用户控件的用法.在前面学习时曾经谈到过自定义控件.那么自定义控件又是如何使用的呢???留在后面的学习中再解决...