日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Community Server系列之二:页面之间的关系1[介绍]

發布時間:2025/7/14 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Community Server系列之二:页面之间的关系1[介绍] 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?????? 當我們第一眼見到CS的時候你是不是被他那么紛繁復雜的結構看傻眼呢。那么在認識CS之前最好對它的頁面間關系做一個全面的了解,這對我門改造CS有較大的幫助。
?????? 首先我們對整體一個簡單的了解,如圖,此為在IDE中打開的項目列表:


?

?????? 其中CommunityServerWeb項目為IIS運行的WEB項目,項目的UI層相關的都放在此處。CommunityServerComponents和CommunityServerControls都是支持整個系統運行所必須的組件,其中CommunityServerControls項目里有大量的系統公用的控件,由于本系統的幾乎所有頁面都是由用戶控件組合而成的所以需要先了解在CS中用戶控件的分布機制。

CS用戶控件:我們知道,用戶控件一般以.ascx為擴展名,在建立時都自帶了相應的.cs文件,而在CS中考慮到明晰的分層結構和方便的修改UI以及最大程度的換膚功能,程序使用了后臺代碼與前臺UI分開在兩個層中,拿常用的CommunityServerControls這個項目來說在項目里我們看不到ascx文件的蹤影,原來ascx文件保存在UI層,并且按照各種風格定義了不一樣的ascx文件,具體的路徑可以參照下圖的導航找到:

?

??????? 我們可以看到,在UI層的Themes文件夾里保存了所有在其他層實現的用戶控件的ascx文件,我想有必要介紹一下此目錄的結構。

??????? 如上圖,default文件夾里保存了系統平臺使用的默認風格的UI文件,其他文件夾為相應的其他風格的文件集合。當然在此Blogs和Galleries兩個文件夾分別是針對博客的皮膚和相冊的皮膚,因為這兩個項目需要根據具體用戶的需要單獨制定樣式,和網站整體的樣式不相關所以單獨保存在平行的文件夾里。讓我們再看看default文件夾里有些什么,首先是images這個不用說,就是保存了此種風格需要的圖片資源,Masters文件夾里保存了頁面的整體風格的相關框架,這個在后面詳細描述,Skin文件夾里保存的既是大量的用戶控件的UI實現,其中命名規則為:skin-ClassName.ascx 其中ClassName為相對應的類名,注意文件名一定要按照這個規則來命名,程序運行的時候,后臺的是根據這個名稱來找到相應的UI層文件的,這樣隔離了依賴關系,前臺不用被后臺代碼束縛了,此種模式我們把它叫著模板用戶控件,因為UI層的職責已經很明確了那就是提供控件展現的模板供后臺代碼調用。

??????? 可以這樣來看頁面執行過程:用戶請求aspx的頁面à程序查找頁面中的用戶控件à實例話用戶控件à實例化時根據配置找到相應風格相應名稱的ascx文件一并實例化à生成關聯后的控件對象返回給aspx頁面。這樣看來,雖然代碼分離了,達到的效果卻沒有變。

問題出現了,后臺代碼和UI層的同步問題,我們在平時建立用戶控件或aspx文件的時候IDE自動為我們生成了很多代碼,使得我們不需要考慮前臺的控件和后臺代碼的對應問題,而這里代碼和UI是分開的,涉及到增加刪減控件不同步的問題。要達到用戶在UI層刪除一個元素之后頁面也不會出錯,用戶隨時需要了可以隨時修改回去,在CS中是這樣處理的,拿登陸的用戶控件來看(\src\Controls\Login.cs&\src\Web\Themes\default\Skins\Skin-Login.ascx)

