Tech Off Thread

12 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

ASP.NET control identifiers

Back to Forum: Tech Off
  • User profile image
    W3bbo

    'lo

    I've been having some problems with nested server controls (specifically, a bunch of System.Web.UI.HtmlControls) that keep on using the 'stacking order' to generate their id="" attributes.

    So this:

    <input type="text" runat="server" id="txtUsername" />

    Returns this:

    <input type="text" id="_ctl0:_ctl2:txtUsername" name="_ctl0:_ctl2:txtUsername" value="" size="30" />

    Instead of this:

    <input type="text" id="txtUsername" name="txtUsername" value="" size="30" />

    Is there a way to override this, preferably one that doesn't involve having to create my own Web-control classes?

  • User profile image
    spaceghost

    No, because the point of this scheme is that your ClientIDs are unique, allowing things like postback to work.

    The only "good" thing here is that these ids are fairly predictable. So long as the nesting remains the same so will the ClientIDs.

  • User profile image
    W3bbo

    spaceghost wrote:
    No, because the point of this scheme is that your ClientIDs are unique, allowing things like postback to work.

    The only "good" thing here is that these ids are fairly predictable. So long as the nesting remains the same so will the ClientIDs.


    My problem is that I'm using <label for="#"> which requires that I know the exact identifier of the <input /> element.

    Another issue is that since I'm using LoadControl() and altering the nesting, it isn't consistent nor predictable.

    I could runat="server" all the <label></label> elements though, but that would incurr a performance hit.

  • User profile image
    Shiv

    are u using vs 2003 ? if so i think that vs changes it automatically . why can't u use vs 2005 . it keeps any changes made in html/aspx code intact .

  • User profile image
    W3bbo

    Shiv wrote:

    are u using vs 2003 ? if so i think that vs changes it automatically . why can't u use vs 2005 . it keeps any changes made in html/aspx code intact .



    This is an ASP.NET thing, nothing to do with the IDE.

    I don't touch "Design view" with a 10-foot barge pole anyway.

  • User profile image
    lcriscola

    What about using <label for "#<%#txtUsername.ClientID%>" /> and then this.Databind() after the  LoadControl()
    Luciano.

  • User profile image
    W3bbo

    lcriscola wrote:
    What about using <label for "#<%#txtUsername.ClientID%>" /> and then this.Databind() after the  LoadControl()
    Luciano.


    Hmm, that's a thought.

    So I'd do it like this?

    -------------

    <dl>
    <dt><label for="<%# txtUsername.ClientID %>">Username</label></dt>
    <dd><input type="text" id="txtUsername" runat="server" /></dd>
    </dl>

    ------------

    And in my inherited class:

    CommandAddForm CAF = LoadControl("PathToControl");
    CAF.Databind();

    right?

  • User profile image
    lcriscola

    Not CAF.Databind(); but this.Databind(); <%#%> is a Databind expression an it is evaluated at the Page Databinding event.

    Luciano.

  • User profile image
    footballism

    If you don't wanna call the redundant Method Page.DataBind(), then use this instead:
            <p>
                <label for="<%=txtUserName.ClientID%>">UserName:</label>
                <input id="txtUserName" type="text" runat="server"/>
            </p>

    Sheva

  • User profile image
    W3bbo

    footballism wrote:
    If you don't wanna call the redundant Method Page.DataBind(), then use this instead:
            <p>
                <label for="<%=txtUserName.ClientID%>">UserName:</label>
                <input id="txtUserName" type="text" runat="server"/>
            </p>

    Sheva


    Ah yes, thanks a lot footballism Smiley

  • User profile image
    W3bbo

    There's a problem.

    When I use this ASP.NET:


    <X:XForm runat="server">
        <asp:Literal ID="litUser" Runat="server" />
        <div><asp:Literal ID="InputViewstate" Runat="server" /></div>
        <dl>
            <asp:PlaceHolder ID="plcLogin" Runat="server">
            <dt><label for="<%= txtUsername.ClientID %>">Username</label></dt>
            <dd><input type="text" id="txtUsername" runat="server" size="30"  />
                <asp:RequiredFieldValidator Runat="server"
                ControlToValidate="txtUsername" ErrorMessage="Please provide your username" display="Dynamic" /></dd>
           
            <dt><label for="<%= pwdPassword.ClientID %>">Password</label></dt>
            <dd><input type="password" id="pwdPassword" runat="server" size="30" />
                <asp:RequiredFieldValidator Runat="server"
                ControlToValidate="pwdPassword" ErrorMessage="Please provide your password" display="Dynamic" /></dd>
           
            </asp:PlaceHolder>
            <dt><label for="<%= txaComment.ClientID %>">Your Comment</label></dt>
            <dd><textarea runat="server" id="txaComment" rows="8" cols="40"></textarea>
                <asp:RequiredFieldValidator Runat="server"
                ControlToValidate="txaComment" ErrorMessage="Please provide a comment" display="Dynamic" /></dd>
        </dl>
       
        <div>
            <button type="submit">Post Comment</button>
        </div>
       
    </X:XForm>

    This gets generated.


    <form id="__aspnetForm" method="post" action="/AMS/Redirector.aspx?404;http://proj:80/AMS/features/mailbag/2005-11-12"><input type="hidden" name="__VIEWSTATE" value="dDw1MzgxO3Q8O2w8aTwwPjs+O2w8dDw7bDxpPDE+Oz47bDx0PHA8bDxUZXh0Oz47bDxOb3ZlbWJlciAxMiwgMjAwMjs+Pjs7Pjs+Pjs+Pjs+" />
        <dt><label for="_ctl0__ctl2_txtUsername">Username</label></dt>
        <dd><input name="_ctl0:_ctl2:txtUsername" id="_ctl0__ctl2_txtUsername" type="text" size="30" /></dd>

                           
        <dt><label for="_ctl0__ctl2_pwdPassword">Password</label></dt>
        <dd><input name="_ctl0:_ctl2:pwdPassword" id="_ctl0__ctl2_pwdPassword" type="password" size="30" /></dd>
                           
        <textarea name="_ctl0:_ctl2:txaComment" id="_ctl0__ctl2_txaComment" rows="8" cols="40"></textarea></form>

    Any idea why it removes so much static code?

  • User profile image
    W3bbo

    Just to append to the above post, but before I added the <%= %> bits, it worked perfectly, without any of the static XHTML missing.

    Just to make sure it wasn't the Placeholder messing things up, I removed it.

    So now the input ASP.NET is this:

    ---------------
    <X:XForm runat="server">
        <asp:Literal ID="litUser" Runat="server" />
        <div><asp:Literal ID="InputViewstate" Runat="server" /></div>
        <dl>
            <dt><label for="<%= txtUsername.ClientID %>">Username</label></dt>
            <dd><input type="text" id="txtUsername" runat="server" size="30"  />
                <asp:RequiredFieldValidator Runat="server"
                ControlToValidate="txtUsername" ErrorMessage="Please provide your username" display="Dynamic" /></dd>
            <dt><label for="<%= pwdPassword.ClientID %>">Password</label></dt>
            <dd><input type="password" id="pwdPassword" runat="server" size="30" />
                <asp:RequiredFieldValidator Runat="server"
                ControlToValidate="pwdPassword" ErrorMessage="Please provide your password" display="Dynamic" /></dd>
            <dt><label for="<%= txaComment.ClientID %>">Your Comment</label></dt>
            <dd><textarea runat="server" id="txaComment" rows="8" cols="40"></textarea>
            <asp:RequiredFieldValidator Runat="server"
                ControlToValidate="txaComment" ErrorMessage="Please provide a comment" display="Dynamic" /></dd>
        </dl>
        <div>
            <button type="submit">Post Comment</button>
        </div>
    </X:XForm>
    ---------------

    And the generated output is this:

    ---------------
                <form id="__aspnetForm" method="post" action="/AMS/Redirector.aspx?404;http://proj:80/AMS/features/mailbag/2005-11-12"><input type="hidden" name="__VIEWSTATE" value="dDw1MzgxO3Q8O2w8aTwwPjs+O2w8dDw7bDxpPDE+Oz47bDx0PHA8bDxUZXh0Oz47bDxOb3ZlbWJlciAxMiwgMjAwMjs+Pjs7Pjs+Pjs+Pjs+" /><input name="_ctl0:_ctl2:txtUsername" id="_ctl0__ctl2_txtUsername" type="text" size="30" /><input name="_ctl0:_ctl2:pwdPassword" id="_ctl0__ctl2_pwdPassword" type="password" size="30" /><textarea name="_ctl0:_ctl2:txaComment" id="_ctl0__ctl2_txaComment" rows="8" cols="40"></textarea></form>
       
    ---------------

    ...why the heck is it removing it all?

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.