📄 extendercontrolbase.cs
字号:
void Page_PreLoad(object sender, EventArgs e)
{
// Needs to happen sometime after ASP.NET populates the control
// values from the postback but sometime before Load so that
// the values will be available to controls then. PreLoad is
// therefore the obvious choice.
LoadClientStateValues();
}
/// <summary>
/// Override OnLoad to call LoadClientStateValues if it hasn't already been called
/// </summary>
/// <remarks>
/// This can be the case if an extender is within a template - the page lifecycle
/// catch-up seems to take place after Page.PreLoad has already fired
/// </remarks>
/// <param name="e">event args</param>
protected override void OnLoad(EventArgs e)
{
if (!_loadedClientStateValues)
{
LoadClientStateValues();
}
base.OnLoad(e);
ScriptObjectBuilder.RegisterCssReferences(this);
}
/// <summary>
/// This creates the field for any client state and sets up
/// it's name.
/// </summary>
/// <returns></returns>
private HiddenField CreateClientStateField()
{
// add a hidden field so we'll pick up the value
//
HiddenField field = new HiddenField();
field.ID = GetClientStateFieldID();
Controls.Add(field);
ClientStateFieldID = field.ID;
return field;
}
/// <summary>
/// Loads the values for each of the TargetProperties classes coming back from a postback.
/// </summary>
private void LoadClientStateValues()
{
if (EnableClientState && !string.IsNullOrEmpty(ClientStateFieldID))
{
HiddenField hiddenField = (HiddenField)NamingContainer.FindControl(ClientStateFieldID);
if ((hiddenField != null) && !string.IsNullOrEmpty(hiddenField.Value))
{
ClientState = hiddenField.Value;
}
}
if (null != ClientStateValuesLoaded)
{
ClientStateValuesLoaded(this, EventArgs.Empty);
}
_loadedClientStateValues = true;
}
/// <summary>
/// Fired when the ClientState values have been loaded from the page
/// </summary>
protected event EventHandler ClientStateValuesLoaded;
protected override void Render(HtmlTextWriter writer)
{
// Use ASP.NET's mechanism for ensuring Controls are within a form
// (since unexpected behavior can occur when ExtenderControls are not)
if (null != Page)
{
Page.VerifyRenderingInServerForm(this);
}
base.Render(writer);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (Enabled && TargetControl.Visible)
{
SaveClientStateValues();
}
}
/// <summary>
/// Fired when the extender can not locate it's target control. This may happen if the target control is in a different naming container.
/// By handling this event, user code can locate the target and return it via the ResolveControlEventArgs.Control property.
/// </summary>
/// <param name="e"></param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
protected virtual void OnResolveControlID(ResolveControlEventArgs e)
{
if (ResolveControlID != null)
{
ResolveControlID(this, e);
}
}
/// <summary>
/// Allows generation of markup within the behavior declaration in XML script
/// </summary>
protected virtual void RenderInnerScript(ScriptBehaviorDescriptor descriptor)
{
}
/// <summary>
/// Walks each of the properties in the TargetProperties object and renders script for them.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Child classes may want to use ScriptBehaviorDescriptor")]
protected virtual void RenderScriptAttributes(ScriptBehaviorDescriptor descriptor)
{
try
{
_renderingScript = true;
ScriptObjectBuilder.DescribeComponent(this, descriptor, this, this);
}
finally
{
_renderingScript = false;
}
}
protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors(Control targetControl)
{
if (!Enabled || !TargetControl.Visible)
return null;
EnsureValid();
ScriptBehaviorDescriptor descriptor =
new ScriptBehaviorDescriptor(ClientControlType, targetControl.ClientID);
// render the attributes for this element
//
RenderScriptAttributes(descriptor);
// render profile bindings
//
//RenderProfileBindings(descriptor);
// render any child scripts we need to.
//
RenderInnerScript(descriptor);
return new List<ScriptDescriptor>(new ScriptDescriptor[] { descriptor });
}
protected override IEnumerable<ScriptReference> GetScriptReferences()
{
if (Enabled) {
return EnsureScripts();
}
return null;
}
#if false
/* TODO: Migrate */
private static string[] NoSerializeProps = new string[] { "TargetControlID", "ProfileBindings" };
/// <summary>
/// Renders the script for a given target control
/// </summary>
/// <param name="writer">The text writer</param>
/// <param name="targetControl">The control being extended</param>
private void RenderProfileBindings()
{
string profileComponent = null;
bool startElementWritten = false;
foreach (ProfilePropertyBinding profileItem in ProfileBindings)
{
if (profileComponent == null)
{
IScriptService profileService = EnsureProfileScriptService(ScriptManager.GetCurrent(Page), true);
Sys.Debug.assert(profileService != null, "Failed to get ProfileScriptService");
profileComponent = profileService.ID;
}
if (!startElementWritten)
{
startElementWritten = true;
writer.WriteStartElement("bindings");
}
writer.WriteStartElement("binding");
Sys.Debug.assert(!string.IsNullOrEmpty(profileItem.ProfilePropertyName), "Property Name is null or empty");
writer.WriteAttributeString("dataPath", profileItem.ProfilePropertyName);
writer.WriteAttributeString("dataContext", profileComponent);
string propertyName = profileItem.ExtenderPropertyName;
Sys.Debug.assert(!string.IsNullOrEmpty(propertyName), "Property Name is null or empty");
if (propertyName != null)
{
PropertyDescriptor pd = TypeDescriptor.GetProperties(props)[propertyName];
if (pd == null)
{
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "Can't find property '{0}' to bind to. Check the value of ExtenderPropertyName.", propertyName));
}
ClientPropertyNameAttribute displayName = (ClientPropertyNameAttribute)pd.Attributes[typeof(ClientPropertyNameAttribute)];
if (displayName != null && !displayName.IsDefaultAttribute())
{
propertyName = displayName.PropertyName;
}
writer.WriteAttributeString("property", propertyName);
writer.WriteAttributeString("direction", "InOut");
}
writer.WriteEndElement();
}
if (startElementWritten)
{
writer.WriteEndElement();
}
}
internal void RenderValidationScript(Microsoft.Web.Script.ScriptTextWriter writer)
{
if (!SuppressValidationScript && !_validationScriptRendered)
{
_validationScriptRendered = true;
writer.WriteStartElement(Constants.BuiltinScriptPrefix + ":" + Constants.ValidationScriptTagName);
if (ScriptNamespacePrefix != null)
{
writer.WriteAttributeString("Prefix", ScriptNamespacePrefix);
}
writer.WriteAttributeString("TagName", ClientControlType);
writer.WriteEndElement();
}
}
#endif
/// <summary>
/// Save any values in the TargetProperties objects out to client state so they are available
/// on the client side.
/// </summary>
private void SaveClientStateValues()
{
if (EnableClientState)
{
HiddenField hiddenField = null;
// if we don't have a value here, this properties
// object may have been created dynamically in code
// so we create the field on demand.
//
if (string.IsNullOrEmpty(ClientStateFieldID))
{
hiddenField = CreateClientStateField();
}
else
{
hiddenField = (HiddenField)NamingContainer.FindControl(ClientStateFieldID);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -