0

I'm trying to create something almost similar to this [1]: http://weblogs.asp.net/infinitiesloop/archive/2007/09/17/inline-script-inside-an-asp-net-ajax-updatepanel.aspx

The problem I ran into is that if I use an inline tag, the page property ClientID it's resolved as "__Page" although this doesn't happen with child controls of that page. So for example if I use the control like this

  <tag:InlineScript runat="server">
        <script type="text/javascript">
          alert('<%= ClientID %>');
          alert('<%= SomeLabelInTheSamePage.ClientID %>');
        </script>
  </tag:InlineScript>

the page is rendered as:

    <script type="text/javascript">
      alert('__Page');
      alert('MainContent_CorrectLabelId');
    </script>

Any idea why?

[1] the diference is that I'm using a user control (instead of server control) decorated with ParseChildren(false), same code as the page I linked.

4
  • are you sure that you are doing the Binding properly to return the ClientID where in the code behind if necessary are you assigning or declaring the ClientID also is ClientID stored in a Session["ClientID"] within your code..? if you are trying to follow the example .. I don't see where you are registering ScriptManager.RegisterStartupScript(this, typeof(Page), UniqueID, "alert('hi')", true); as in the Url example.. Commented Jan 12, 2012 at 19:02
  • What other scenarios does alert('<%= ClientID %>'); render the correct ID? only in user controls? Commented Jan 12, 2012 at 19:06
  • you have Tag:InLineScript, have you tried to do anything like var tmp = document.getElementsByTagName("yourTagName") Commented Jan 12, 2012 at 19:09
  • 1
    Isn't it just because ClientID on its own is picking up the base ClientID - this.ClientId - i.e. Page.ClientID? And when you call it in a user control, it's picking up this.ClientId - control.ClientId = the controls client id? Commented Jan 12, 2012 at 19:13

1 Answer 1

1

Try this:

<tag:InlineScript id="myInlineScript" runat="server">
    <script type="text/javascript">
        alert('<%=myInlineScript.ClientID %>');
        alert('<%=SomeLabelInTheSamePage.ClientID %>');
    </script>
</tag:InlineScript>
Sign up to request clarification or add additional context in comments.

2 Comments

That should work - just calling ClientID will pull the this.ClientID which will be Page.ClientId, Control.ClientId and so on...
Ha, how did I missed that?. Thanks everyone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.