1

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

6
  • 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. Commented Jul 12, 2010 at 14:13
  • i tried that also but no use :( Commented Jul 12, 2010 at 14:33
  • What does that end up looking like when you do a "view source" on the page? Commented Jul 12, 2010 at 14:40
  • 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 Commented Jul 12, 2010 at 14:45
  • 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. Commented Jul 12, 2010 at 14:48

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.....
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. :(

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.