views:

38

answers:

1

hi

I have one user control to pick the date which utilizes ajax calendar extender. Also it has javascript validation for checking date format. Control is working fine when its visibility is true in loading time. But giving javascript error if i am making it visible with a button (server control) click.

Below is the javascript function to checking the date

txtDate.Attributes.Add("onblur", "javascript:ValidateDate('" & txtDate.ClientID & "');")
objStringBuilder.AppendLine("function ValidateDate(d) ")
objStringBuilder.AppendLine("   {          ")
objStringBuilder.AppendLine("    var t = document.getElementById(d);")
objStringBuilder.AppendLine("    if (t.value.toString() != "")")
objStringBuilder.AppendLine("     {")
objStringBuilder.AppendLine("       if ( isDate(t.value.toString()) == true )")
objStringBuilder.AppendLine("       {")
objStringBuilder.AppendLine("          return true;")
objStringBuilder.AppendLine("       }")
objStringBuilder.AppendLine("       else")
objStringBuilder.AppendLine("       { ")
objStringBuilder.AppendLine("        alert("Please enter valid date in dd/mm/yyyy format");")
objStringBuilder.AppendLine("        t.value = "";")
objStringBuilder.AppendLine("        t.focus();")
objStringBuilder.AppendLine("        return false;")
objStringBuilder.AppendLine("       }")
objStringBuilder.AppendLine("      } ")
objStringBuilder.AppendLine("   } ")

while making visible and onblur it is giving error at txtDate.Attributes.Add("onblur", "javascript:ValidateDate('" & txtDate.ClientID & "');")"

line. The cause i think control's id is not getting passed while it making visible runtime. But working fine if the visibility is true during the loading.

please help

thanks Anto

+1  A: 

You can't use ASP.NET Visible property if you are referencing them in JavaScript. Visible=false means the control doesn't not get rendered to the page, therefore its not in the DOM for reference.

Use CSS:
disply:none or visiblity:hidden to show/hide and your JavaScript will work fine.

DEBUG TIP: Remove the UpdatePanel when things get hairy to see whats going on, then put it back.

rick schott
thanks Rick. I was not trying to use visible property from javascript but the asp:panel's visible property was changing and control is in that panel. When i make asp:panel visible control also appear but the javascript function ValidateDate() not getting the id (client id) of the textbox inside the user control.....
Antoops
I could not solve the Javascript issue. As a work around I created another user control which is using all the server events to validate the dates. :(
Antoops