
<form id="webForm" method="post" runat="server"> <asp:DropDownList id="dropDownList" runat="server"></asp:DropDownList> <asp:Button id="button" runat="server" Text="Button"></asp:Button> <asp:DataGrid id="dataGrid" runat="server"></asp:DataGrid> </form>
//頁(yè)面初始化事件 private void Page_Load(object sender, System.EventArgs e) { if ( ! IsPostBack ) { string SQL_SELECT_PORTAL = "SELECT * FROM PORTAL"; //使用using確保釋放數據庫連接 //連接字符串存放在Web.Config文件中便于修改 using( SqlConnection conn = new SqlConnection( ConfigurationSettings.AppSettings["ConnectionString"] ) ) { SqlDataAdapter dataAdapter = new SqlDataAdapter( SQL_SELECT_PORTAL, conn ); DataSet dataSet = new DataSet(); dataAdapter.Fill( dataSet ); //設置下拉列表的數據源與文本域、值域 dropDownList.DataSource = dataSet; dropDownList.DataTextField = "portalName"; dropDownList.DataValueField = "portalId"; dropDownList.DataBind(); } } } //Button的Click事件 private void button_Click(object sender, System.EventArgs e) { string SQL_SELECT_SUBJECT = "SELECT * FROM SUBJECT WHERE portalId = {0}"; using( SqlConnection conn = new SqlConnection( ConfigurationSettings.AppSettings["ConnectionString"] ) ) { //用下拉列表選擇的值替換掉SQL語(yǔ)句中的待定字符{0} SqlDataAdapter dataAdapter = new SqlDataAdapter( string.Format( SQL_SELECT_SUBJECT, dropDownList.SelectedValue ), conn ); DataSet dataSet = new DataSet(); dataAdapter.Fill( dataSet ); dataGrid.DataSource = dataSet; dataGrid.DataBind(); } } 

private static string SQL_SELECT_PORTAL = "SELECT * FROM PORTAL"; private static string SQL_SELECT_SUBJECT = "SELECT * FROM SUBJECT WHERE portalId = {0}"; private static string SQL_CONNECTION_STRING = ConfigurationSettings.AppSettings["ConnectionString"]; public static DataSet GetPortal() { return GetDataSet( SQL_SELECT_PORTAL ); } public static DataSet GetSubject( string portalId ) { return GetDataSet( string.Format( SQL_SELECT_SUBJECT, portalId ) ); } public static DataSet GetDataSet( string sql ) { using( SqlConnection conn = new SqlConnection( SQL_CONNECTION_STRING ) ) { SqlDataAdapter dataAdapter = new SqlDataAdapter( sql, conn ); DataSet dataSet = new DataSet(); dataAdapter.Fill( dataSet ); return dataSet; } } Controller(webForm.aspx.cs):負責轉化用戶(hù)的輸入 private void Page_Load(object sender, System.EventArgs e) { if ( ! IsPostBack ) { //調用Model的方法獲得數據源 dropDownList.DataSource = SQLHelper.GetPortal(); dropDownList.DataTextField = "portalName"; dropDownList.DataValueField = "portalId"; dropDownList.DataBind(); } } private void button_Click(object sender, System.EventArgs e) { dataGrid.DataSource = SQLHelper.GetSubject( dropDownList.SelectedValue ); dataGrid.DataBind(); } 

Page Controller(BasePage.cs): public class BasePage : System.Web.UI.Page { private string _title; public string Title//頁(yè)面標題,由子類(lèi)負責指定 { get { return _title; } set { _title = value; } } public DataSet GetPortalDataSource() { return SQLHelper.GetPortal(); } public DataSet GetSubjectDataSource( string portalId ) { return SQLHelper.GetSubject( portalId ); } protected override void Render( HtmlTextWriter writer ) { writer.Write( "<html><head><title>" + Title + "</title></head><body>" );//統一的頁(yè)面頭 base.Render( writer );//子頁(yè)面的輸出 writer.Write( @"<a href=""http://www.asp.net"">ASP.NET</a></body></html>" );//統一的頁(yè)面尾 } } public class webForm : BasePage//繼承頁(yè)面基類(lèi) { private void Page_Load(object sender, System.EventArgs e) { Title = "Hello, World!";//指定頁(yè)面標題 if ( ! IsPostBack ) { dropDownList.DataSource = GetPortalDataSource();//調用基類(lèi)的方法 dropDownList.DataTextField = "portalName"; dropDownList.DataValueField = "portalId"; dropDownList.DataBind(); } } private void button_Click(object sender, System.EventArgs e) { dataGrid.DataSource = GetSubjectDataSource( dropDownList.SelectedValue ); dataGrid.DataBind(); } } 
因為Front Controller模式要比上面兩個(gè)模式復雜一些,我們再來(lái)看看例子的類(lèi)圖:

<!-- 指定對Dummy開(kāi)頭的aspx文件交由Handler處理 --> <httpHandlers> <add verb="*" path="/WebPatterns/FrontController/Dummy*.aspx" type="WebPatterns.FrontController.Handler,WebPatterns"/> </httpHandlers> <!-- 指定名為FrontControllerMap的頁(yè)面映射塊,交由UrlMap類(lèi)處理,程序將根據key找到對應的url作為最終的執行路徑,您在這可以定義多個(gè)key與url的鍵值對 --> <configSections> <section name="FrontControllerMap" type="WebPatterns.FrontController.UrlMap, WebPatterns"></section> </configSections> <FrontControllerMap> <entries> <entry key="/WebPatterns/FrontController/DummyWebForm.aspx" url="/WebPatterns/FrontController/ActWebForm.aspx" /> 。。。 </entries> </FrontControllerMap> 修改webForm.aspx.cs: private void button_Click( object sender, System.EventArgs e ) { Response.Redirect( "DummyWebForm.aspx?requestParm=" + dropDownList.SelectedValue ); } 當程序執行到這里時(shí)將會(huì )根據Web.Config里的定義觸發(fā)類(lèi)Handler的ProcessRequest事件: Handler.cs: public class Handler : IHttpHandler { public void ProcessRequest( HttpContext context ) { Command command = CommandFactory.Make( context.Request.Params ); command.Execute( context ); } public bool IsReusable { get { return true; } } } public class CommandFactory { public static Command Make( NameValueCollection parms ) { string requestParm = parms["requestParm"]; Command command = null; //根據輸入參數得到不同的Command對象 switch ( requestParm ) { case "1" : command = new FirstPortal(); break; case "2" : command = new SecondPortal(); break; default : command = new FirstPortal(); break; } return command; } } public interface Command { void Execute( HttpContext context ); } public abstract class RedirectCommand : Command { //獲得Web.Config中定義的key和url鍵值對,UrlMap類(lèi)詳見(jiàn)下載包中的代碼 private UrlMap map = UrlMap.SoleInstance; protected abstract void OnExecute( HttpContext context ); public void Execute( HttpContext context ) { OnExecute( context ); //根據key和url鍵值對提交到具體處理的頁(yè)面 string url = String.Format( "{0}?{1}", map.Map[ context.Request.Url.AbsolutePath ], context.Request.Url.Query ); context.Server.Transfer( url ); } } public class FirstPortal : RedirectCommand { protected override void OnExecute( HttpContext context ) { //在輸入參數中加入項portalId以便頁(yè)面處理 context.Items["portalId"] = "1"; } } public class SecondPortal : RedirectCommand { protected override void OnExecute(HttpContext context) { context.Items["portalId"] = "2"; } } 最后在A(yíng)ctWebForm.aspx.cs中: dataGrid.DataSource = GetSubjectDataSource( HttpContext.Current.Items["portalId"].ToString() ); dataGrid.DataBind(); <%@ OutputCache Duration="60" VaryByParam="none" %>,
public static DataSet GetPortal() { DataSet dataSet; if ( HttpContext.Current.Cache["SELECT_PORTAL_CACHE"] != null ) { //如果數據存在于緩存中則直接取出 dataSet = ( DataSet ) HttpContext.Current.Cache["SELECT_PORTAL_CACHE"]; } else { //否則從數據庫中取出并插入到緩存中,設定絕對過(guò)期時(shí)間為3分鐘 dataSet = GetDataSet( SQL_SELECT_PORTAL ); HttpContext.Current.Cache.Insert( "SELECT_PORTAL_CACHE", dataSet, null, DateTime.Now.AddMinutes( 3 ), TimeSpan.Zero ); } return dataSet; } Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=48637
聯(lián)系客服