Getting ClientID of textbox in CreateUser Wizard using jquery

4.4k views Asked by At

I want to check the username availbilty so I have this code:

 <script type = "text/javascript">
        function ShowAvailability() {
            $.ajax({
                type: "POST",
                url: "Default.aspx/CheckUserName",
                data: '{userName: "' + $("#<%=UserName.ClientID%>")[0].value + '" }',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                failure: function (response) {
                    alert(response);
                }
            });
        }
        function OnSuccess(response) {
            var mesg = $("#mesg")[0];

            switch (response.d) {
                case "true":
                    mesg.style.color = "green";
                    mesg.innerHTML = "Available";
                    break;
                case "false":
                    mesg.style.color = "red";
                    mesg.innerHTML = "Not Available";
                    break;
                case "error":
                    mesg.style.color = "red";
                    mesg.innerHTML = "Error occured";
                    break;
            }
        }
        function OnChange(txt) {
            $("#mesg")[0].innerHTML = "";
        }
    </script> 

In general we write this way to get the required clientID

$("#<%=UserName.ClientID%>")

Now I need to get the Username textbox Client ID as I am using create user wizard. How do I do in this case?

I have tried this but I'm getting error as 'TextBox' is a type and cannot be used as an expression.

   var UserName = '<%= ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("UserName")).ClientID %>';
3

There are 3 answers

3
ShankarSangoli On BEST ANSWER

Try this

var UserName = '<%= ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("UserName")).ClientID %>';
1
DotNetUser On

Try this

    var UserName=document.getElementById('<%=((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("UserName")).ClientID %>')
1
dinotom On

Set the ClientIdMode to static and reference your control's I'd exactly as you set it For example;

<asp:DropDownList Id="dropdown" ClientIdMode="Static" runat="server" />

Use $('#dropdown') to access it