locked
ClientID is never right RRS feed

  • Question

  • User1507783221 posted

    using VS2010, targeting 4.0 framework and I have a number of user controls ...the user control definition follows

    This works 

    var selDescriptionTextarea = 'textarea#<%=txtReportDescr.ClientID + "_txtArea"%>'; //<<-- I had to know the html id and append it

    but is "hacky" and likely to cause someone in the future hours of time.  How do i get the control to return its true ID?

    The user control definition:

    <%@ Control Language="C#" ... %>
    <div class="multi-line-edit" style="height:58px;width:408px;">
    <div class="north top-bottom"></div>
        <div class="south top-bottom"></div>
        <div class="east side"></div>
        <div class="west side"></div>
        <div class="nw corner"></div>
        <div class="ne corner"></div>
        <div class="sw corner"></div>
        <div class="se corner"></div>
        <div class="body">
            <textarea ID="txtArea" runat="server" rows="1" MaxLength="1000" style="resize:none;" ></textarea>
        </div>
    </div>

    and renders as:

    <div style="height:58px;width:408px;" class="multi-line-edit">
    <div class="north top-bottom"></div>
    <div class="south top-bottom"></div>
    <div class="east side"></div>
    <div class="west side"></div>
    <div class="nw corner"></div>
    <div class="ne corner"></div>
    <div class="sw corner"></div>
    <div class="se corner"></div>
    <div class="body">
    <textarea style="resize:none;" maxlength="200" rows="1" id="ctl00_Content_txtReportDescr_txtArea" name="ctl00$Content$txtReportDescr$txtArea"></textarea>
    </div>
    </div>
    Friday, October 21, 2011 9:50 AM

Answers

  • User3866881 posted

    Hello:)

    If you put a control into another control (something like put a TextBox in a Panel, content……,ect). You should know it will generate the name like this:

    ctl00_Your Parent1's Id_Your Parent2's Id_……_Your ParentN's Id_Your Control's Id

    So,

    <% = txtReportDescr.ClientID %> renders as ctl00_Content_txtReportDescr to the page / in the browser

    You put the txtReportDescr in the Content, so it generates——ctl00_Content_txtReportDescr

    However——

    <textarea id="ctl00_Content_txtReportDescr_txtArea" name="ctl00$Content$txtReportDescr$txtArea"></textarea>

    Because you've put the textarea into txtReportDescr, so it renders its ClientId as ctl00_Content_txtReportDescr_txtArea.

    That'd be right.

    Thx again

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 22, 2011 9:37 PM

All replies

  • User757060486 posted

    Let me get this right you want to bind the user control client id in codebehind.

    Try this:

    var selDescriptionTextarea = txtReportDescr.ClientID + "_txtArea";

    Binding expressions belong in the html file not codebehind. In codebehind you can actually call the property of the control itself.

    Friday, October 21, 2011 2:39 PM
  • User1507783221 posted

    Let me get this right you want to bind the user control client id in codebehind.

    Try this:

    var selDescriptionTextarea = txtReportDescr.ClientID + "_txtArea";

    Binding expressions belong in the html file not codebehind. In codebehind you can actually call the property of the control itself.

    NO.  Your misunderstanding me. That's javascript's var, not the .NET code.

    That binding express is in the *.aspx page populating a javascipt variable.  My page works fine.  

    My question/issue is with the string that is returned by txtReportDescr.ClientID (the UserControl) once the page is rendered to the browser.  

    The user control should behave as a black box: calling txtReportDescr.ClientID should "Get[s] the control ID for HTML markup that is generated by ASP.NET" per the docs. Instead I had to append "_txtArea" to that call.  "_txtArea" is the id of the html textarea element in the user control (see my original post)

    I.E.

    <% = txtReportDescr.ClientID %> renders as ctl00_Content_txtReportDescr to the page / in the browser

    while the user control ends up rendering (in the browser) as <textarea id="ctl00_Content_txtReportDescr_txtArea" name="ctl00$Content$txtReportDescr$txtArea"></textarea>

    Friday, October 21, 2011 4:29 PM
  • User757060486 posted

    Oh sorry. That is quite interesting can't say I have ever seen a problem like this. Just out of curiousity try using multiline textbox. If it still gives same problem please post.

    Saturday, October 22, 2011 1:40 AM
  • User3866881 posted

    Hello:)

    If you put a control into another control (something like put a TextBox in a Panel, content……,ect). You should know it will generate the name like this:

    ctl00_Your Parent1's Id_Your Parent2's Id_……_Your ParentN's Id_Your Control's Id

    So,

    <% = txtReportDescr.ClientID %> renders as ctl00_Content_txtReportDescr to the page / in the browser

    You put the txtReportDescr in the Content, so it generates——ctl00_Content_txtReportDescr

    However——

    <textarea id="ctl00_Content_txtReportDescr_txtArea" name="ctl00$Content$txtReportDescr$txtArea"></textarea>

    Because you've put the textarea into txtReportDescr, so it renders its ClientId as ctl00_Content_txtReportDescr_txtArea.

    That'd be right.

    Thx again

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 22, 2011 9:37 PM