Pergunta

I'm creating a server control, which has a property that I am adding as an expando attribute. It works on the initial page load, but after a partial postback, it stops working. My expando attribute is gone.

The class is a class derived from a telerik RadButton.

This is what I am doing in the overridden PreRender method:

Dim radContextMenu As RadContextMenu = Parent.FindControl(ContextMenuID)
If radContextMenu IsNot Nothing Then
  'register the name of our context menu so we can access it from javascript
  ScriptManager.RegisterExpandoAttribute(Me, Me.ClientID, "ContextMenuClientID", radContextMenu.ClientID, False)
End If

As you can see, I'm using ScriptManager, which is supposed to work in partial postback senarios (and it has for other projects of mine) but RegisterExpandoAttribute doesn't seem to work here. Also, I have stepped through the code as well and determined that radContextMenu is indeed found by FindControl.

Any insight as to what I'm doing wrong would be greatly appriciated. Thank you

Foi útil?

Solução

Resolved similar issue by adding check for page's ScriptManager.IsInAsyncPostBack property. When it is set to false you should use <control>.Page.ClientScript.RegisterExpandoAttribute method.

I ended up with new extension method for Control containing this logic:

public static void RegisterExpandoAttribute(this Control control, HtmlTextWriter writer, string attributeName, string attributeValue, bool encode = false)
{
    var scriptManager = ScriptManager.GetCurrent(control.Page);
    if (scriptManager != null && scriptManager.IsInAsyncPostBack)
        ScriptManager.RegisterExpandoAttribute(control, control.ClientID, attributeName, attributeValue, encode);
    else
        control.Page.ClientScript.RegisterExpandoAttribute(control.ClientID, attributeName, attributeValue, encode);
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top