??? 如圖,這是LoginCommunityServerControls項目里的繼承關系,我們的重點應該放在TemplatedWebControl這個類里,此類是所有通過模板方式建立用戶控件的基類,在這里包含了處理模板相關的功能,比如命名規則自動找到相應的ascx文件等等,這里注意一下這個的方法:protected abstract void AttachChildControls();可以看出此方法為抽象的,而且整個類里也就只有這么一個抽象方法,由繼承關系我們知道在Login類里必須實現此方法,此方法的作用是干什么的呢,讓我們看看Login類里這個方法都干了些什么:
login.cs
head#region?head
//------------------------------------------------------------------------------
//?<copyright?company="Telligent?Systems">
//?????Copyright?(c)?Telligent?Systems?Corporation.??All?rights?reserved.
//?</copyright>?
//------------------------------------------------------------------------------

using?System;
using?System.Web;
using?System.Web.Security;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?CommunityServer.Components;

#endregion

namespace?CommunityServer.Controls?
{
????[
????????ParseChildren(
true)
????]
????
public?class?Login?:?SecureTemplatedWebControl?{

????????CSContext?csContext?
=?CSContext.Current;
????????TextBox?username;
????????TextBox?password;
????????IButton?loginButton;
????????CheckBox?autoLogin;

????????
Other?Code#region?Other?Code
????????
//?*********************************************************************
????????
//??Login
????????
//
????????/**////?<summary>
????????
///?Constructor
????????
///?</summary>

????????//?***********************************************************************/
????????public?Login()?:?base()?
????????
{

????
????????}


????????
protected?string?ReferralLink
????????
{
????????????
get
????????????
{
????????????????
return?ViewState["ReferralLink"]?as?string;
????????????}

????????????
set
????????????
{
????????????????ViewState[
"ReferralLink"]?=?value;
????????????}

????????}


????????
protected?override?void?OnInit(EventArgs?e)
????????
{
????????????
this.EnableViewState?=?true;
????????????
base.OnInit?(e);
????????}



????????
protected?override?void?OnLoad(EventArgs?e)
????????
{


????????????
base.OnLoad?(e);

????????????
if(!Page.IsPostBack)
????????????
{
????????????????Uri?referral?
=??Context.Request.UrlReferrer;
????????????????
if(referral?!=?null)
????????????????????ReferralLink?
=?referral.ToString();
????????????}

????????}



????????
//?*********************************************************************
????????
//??CreateChildControls
????????
//
????????/**////?<summary>
????????
///?This?event?handler?adds?the?children?controls.
????????
///?</summary>

????????//?***********************************************************************/
????????protected?override?void?CreateChildControls()?
????????
{
????????????
//?If?the?user?is?already?authenticated?we?have?no?work?to?do
????????????if(Page.Request.IsAuthenticated)
????????????
{
????????????????
//?If?the?URL?is?for?the?login?page?and?the?user?is?already?logged?in
????????????????
//?we?need?to?throw?an?access?denied?exception
????????????????if?(Globals.GetSiteUrls().Login.StartsWith(csContext.Context.Request.Path))?
????????????????????
throw?new?CSException?(CSExceptionType.UserAlreadyLoggedIn,?csContext.ReturnUrl);

????????????????
return;
????????????}


????????????
base.CreateChildControls();
????????}

????????
#endregion


????????
protected?override?void?AttachChildControls()
????????
{
????????????
//?Find?the?username?control
????????????username?=?(TextBox)?FindControl("username");

????????????
//?Find?the?password?control
????????????password?=?(TextBox)?FindControl("password");

????????????
//?Find?the?login?button
????????????loginButton?=?ButtonManager.Create(FindControl("loginButton"));

????????????loginButton.Click?
+=?new?EventHandler(LoginButton_Click);
????????????loginButton.Text?
=?ResourceManager.GetString("LoginSmall_Button");

????????????
//?Find?the?autologin?checkbox
????????????autoLogin?=?(CheckBox)?FindControl("autoLogin");
????????????autoLogin.Text?
=?ResourceManager.GetString("LoginSmall_AutoLogin");????????
????????????
//autoLogin.AutoPostBack?=?true;
????????????
//autoLogin.CheckedChanged?+=?new?EventHandler(?AutoLogin_OnCheckedChanged?);
????????????
????????????
//?Restore?autologin?status?from?the?auto?login?cookie
????????????AutoLoginCookie?alCookie?=?new?AutoLoginCookie();
????????????autoLogin.Checked?
=?alCookie.GetValue();

????????????RegisterSetFocusScript();
????????}


????????
Other?Codes#region?Other?Codes
????????
/**////?<summary>
????????
///?Event?handler?to?update?auto?login?cookie?value.
????????
///?</summary>

????????protected?void?AutoLogin_OnCheckedChanged?(Object?sender,?EventArgs?e)?
????????
{
????????????AutoLoginCookie?alCookie?
=?new?AutoLoginCookie();
????????????alCookie.Write(?autoLogin.Checked?);
????????}


????????
//?*********************************************************************
????????
//??LoginButton_Click
????????
//
????????/**////?<summary>
????????
///?Event?handler?to?handle?the?login?button?click?event
????????
///?</summary>

????????//?***********************************************************************/
????????public?void?LoginButton_Click?(Object?sender,?EventArgs?e)?{
????????????User?userToLogin?
=?new?User();
????????????
string?redirectUrl?=?null;

????????????
//?Save?in?cookie?auto?login?user's?preference
????????????
//?only?if?it?wasn't?previously?set?or?the?cookie?value?differs?from
????????????
//?login's?autologin?checkbox?status.
????????????
//
????????????AutoLoginCookie?alCookie?=?new?AutoLoginCookie();
????????????
if?(!alCookie.HasValue?||
????????????????(alCookie.HasValue?
&&?(alCookie.GetValue()?!=?autoLogin.Checked)))?
????????????
{
????????????????alCookie.Write(?autoLogin.Checked?);
????????????}
?

????????????
if?(!Page.IsValid)
????????????????
return;

????????????userToLogin.Username?
=?username.Text;
????????????userToLogin.Password?
=?password.Text;
????????????
????????????LoginUserStatus?loginStatus?
=?Users.ValidUser(userToLogin);
????????????
bool?enableBannedUsersToLogin?=?CSContext.Current.SiteSettings.EnableBannedUsersToLogin;
????????????
????????????
//?Change?to?let?banned?users?in
????????????
//
????????????if?(loginStatus?==?LoginUserStatus.Success?||?
????????????????(enableBannedUsersToLogin?
&&?loginStatus?==?LoginUserStatus.AccountBanned))?{
????????????????
//?Are?we?allowing?login?
????????????????
//?TODO?--?this?could?be?better?optimized
????????????????if?(!CSContext.Current.SiteSettings.AllowLogin?&&?!userToLogin.IsAdministrator)?{
????????????????????
throw?new?CSException(CSExceptionType.UserLoginDisabled);
????????????????}


????????????????????HttpCookie?formsAuthCookie;
????????????????formsAuthCookie?
=?FormsAuthentication.GetAuthCookie(userToLogin.Username,?autoLogin.Checked);
????????????????UserCookie?userCookie?
=?csContext.User.GetUserCookie();
????????????????userCookie.WriteCookie(formsAuthCookie,?
30,?autoLogin.Checked);

????????????????
//?Get?the?link?from?the?context
????????????????if?((CSContext.Current.ReturnUrl?!=?null)?&&?(CSContext.Current.ReturnUrl.Trim()?!=?string.Empty))
????????????????????redirectUrl?
=?CSContext.Current.ReturnUrl;

????????????????
//?If?none,?get?the?stored?redirect?url
????????????????if?((redirectUrl?==?null)?&&?(ReferralLink?!=?null)?&&?(ReferralLink.Trim()?!=?string.Empty))
????????????????????redirectUrl?
=?ReferralLink;

????????????????
//?Check?to?ensure?we?aren't?redirecting?back?to?a?Message?prompt?or?back?to?the?logout?screen
????????????????
//?Or?ChangePassword*,?or?CreateUser*,?or?EmailForgottenPassword*
????????????????
//?Or,?if?no?URL,?use?appPath
????????????????if?(Globals.IsNullorEmpty(redirectUrl)
????????????????????
||?(redirectUrl.IndexOf("MessageID")?!=?-1)
????????????????????
||?(redirectUrl.IndexOf(Globals.GetSiteUrls().Logout)?!=?-1)
????????????????????
||?(redirectUrl.IndexOf("ChangePassword")?!=?-1)
????????????????????
||?(redirectUrl.IndexOf("EmailForgottenPassword")?!=?-1))
????????????????????redirectUrl?
=?Globals.GetSiteUrls().Home;

????????????????LeaveSecureConnection(redirectUrl);
????????????}

????????????
else?if?(loginStatus?==?LoginUserStatus.InvalidCredentials)?{?
????????????????
//?Invalid?Credentials
????????????????Page.Response.Redirect(?Globals.GetSiteUrls().Message(CSExceptionType.UserInvalidCredentials),?true?);
????????????}
?
????????????
else?if?(loginStatus?==?LoginUserStatus.AccountPending)?{?
????????????????
//?Account?not?approved?yet
????????????????Page.Response.Redirect(?Globals.GetSiteUrls().Message(CSExceptionType.UserAccountPending),?true?);
????????????}
?
????????????
else?if?(loginStatus?==?LoginUserStatus.AccountDisapproved)?{?
????????????????
//?Account?disapproved
????????????????Page.Response.Redirect(?Globals.GetSiteUrls().Message(CSExceptionType.UserAccountDisapproved),?true?);
????????????}

????????????
else?if?(loginStatus?==?LoginUserStatus.UnknownError)?{?
????????????????
//?Unknown?error?because?of?miss-syncronization?of?internal?data
????????????????throw?new?CSException(CSExceptionType.UserUnknownLoginError);
????????????}

????????????
//?Reject?banned?users?if?they?are?not?allowed?to
????????????
//?pass?through?login.
????????????
//
????????????else?if?(!enableBannedUsersToLogin?&&?loginStatus?==?LoginUserStatus.AccountBanned)?{?
????????????????
//?Account?banned
????????????????Page.Response.Redirect(?Globals.GetSiteUrls().Message(CSExceptionType.UserAccountBanned),?true?);
????????????}

????????}


????????
private?void?RegisterSetFocusScript()
????????
{
????????????
string?key?=?"LoginOnFocus";
????????????
if(Page.IsStartupScriptRegistered(key))
????????????????
return;

????????????
string?script?=?@"
????????????????????<script?language=""javascript"">
????????????????????<!--
????????????????????????document.forms[0].{0}.focus()
????????????????????-->
????????????????????</script>
";
??????
????????????Page.RegisterStartupScript(
"LoginOnFocus",?string.Format(script,?username.ClientID)?)?;
????????????
????????}

????????
#endregion

????}

}

