1
votes

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
One quick observation: remove the 'javascript:' from your first line. onblur indicates javascript, so the value of the attribute should simply start with the function call. - KP.
i tried that also but no use :( - Anto Varghese
What does that end up looking like when you do a "view source" on the page? - Pointy
in view source i cant see onblur its because i am using ajax update panel for this control......and browser only give the source of first loaded page - Anto Varghese
You should be able to see the DOM with IE8 or Firefox with Firebug, and that will show you the "live" DOM, not just the loaded page. - Pointy

1 Answers

2
votes

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.