0

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 %>';
coder
  • 13,002
  • 31
  • 112
  • 214

3 Answers3

2

Try this

var UserName = '<%= ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("UserName")).ClientID %>';
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
1

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

dinotom
  • 4,990
  • 16
  • 71
  • 139
0

Try this

    var UserName=document.getElementById('<%=((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("UserName")).ClientID %>')
DotNetUser
  • 6,494
  • 1
  • 25
  • 27
  • Try this as well-var UserName= $('#<%=((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("UserName")).ClientID %>'); – DotNetUser Jan 17 '12 at 15:08