看了這個我們應該明白了吧,此方法就是我們手動建立相關控件的關聯,使用FindControl("controlname")方法我們就可以找到模板的相應控件所以在定制模板的時候模板里的控件的ID一定要和此處一一對應即可。

??????? 你一定會想,這樣一一對應后每每修改前臺模板內的控件后不是都要到相應的后臺代碼里修改相應的代碼,不錯,是這樣,不過還是有相應的對策來彌補這種不足,那就是在后臺盡量把前臺需要的功能和代碼考慮全,這樣在前臺如果需要去掉某個控件后臺的代碼也不需要改變,這里后臺代碼就應該這樣寫了:

TextBox name = FindControl("username") as TextBox;

if(name != null)

{

??? //處理代碼

}

??????? 這里可以看出,第一句使用了as語句,作用為把找到的對象轉換為TextBox類型,如果沒找到或類型轉換失敗也不引發異常而是將NULL付給變量,這樣我們在代碼里只需要加多判斷引用是否為NULL即可按照我們的想法處理相應邏輯了。

??????? 怕寫太多讓人沒耐心,故分成幾篇來分析,后面將介紹在CS中的模板處理機制。?

轉載于:https://www.cnblogs.com/Dragonpro/archive/2006/04/24/383097.html

總結

以上是生活随笔為你收集整理的Community Server系列之二:页面之间的关系1[介绍]的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。