?? webcontrol.cs
字號:
using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Configuration;
using System.Reflection;
namespace ComponentArt.Web.UI
{
/// <summary>
/// ComponentArt base WebControl class. Provides licensing, client-script rendering, and search engine stamp rendering services. All ComponentArt server controls inherit from this class.
/// </summary>
public abstract class WebControl : System.Web.UI.WebControls.WebControl
{
#region Public Properties
/// <summary>
/// ID of the client-side object corresponding to this control.
/// </summary>
/// <value>The name of the JavaScript global variable representing this control's client-side object.</value>
/// <remarks>ClientObjectId is often the same as ClientID. However this cannot always be the case,
/// because not all ClientID values are valid JavaScript variable names.</remarks>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
[Description("ID of the client-side object corresponding to this control.")]
public string ClientObjectId
{
get
{
return this.GetSaneId();
}
}
/// <summary>
/// Relative or absolute path to the folder containing the client-side script file(s).
/// </summary>
/// <remarks>
/// The actual JS files are placed in a folder named to correspond to the version of Web.UI being used, inside a folder named
/// <b>componentart_webui_client</b>, which is placed in the folder specified by this property.
/// </remarks>
[Category("Support")]
[DefaultValue("")]
[Description("Relative or absolute path to the folder containing the client-side script file(s).")]
public string ClientScriptLocation
{
get
{
object o = ViewState["ClientScriptLocation"];
return (o == null) ? String.Empty : Utils.ConvertUrl(Context, string.Empty, (string)o);
}
set
{
ViewState["ClientScriptLocation"] = value;
}
}
/// <summary>
/// Specifies the level of client-side content that the control renders.
/// </summary>
/// <value>
/// Gets or sets a value that allows you to override automatic detection of browser capabilities and to specify
/// how the control renders. Default is Auto, indicating that the control will decide based on the client's
/// browser whether it is appropriate to serve it uplevel or downlevel content.
/// </value>
[DefaultValue(ClientTargetLevel.Auto)]
[Description("Specifies the level of client-side content that the control renders.")]
public ClientTargetLevel ClientTarget
{
get
{
return Utils.ParseClientTargetLevel(ViewState["ClientTarget"], ClientTargetLevel.Auto);
}
set
{
ViewState["ClientTarget"] = value;
}
}
/// <summary>
/// Whether this control considers the current browser down-level.
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
public bool IsDownLevelBrowser
{
get
{
if(Context != null)
{
return this.IsDownLevel();
}
else
return false;
}
}
/// <summary>
/// Whether to render the search engine stamp.
/// </summary>
[Category("Support")]
[DefaultValue(true)]
[Description("Whether to render the search engine stamp.")]
public bool RenderSearchEngineStamp
{
get
{
object o = ViewState["RenderSearchEngineStamp"];
return (o == null) ? true : (bool) o;
}
set
{
ViewState["RenderSearchEngineStamp"] = value;
}
}
#endregion
#region Public Methods
/// <summary>
/// IsLicensed method.
/// </summary>
/// <returns>Whether this control is licensed.</returns>
public virtual bool IsLicensed()
{
try
{
License = LicenseManager.Validate(this.GetType(), this);
return true;
}
catch
{
return false;
}
}
#endregion
#region Protected Properties
protected System.ComponentModel.License License = null;
#endregion
#region Protected Methods
protected string GetSaneId()
{
if(this.ClientID == null || this.ClientID == "")
{
throw new Exception("An ID must be defined on the control.");
}
else
{
return this.ClientID.Replace("$", "_").Replace("{", "_").Replace("}", "_");
}
}
protected bool IsBrowserSearchEngine()
{
if(Context == null || Context.Request == null)
return false;
string sUserAgent = Context.Request.UserAgent;
if(sUserAgent == null)
{
return false;
}
return (
sUserAgent.IndexOf("Googlebot") >= 0 ||
sUserAgent.IndexOf("SideWinder") >= 0 ||
sUserAgent.IndexOf("inktomi") >= 0 ||
sUserAgent.IndexOf("ZyBorg") >= 0 ||
sUserAgent.IndexOf("FAST-WebCrawler") >= 0 ||
sUserAgent.IndexOf("Lycos") >= 0 ||
sUserAgent.IndexOf("Scooter") >= 0 ||
sUserAgent.IndexOf("NPBot") >= 0 ||
sUserAgent.IndexOf("Gigabot") >= 0 ||
sUserAgent.IndexOf("MSNBOT") >= 0 ||
sUserAgent.IndexOf("SearchSpider") >= 0 ||
sUserAgent.IndexOf("Vagabondo") >= 0 ||
sUserAgent.IndexOf("almaden") >= 0 ||
sUserAgent.IndexOf("CipinetBot") >= 0 ||
sUserAgent.IndexOf("QuepasaCreep") >= 0 ||
sUserAgent.IndexOf("Gaisbot") >= 0 ||
sUserAgent.IndexOf("DoCoMo") >= 0 ||
sUserAgent.IndexOf("grub-client") >= 0 ||
sUserAgent.IndexOf("Openbot") >= 0 ||
sUserAgent.IndexOf("Ask Jeeves") >= 0 ||
sUserAgent.IndexOf("Girafabot") >= 0
);
}
// Whether the control is running in design mode
protected bool IsRunningInDesignMode()
{
return HttpContext.Current == null;
}
protected abstract bool IsDownLevel();
protected virtual void ComponentArtPreRender(EventArgs e)
{
}
protected abstract void ComponentArtRender(HtmlTextWriter output);
protected string GetResourceContent(string sFileName)
{
try
{
Stream oStream = Assembly.GetAssembly(this.GetType()).GetManifestResourceStream(sFileName);
StreamReader oReader = new StreamReader(oStream);
return oReader.ReadToEnd();
}
catch(Exception ex)
{
throw new Exception("Could not read resource \"" + sFileName + "\": " + ex);
}
}
protected void RenderCrawlerStamp(HtmlTextWriter output)
{
Assembly thisAssembly = Assembly.GetExecutingAssembly();
string thisAssemblyVersion = thisAssembly.GetName().Version.ToString();
string thisProductName = this.GetType().ToString().Replace("ComponentArt.Web.UI.", "");
output.RenderBeginTag(HtmlTextWriterTag.Noscript);
output.Write("ComponentArtSCStamp " + thisProductName + " " + thisAssemblyVersion);
output.RenderEndTag(); // </noscript>
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (IsLicensed()) ComponentArtPreRender(e);
}
protected override void Render(HtmlTextWriter output)
{
// Render demo warning if required
//if (!IsLicensed())
//{
// RenderRedistributableWarning(output);
// return;
//}
// Render control
ComponentArtRender(output);
if(this.IsBrowserSearchEngine() && this.RenderSearchEngineStamp)
{
RenderCrawlerStamp(output);
}
}
protected void RenderRedistributableWarning(HtmlTextWriter output)
{
output.Write("<div style=\"background-color:#3F3F3F;border:1px;border-style:solid;border-bottom-color:black;border-right-color:black;border-left-color:lightslategray;border-top-color:lightslategray;color:cornsilk;padding:2px;font-family:verdana;font-size:11px;\">");
string productName = this.GetType().ToString().Replace("ComponentArt.Web.UI.", "");
output.Write("<b>ComponentArt " + productName + "</b> :: ");
output.Write("Unlicensed version. <br><br>");
output.Write("This version is licensed for single application use only. <br><br>");
output.Write("You can download the free trial version <a href=http://www.ComponentArt.com/ target=_blank><font color=silver>here</font></a>.");
output.Write("</div>");
}
protected void WriteGlobalClientScript(HtmlTextWriter output, string sDefaultPath, string sScriptFile)
{
string sScript = GenerateClientScriptBlock(sDefaultPath, sScriptFile);
string sInstanceId = sScriptFile;
output.Write(sScript);
}
#endregion
#region Private Members
internal bool IsInUpdatePanel()
{
for (Control oControl = this.Parent; oControl != null; oControl = oControl.Parent)
{
if (oControl.GetType().FullName == "Microsoft.Web.UI.UpdatePanel")
{
return true;
}
}
return false;
}
private string GenerateClientScriptBlock(string sDefaultPath, string sScriptFile)
{
string sScript = string.Empty;
string sScriptLocation = string.Empty;
if(this.ClientScriptLocation != string.Empty)
{
string sVersionString =
Assembly.GetExecutingAssembly().GetName().Version.Major.ToString() + "_" +
Assembly.GetExecutingAssembly().GetName().Version.Minor.ToString() + "_" +
Assembly.GetExecutingAssembly().GetName().Version.Build.ToString();
sScriptLocation = Path.Combine(Path.Combine(this.ClientScriptLocation, sVersionString), sScriptFile).Replace("\\", "/");
}
if(sScriptLocation != string.Empty)
{
// Do we have a tilde?
if(sScriptLocation.StartsWith("~") && Context != null && Context.Request != null)
{
string sAppPath = Context.Request.ApplicationPath;
if(sAppPath.EndsWith("/"))
{
sAppPath = sAppPath.Substring(0, sAppPath.Length - 1);
}
sScriptLocation = sScriptLocation.Replace("~", sAppPath);
}
if(File.Exists(Context.Server.MapPath(sScriptLocation)))
{
sScript = "<script src=\"" + sScriptLocation + "\" type=\"text/javascript\"></script>";
}
else
{
throw new Exception(sScriptLocation + " not found");
}
}
else
{
// If everything failed, emit our internal script
string sResourceUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), sDefaultPath + "." + sScriptFile);
sResourceUrl = sResourceUrl.Replace("&", "&");
sScript = "<script src=\"" + sResourceUrl + "\" type=\"text/javascript\"></script>";
}
return sScript;
}
#endregion
}
/// <summary>
/// Specifies the level of client-side content that the control renders.
/// </summary>
public enum ClientTargetLevel
{
/// <summary>
/// Automatically detect whether the browser should be served uplevel or downlevel content.
/// </summary>
Auto,
/// <summary>
/// Serve downlevel content. This is typically appropriate for older and less advanced browsers.
/// </summary>
Downlevel,
/// <summary>
/// Serve uplevel content. This is typically appropriate for newer and more advanced browsers.
/// </summary>
Uplevel
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -