Reputation: 3241
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
Upvotes: 1
Views: 1354
Reputation: 21117
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.
Upvotes: 2