In my previous blog we saw how to call validation controls from javascripts. This blog we will see how to show and hide a ASP.NET AJAX’ (AJAXControlToolkit) ValidatorCalloutExtender control using javascript. Below is an aspx page with a validator callout control.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <body> |
The above code has a dropdown control with an ASP.NET required field validator control and a validator callout control attached to it. As soon as you click the submit button and if there is a validation error the error will be popped out in the validator callout control as shown below.
Popping of the error message in a validator callout control happens automatically. But there may be scenario where you would like to hide or show the validator control using javascript. The below sample code does exactly that.
<script language="javascript" type="text/javascript"> function showValidatorCallout() |
From the above code we can see that there is a code something like this “AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout”. The _currentCallout property will hold the current validator callout control. Whenever there is a validation failure and if there is validator callout control associated with validator control, the _currentCallout property will be assigned the validator callout control. To hide the validator callout control you need to use the “hide” javascript function along with the _currentCallout property as shown below.
AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout.hide(); |
At any given point the _currentCallout property will have only one validator callout assigned, this is to avoid the confusion or dirtiness which can arise by showing all the validator callout control. If all the validator callouts are shown when multiple validation fails then the screen will be cluttered with all the validator callouts popping here and there. To avoid this cluttered view this approach of showing only one validator callout control has been taken.
Similarly to show the validator callout control you can use the “show” javascript method along with _currentCallout property as shown below.
//Gets the AjaxControlToolkit ValidatorCalloutExtender control using the $find method. |
But the above code if used just like that can throw the following error.
Microsoft JScript runtime error: 'this._popupBehavior' is null or not an object |
The reason why this happens is that we have not called either ValidatorEnable or ValidatorValidate javascript functions. These two functions set the necessary things, like _popupBehavior property, for the validator control to work properly. Validator callout controls are not meant to be shown directly they are actually extender controls which extend the functionality of the validation control and these controls are shown automatically when a validator control’ validation condition fails. So if you want to show a validator callout function just call ValidatorEnable or ValidatorValidate javascript function.
That’ about it, on how to hide and show validator callout controls using javascript. I know I have not mentioned anything about “$find” javascript function. We will have a discussion on this in my subsequent blog. For the time being just understand it as a shortcut method to find a component. In our next blog we will see how to set focus to a particular tab in the AjaxControlToolkit’ tab container control.
Some important methods of ValidatorCallout controls
Methods/Properties | Description |
_getErrorMessage() | Gets/returns the error message |
get_closeImageUrl() | Gets/returns the close image url at the top rightmost corner of the validator callout control. Default is an x mark. |
get_isOpen() | Gets/returns a boolean value indicating whether the validator callout is opened or not. |
get_warningIconImageUrl() | Gets/returns the warning icon image’ url. Default is an exclamation mark inside a triangle. |
get_width() | Gets/returns the width of the validator callout control. |
hide() | Function to hide the validator callout control. |
set_closeImageUrl(imageUrl) | Function to set the close image url. One can use this method to change the default X mark. |
set_warningIconImageUrl(imageUrl) | Function to set the warning image. One can use this function to change the default exclamation image used for warning. |
set_width(int) | Function used to set the width of the validator callout control. |
show() | To show the validator callout control. |
_closeImageUrl | Property to get/set the close image. |
_warningIconImageUrl | Property to get/set the warning image. |
_width | Property to get/set the width of the validator callout control |
_popupBehavior | Property using which one can work with the pop up behaviour of the validator callout control. |
_errorMessageCell | Property using which one can modify the error message. |
Some methods of ValidatorCallout._popupBehavior property
Below are listed few of the _methods of _popupBehavior property of validator callout function. These methods are available with only _popupBehavior property. If one wants use these methods then one has retrieve the _popupBehavior property from validator callout control by any of the following two means shown below.
//Retrieving the validator callout control using the $find helper method. |
After retrieving the _popupBehavior property by any of the above means you can use the following methods.
Methods/Properties | Description |
get_x() | Method to get the X coordinates of the validator callout control |
get_y() | Method to get the Y coordinates of the validator callout control. |
get_visible() | Methods that returns a boolean value specifying whether the validator callout control is visible or not. |
set_x(x_coordinate) | Method to set the X coordinate for the validator callout control. Method takes an integer value as an argument. |
set_y(y_coordinate) | Method to set the Y coordinate for the validator callout control. Method takes an integer value as an argument. |
Some methods of ValidatorCallout._errorMessageCell property
Since _errorMessageCell returns a TD (cell) object there is nothing much new it has all the methods/properties corresponding to a cell object. One use of this property is to change the error message of the validator callout extendor control using javascript. To change the error message using javascript see the code below.
//Retrieving the validator callout control using the $find helper method. |
Mail from my blog reader
Recently one of my blog reader mailed me across some problems he was facing with validator callout control. For the benefit of the readers I am pasting the mail chain discussion here, with prior permission from the reader who mailed me.
Hey Sandeep, how are you? I've read your amazing article on: http://sandblogaspnet.blogspot.com/2009/04/ thank you very much, it really help me! So, I want to call all validators, and them call the specific callout, I could execute all the validators, but now I need to know the name of var validator = Page_Validators[validatorIndex]; do you know how to get the id of the validator? validator.Id ? validator.ClientId ? thank you ! this is my solution for now: <script language="javascript" type="text/javascript"> function ValidatePage() { if (typeof (Page_Validators) == "undefined") return; var noOfValidators = Page_Validators.length; for (var validatorIndex = 0; validatorIndex < noOfValidators; ValidatorValidate(validator); if (!validator.isvalid) { var tabPanel = validator.parentElement.control; showValidatorCallout($find(Page_Callout[validatorIndex])); return false; function hideValidatorCallout() { function showValidatorCallout(currrentCallout) { -- My reply-- If I am not wrong when you are calling the function "showValidatorCallout" it doesn't show the validator callout in the tab? Am I right? If yes I have also faced the same problem. I solved this by a dirty hack. So what I did is something like this. ValidatorEnable(dropDownValidator); //Calling the validator callout for the first time. Just a dummy call. Hope this helps. So when you work with tab you have to use the above hack to display validator callout control. I didn't get much time to find out the reason behind but will try find out. --Response from the reader-- I've another problem with the callout's... Please, take a loot in that Do you know how to avoid this ? --My reply-- //Retrieving the validator callout control using the $find shortcut method. or you can use the below code. AjaxControlToolkit.ValidatorCalloutBehavior. Hope this solves your problem. --Reader response-- Yeah... you solve the problems :) thank you again. cheers, |
Hii...
ReplyDeletehi m not en expert like u people out there..nd dont knw whr to ask a question so m simple sharing my prob.with u... if m wrong somewhre pls correct me buddy.. pls guide me out in solving this problem...
ReplyDeleteI have a webform with a checkboxlist with about 20 items.. and i have used a javascript snippet to validate that at least one item is selected out of the checkboxlist.. till now this works fine... but becoz of using this javascript snippet on the submit button's ONCLIENTCLICK event, all of my validators on the page and all the validatorcalloutextenders have stopped working i.e now they are not popping up and the page simply postbacks. and when i remove this javascipt snippet from the OnClientClick, then all those validators and validatorcalloutextenders works just fine....
Hope u understand my prob... Pls give me some suggestion ..it will be highly appriciated...
TC
Hi Anonymous,
ReplyDeleteSo your problem is that your writing custom validation to validate some control in the UI and after that you want the validation controls in the page to fire. Just call Page_ClientValidate() javascript function after your custom javascript logic. Page_ClientValidate() is an ASP.NET runtime generated javascript function which will call/trigger all the validation controls. I hope this helps you. If not do let me know. Also if the validation are not sucessfull and you don't want to postback the page then just return false from the javascript function. Returning a false value will not trigger postback. You can go through my blog "http://sandblogaspnet.blogspot.com/2009/04/calling-validator-controls-from.html" and know about various javascript functions to call validator controls.
Sandeep
Hello, great write-up with good info about the Validator extender, I wish I found this a long time ago. I am having a problem with updating the error message in an extender. I am using the .errormessage property, and have tried the innerHTML as your blog mentioned, but neither actually update the error message after the first time. I created a post about my issue here: http://stackoverflow.com/questions/1255626/updating-validatorcallout-error-message-from-javascript
ReplyDeleteAny ideas on that one?
Hi,
ReplyDeleteWhat do you mean by "First time"? Could you shed more light on that? Also I have seen you code in the pasted link. Is the sender object a validation control?
Hello, by first time I mean if you enter an incorrect value, the callout will display the correct error message with the correct range (the lowRange and highRange). After that, if you enter a new invalid value that has a different lowRange and highRange, the callout will not display the new range, but will still display the range from the first validation message. If I display the regular validator message (not the callout), the correct values will be displayed every time. So it is just that the ValidatorCalloutExtender will not get its error message updated. And yes, the sender is the CustomValidator. Thanks!
ReplyDeleteHi,
ReplyDeleteI have an issue where the callout validator control shows through in IE when placed next to a drop downlist..meaning the error message is not fully visible since the control is transparent and the control below it are visible. Do you have suggestion to solve this issue?
Thanks,
DP
Hi DP,
ReplyDeleteCould you send me an email with an image attached showing your problem. My email id is sndppr@gmail.com
This comment has been removed by the author.
ReplyDeleteI found this hack for re-setting the text of a callout and it's worked pretty well for me in my Custom Validators.
ReplyDeletevar cell = sender.ValidatorCalloutBehavior._errorMessageCell;
//Cell is going to be null first time
if (cell != null) {
cell.innerHTML = sender.errormessage;
}
Thanks Michael, for sharing the info.
ReplyDeleteHello Sandeep,
ReplyDeleteNice article!!I have a doubt pls can you help me to solve my pblm. I want to expand the link say syllabus and show its content or data under it which is in the database.
like +Syllabus
when i expand this ,i want its data to be shown under it
-Syllabus
This is a concept.........
Hope you got my problem..Could you pls help me out frm this pblm.Kindly waiting for ur reply.
Myself Neha
Hi Neha,
ReplyDeleteFor your problem there are two possible solution. If you want to show the content on the click of the plus symbol or image, you can use ASP.NET AJAX' "CollapsiblePanel" control.
If you want to show multiple content in different accordians then you can use the "Accordian" control in the ASP.NET AJAX toolkit.
Thank you very much for your reply Sandeep. My alias name is Neha.. I will use the ajax control and try to solve the issue. Once again thank u.
ReplyDeleteHello Sandeep.How to load the data which is in the database table. Syllabus data is in the BookTable thru the column i need to retreive.The control which u told,can v retreive the database content? Please help me.Kindly request.
ReplyDeleteThank you in advance
Hi Neha,
ReplyDeleteI doubt whether it is a databound control. Even if it is not you can very well write code to retrieve data from the database and pump the same to the controls content elements. I would suggest to take a look at the control and understand it and then customise it to your requirement.
Hi Sandeep. Do you have a solution on reassigning the TargetControlID of a callout extender? I'm encountering this issue whereby I can clone the bottom row of a table dynamically, and there's no limit on the number of rows that can be added (cloned.) The row itself contains several controls including a textbox with custom validator & callout extender. Since it is a clone, the IDs are the same and won't work, and in fact raises an exception when I call Page_ClientValidate. A help/guide would be much appreciated.
ReplyDeleteJohn
Can you please give me idea to show all the callout extender at the same time of button click. i.e when we submit the form I need to display all the callout extender opened view. Please help me on this.
ReplyDeleteIf I am not wrong it is not possible to display all the callout at the same time. For this you will have to use validation summary control.
ReplyDeleteThanks!!It worked for me...
ReplyDeleteDo you know how to find out which validatorCallOut is exactly open?
ReplyDeleteHi,
ReplyDeleteIf I am not wrong the "_currentCallout" property will hold the active validator call out.
Hi Sandeep ,
ReplyDeletethanks for the code.
It works like a charm.
thanks
ram sagar mourya
hi sir this is kusuma now we are doing mini project, i have doubt on how to do validation for login form by using javascript with out using validation controls in asp.net,normally form consists of username and password,user name must be 6 characters and username should not be the special characters,and password also same.sir plz sir can u send me the code,its very urgent sir.my Email id is masuku503@gmail.com,
ReplyDeleteHi Kusuma,
DeleteYou can do the validation using Javascript. The validation is pretty easy and I think with little bit of effort you easily do it yourself. there are lot of e.g. in the net. Just google around and you will be good to go.
Regards,
Sandeep
hiiiii sir...
ReplyDeletecan u plz tell me how to use print dialog control in asp.net
Hi Sandeep,
ReplyDeleteI tried the above code but its saying me that AjaxControlToolkit is undefined r null i have referenced also the AJAXCONTROLTOOLKIT. Can u pls reply me asap. Thanks
Hi all,
ReplyDeleteI tried some of code listed above but AjaxControlToolkit is undefined error appears even if i reference ajaxtoolkit set CombineScript to true.
Thanks
I'm extremely inspired along with your writing skills and also with the structure to your weblog. Is that this a paid topic or did you modify it yourself? Anyway keep up the excellent high quality writing, it's uncommon to see a nice weblog like this one nowadays..
ReplyDeletegood post and information so good blog sites %100 thx.
ReplyDeleteVenüsbet Venüsbet Grandbetting Grandbetting Hiltonbet Betnano Marsbahis Meritking Asyabahis
Hello, an amazing Information dude. Thanks for sharing this nice information with us. Betnano
ReplyDeleteSMM PANEL
ReplyDeletesmm panel
İs ilanlari blog
instagram takipçi satın al
hirdavatciburada.com
Beyazesyateknikservisi.com.tr
Servis
tiktok jeton hilesi
Good content. You write beautiful things.
ReplyDeletemrbahis
mrbahis
vbet
vbet
korsan taksi
sportsbet
sportsbet
hacklink
taksi
instagram takipçi satın al
ReplyDeletecasino siteleri
NWA0
canlı sex hattı
ReplyDeleteheets
salt likit
salt likit
puff bar
XK2S
canlı sex hattı
ReplyDeleteheets
salt likit
salt likit
puff bar
0ORBD
hatay
ReplyDeleteığdır
iskenderun
ısparta
istanbul
3PSW
malatya
ReplyDeleteelazığ
kadıköy
istanbul
şişli
DMWİGE
bilecik
ReplyDeletegebze
ısparta
şırnak
alsancak
İ52P7T
https://saglamproxy.com
ReplyDeletemetin2 proxy
proxy satın al
knight online proxy
mobil proxy satın al
28HSJF
شركة مكافحة حشرات بالقطيف G1B2zuThQO
ReplyDelete