1

I'm using the ASP.Net LoginControl for user validation:

   <asp:Login ID="Login1" runat="server">
        <LayoutTemplate>
            <asp:TextBox id="UserName" runat="server" class="inD"></asp:TextBox>
            <asp:TextBoxWatermarkExtender ID="UserNameWatermarkExtender" runat="server" TargetControlID="UserName" WatermarkText="Username" WatermarkCssClass="weD inD"/>
            <asp:TextBox id="Password" runat="server" class="inD" textMode="Password" onfocus="passwordFocus()" onblur="passwordBlur()"></asp:TextBox>
            <asp:TextBoxWatermarkExtender ID="PasswordWatermarkExtender" runat="server" TargetControlID="Password" WatermarkText="Password" WatermarkCssClass="weD inD"/>
            <asp:button id="Login" CommandName="Login" runat="server" Text="Login" class="btL" BorderStyle="None"></asp:button>
            <br />
            <asp:Checkbox id="RememberMe" runat="server" Text="Remember me"></asp:Checkbox>
            <asp:requiredfieldvalidator id="UserNameRequired" runat="server" ControlToValidate="UserName" Text="*"></asp:requiredfieldvalidator>
            <asp:requiredfieldvalidator id="PasswordRequired" runat="server" ControlToValidate="Password" Text="*"></asp:requiredfieldvalidator>
            <asp:Literal id="FailureText" runat="server"></asp:Literal>
        </LayoutTemplate>
    </asp:Login>

Now I would like to access the password box (ID: Password) through JavaScript. I tried

document.getElementById('Password');

but this doesn't work. Thanks for any advice,

Alexander

1 Answer 1

1

ASP.NET (depending on which version of the framework you're on) likes to mangle client ids. I've gotten around this in the past like this:

<script type="text/javascript">
var passwordId = '<%= Password.ClientID %>';
</script>

Seems kind of dirty, but it works!

Edit:

I see that you are in a LoginForm control. Try this instead:

<script type="text/javascript">
var passwordId = '<%= ((TextBox)LoginForm.FindControl("Password")).ClientID %>';
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply. Unfortunately I'm getting a compiler error, saying, that "Password is not declared". The code looks like this: inD1 = document.getElementById('<%= Password.ClientID %>'); Have you got an idea, of what goes wrong? Thank you, Alexander

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.