开发者

UpdatePanel does not work with Code Blocks

I have a reference to a control (rcbModels) using a codeblock like this

  function pageLoad() {

      models = $find("<%= rcbModels.ClientID %>");
  }

I added an UpdatePanel to this page but I always get the following error: "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)."

I have tried changing the codeblock to:

      function pageLoad() {

      models = $find("<%# rcbModels.ClientID %>");
  }

And DataBinding on the Pre_RenderComplete event without success.

What's the bes开发者_如何转开发t way to solve this issue ?


Looking at http://www.telerik.com/community/forums/aspnet/editor/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blocks-i-e-lt-gt.aspx

it seems that this could be caused by the runat="server" in the head tag. moving the javascript code into the body but still outside the update panel may remedy this.


For simple cases (trivial page, container chain to rcbModels never changing, code quality not a concern), hardcoding rcbModels.ClientId into the script block saves time and gets the job done.

function pageLoad()
{
    models = $find("panelX_containerY_rcbModels");
}

In other cases, generate a page-wide, client-side dictionary of controls ids mapping to client ids.

protected void Page_PreRender(object sender, EventArgs e)
{
    ScriptManager.RegisterClientScriptBlock(this, typeof(YourPageClass),
        "__BehaviorIds", String.Format(CultureInfo.InvariantCulture, @"
            var __BehaviorIds = {{
                rcbModels: '{0}',
                anotherControl: '{1}',
                yetAnotherControl: '{2}'
            }};",
        rcbModels.ClientID,
        anotherControl.ClientID,
        yetAnotherControl.ClientID), true);
}

Then you can access the behavior ids by name on the client side.

function pageLoad()
{
    models = $find(__BehaviorIds.rcbModels);
    anotherControl = $find(__BehaviorIds.anotherControl);
    yetAnotherControl = $find(__BehaviorIds.yetAnotherControl);
}


Thanks for your response. What I ended up doing was changing the "=" for a "#" and adding a DataBind() clause in the code behind and that solved it.

function pageLoad() {   

  models = $find("<%# rcbModels.ClientID %>");   
 }   




protected void Page_Load(object sender, EventArgs e)   
{      
     Page.Header.DataBind();       
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