I'm sure you have experinence of building autocomplete functionality in some way. There many ways to do it -for example with AJAX Control Toolkit. Since i'm teaching myself a jquery now i want to implement the jquery autocomplete plugin in my ASP.NET site.
Its looks easy as it appears in the exapmle :
After placing this javascript you only need to create input with id -"LOCATION" and the things start working.
So far so good.
The only problem for me with this code is that values coming from the Array(cities). I want values to come from the WebService. So i desided to build some small site project to experience with plugin.
This is the WebService code:
As you can see there is no logic at all- web method returns the ";" deleimted Cities names in string.
Firstly i tryed to fill the values in separate ajax request-But its
Just Not Worked
Why?
Because the AJAX request that fills data in 'cities' Array is asyncronic. It means that when compiler reaches the '.autocomlete(cities' -the cities is still empty.
One way to work this around is to put the 'autocomplete' inside the 'success' function of request:
This works fine [Download source]
But I'm not content with this solution because i want the 'suggested' values to be filled in response of user enter characters:
For this ,as i saw in plugin documentation i can put the WebService Url directly inside statement:
Here the things started to be messy. The problem is the break point in the code of webservice became not reacheable.
It taked me a long time to figure out what is the problem. I figured that after inside plugin code (jquery.autocomplete.js) in the 'request' funciton i put the error handler
inside '$.ajax' call. Yes, the ajax call of the plugin cannot reach the server unless you put following code inside web.config under 'system.web' node:
Now, after i can reach the server i started to get another error inside 'parse' function inside the plugin that tryes to split the data to rows with '\n' key.
As solution to this problem i decided to override the 'parse' function:
Now the things started to work. Now we see all cities in the list no matter wath user has
entered.
To repair this i had modifyed the WebService- GetCities method:
So now the method getting user input as parameter.
The only thing left is to modify the 'data' sended with the request inside the 'request' function of the plugin:
Now the thing is realy working:
[Download the source]
1/24/11
1/1/10
Web User Control
Now i want to make all the login form (with the datepicker enabled field) to be placed on the page as Web User Control.
Lets add to our project new web user control :
Right click on project->Add new item-> Web User Control
Lets call it DatePickerControl
copy the markup code inside the
<input name="DatePickerControl1$txtBirthDate" type="text" id="DatePickerControl1_txtBirthDate" name="txtBirthDate" style="height:24px;" />
One way to solve this problem is to register the javascript in the Load Page event of contol:
Lets add to our project new web user control :
Right click on project->Add new item-> Web User Control
Lets call it DatePickerControl
copy the markup code inside the
<input name="DatePickerControl1$txtBirthDate" type="text" id="DatePickerControl1_txtBirthDate" name="txtBirthDate" style="height:24px;" />
One way to solve this problem is to register the javascript in the Load Page event of contol:
string script = "<script type=\"text/javascript\">$(function() {"+
"$('#"+txtBirthDate.ClientID + "').datepick({maxDate: 0});"+
"$('#form1').validate({" +
"errorPlacement: $.datepick.errorPlacement," +
"rules: {" +
"txtBirthDate: { dpMaxDate: [] }" +
"}," +
"messages: {" +
" txtBirthDate: '<span style=\"color:red\">The date is icorrent or later than now!</span>'" +
" } " +
"});" +
"});</script>";
Page.RegisterClientScriptBlock("datepickerfuncs",script);
12/31/09
Validation
Now, since the user can write free text in the txtBirthDate input we need to validate if the date he intered is in the correct format.
To do so i will use the RegulaExpressionValidator.
lets write some regular expression to validate that date is in the dd.MM.yyy format...
^(((0[1-9]|[12]\d|3[01])\.(0[13578]|1[02])\.((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\.(0[13456789]|1[012])\.((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\.02\.((1[6-9]|[2-9]\d)\d{2}))|(29\.02\.((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$
Oops! A little problem here: the validator functions are triggered by "onChange" event causing a bug
To do so i will use the RegulaExpressionValidator.
lets write some regular expression to validate that date is in the dd.MM.yyy format...
^(((0[1-9]|[12]\d|3[01])\.(0[13578]|1[02])\.((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\.(0[13456789]|1[012])\.((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\.02\.((1[6-9]|[2-9]\d)\d{2}))|(29\.02\.((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$
Oops! A little problem here: the validator functions are triggered by "onChange" event causing a bug
Frotunately there is a solution- The plugin have its own validator:
We have only to add two more js files:
<script src="/moia/jquery.datepick.package-3.5.2/jquery.validate.js" type="text/javascript"></script>
<script src="/moia/jquery.datepick.package-3.5.2/jquery.datepick-validation.js" type="text/javascript"></script>
{The order is important!}
and add to our code:
23 $('#form1').validate({
24 errorPlacement: $.datepick.errorPlacement,
25 rules: {
26 txtBirthDate: { dpMaxDate: [] }
27 },
28 messages: {
29 txtBirthDate: '<span style="color:red"></span>'
30 }
31
32 });
Now we almost got the thing working , only one imporatne thing left:
there must be the "name" attribute inside txtBirthDate control. (The name is not compatible tag for asp control so you will get warning)
Now we almost got the thing working , only one imporatne thing left:
there must be the "name" attribute inside txtBirthDate control. (The name is not compatible tag for asp control so you will get warning)
<asp:TextBox ID="txtBirthDate" runat="server" Height="24px" name="txtBirthDate">
Now the datepicker and validator should work.
Good
Subscribe to:
Posts (Atom)
Getting started with docker
It is very simple to get started usig docker. All you need to do-is download the docker desktop for your system Once you get docker syste...
-
It is very simple to get started usig docker. All you need to do-is download the docker desktop for your system Once you get docker syste...
-
well - main thing application should do - is to display data. so, you can fetch data from external source very simple: export const getSta...
-
In frameworks like angular and react we have this behavior that tracks every change of model (a javascript variable which stores state of so...



