Lately I faced an issue where I was passing a string through query string and the string had quotes and some other special characters. I had to open another modal window where i was supposed to load the new page. Due to quotes it gave me a javascript Error "Expected )".
At once I tried using Uri.EscapeDataString, Uri.EscapeUriString, and HttpUtility.UrlEncode and other similar functions available in c#.
My findings were as follows
original string is: Hello 'Deepa/Hari'
Then,Uri.EscapeDataString("Hello 'Deepa/Hari'") gives: Hello%20'Deepa2FHari'
Uri.EscapeUriString("Hello 'Deepa/Hari'") gives Hello%20'Deepa/Hari'
System.Web.HttpUtility.UrlEncode("Hello 'Deepa/Hari'") gives Hello+'Deepa2FHari'
System.Web.HttpUtility.UrlPathEncode("Hello 'Deepa/Hari'") gives Hello%20'Deepa/Hari'
However JScript.Net's function gives a full proof solution.
Microsoft.JScript.GlobalObject.escape("Hello 'Deepa/Hari'") gives Hello%20%27Deepa/Hari%27
Similarly to unescape, use Microsoft.JScript.GlobalObject.unescape() function.
Friday, May 14, 2010
Monday, May 3, 2010
Stopping a postback on client side with Ajax
Requirement was - on click of a button I wanted to get an alert and then based on logic i either wanted to do a postback or stop it.
Initial code
btn.onClientClick="return validate();"
Javascript code looked like this
function validate()
{
alert('some message');
if(some condition)
return true;
else
return false;
}
I had ajax post back on my button click which i wanted to stop when false is returned from validate function. But with above code it never posted back not even if the function returned true.
Ok! What went wrong
The code btn.OnClientClick = "return validate();" would get rendered as
input type="submit" onclick="return validate(); __doPostback(..)"
So what is happening is whether the javascript function returns true or false it doesnt matter, __doPostBack will never get called.
Lets now change it to make it work
btn.OnClientClick="if(!return validate()) return false;"
This renders
input type="submit" onclick=if(!validate()) return false; __doPostBack(..)"
So only when the validate() function returns false will it execute the first statement and will return from there else it will go to __doPostBack and cause a post back to happen.
Took me some time to figure this out and it just goes to prove that common sense is actually not so common ;-)
Initial code
btn.onClientClick="return validate();"
Javascript code looked like this
function validate()
{
alert('some message');
if(some condition)
return true;
else
return false;
}
I had ajax post back on my button click which i wanted to stop when false is returned from validate function. But with above code it never posted back not even if the function returned true.
Ok! What went wrong
The code btn.OnClientClick = "return validate();" would get rendered as
input type="submit" onclick="return validate(); __doPostback(..)"
So what is happening is whether the javascript function returns true or false it doesnt matter, __doPostBack will never get called.
Lets now change it to make it work
btn.OnClientClick="if(!return validate()) return false;"
This renders
input type="submit" onclick=if(!validate()) return false; __doPostBack(..)"
So only when the validate() function returns false will it execute the first statement and will return from there else it will go to __doPostBack and cause a post back to happen.
Took me some time to figure this out and it just goes to prove that common sense is actually not so common ;-)
Thursday, March 11, 2010
Calling server side code from javascript
Found a good article on how to do this ... Posting some intercepts here
You cannot call server-side code ‘directly’ from client-side code. That is because by design, the server side code executes at server side and client side code at the client. However there are some workarounds. To call serverside code from javascript, you will need to use AJAX, and the easiest way out, is to use the ASP.NET AJAX Extensions.
One option while using Microsoft ASP.NET AJAX is to call ASP.NET Web services (.asmx files) from the browser by using client script. The script can call a webservice containing server-based methods (Web methods) and invoke these methods without a postback and without refreshing the whole page. However this approach could be overkill sometimes, to perform the simplest of tasks. Moreover the logic needs to be kept in a separate .asmx file. We need something that is more ‘integrated’ with our solution.
The option we are going to use in this article involves PageMethods. A PageMethod is basically a public static method that is exposed in the code-behind of an aspx page and is callable from the client script. PageMethods are annotated with the [WebMethod] attribute. The page methods are rendered as inline JavaScript.
Example
C#
[System.Web.Services.WebMethod]
public static string GetContactName(string custid)
{
}
Javascript function calling server method
function CallMe(src,dest)
{
var ctrl = document.getElementById(src);
// call server side method
PageMethods.GetContactName(ctrl.value, CallSuccess, CallFailed, dest);
}
//
And here's the source http://sappidireddy.wordpress.com/2008/03/31/how-to-call-server-side-function-from-client-side-code-using-pagemethods-in-aspnet-ajax/
You cannot call server-side code ‘directly’ from client-side code. That is because by design, the server side code executes at server side and client side code at the client. However there are some workarounds. To call serverside code from javascript, you will need to use AJAX, and the easiest way out, is to use the ASP.NET AJAX Extensions.
One option while using Microsoft ASP.NET AJAX is to call ASP.NET Web services (.asmx files) from the browser by using client script. The script can call a webservice containing server-based methods (Web methods) and invoke these methods without a postback and without refreshing the whole page. However this approach could be overkill sometimes, to perform the simplest of tasks. Moreover the logic needs to be kept in a separate .asmx file. We need something that is more ‘integrated’ with our solution.
The option we are going to use in this article involves PageMethods. A PageMethod is basically a public static method that is exposed in the code-behind of an aspx page and is callable from the client script. PageMethods are annotated with the [WebMethod] attribute. The page methods are rendered as inline JavaScript.
Example
C#
[System.Web.Services.WebMethod]
public static string GetContactName(string custid)
{
}
Javascript function calling server method
function CallMe(src,dest)
{
var ctrl = document.getElementById(src);
// call server side method
PageMethods.GetContactName(ctrl.value, CallSuccess, CallFailed, dest);
}
//
And here's the source http://sappidireddy.wordpress.com/2008/03/31/how-to-call-server-side-function-from-client-side-code-using-pagemethods-in-aspnet-ajax/
Coffee time
Sometimes coffee time talks though very brief have lot of depth.... It generally starts with gossip but the climax rather take way is so wise that the gossip that started it even starts looking worth :D .... Not to miss on today's climax....When you have to choose between being smart or genuine, I would say genuine. Counting on no of people I met were smart, there were many but those who were genuine i could count on fingers... and that's when i occured that the latter is rare....And yea genuinely smart is rarer :) ..
With that note Shubraatri
With that note Shubraatri
Tuesday, December 29, 2009
Spending time with Naishu
Never thought its so funnnnn to watch ur kid..... Few days back she was tossing half way and I was just waiting for her to do it fully. And it was amazing to see her do it one fine day :) ..... I simply screamed to call Gaurav and wanted him also watch it..... We were so happy and almost shouting and this little one was giving us such surprise looks as if thinking "What has happened to these creatures.......why are they soooo excited..... :D" About 20 days later she started moving ahead .... and her technique (as I would call it) was also very interesting... She moved like a snail :D ... moving her cute little bum up in air, lowering her head and pushing herself forward :D .... These are really memmorable momments and I wish I could also capture my feelings and remember the way I felt when she did something new :) ......
Thursday, October 9, 2008
India & History
Sometimes I wonder how did so many languages get formed. Who wrote the rules, the grammar, the phonetics. Would it be right to say that there was only one language in beginning and then different people speaking it differently some making mistakes led to different languages. Well couldnt find the truth about this notion but I did find something that was quite surprising atleast to me. All of us know that Sanskrit is considered as mother of all Indian languages. But very few of us know that if obsereved closesly Sanskrit is very close to Greek and Latin. For e.g Mother is Matr in Sanskrit mater in Latin. Father is Pater in latin and Pitra in Sanskrit.
With these similarities between foreign language and Sanskrit can we surely say that Sanskrit orignated in India. Aryans were believed to speak Sanskrit but Aryans came from deserts of Iran and are believed to settle in different parts on India specially on the coast of Ganges. Rigveda talks lot about Sanskrit and about people who spoke it. There is a mention of a holy drink called Som in Rigveda. This drink is made of a plant called som which is very small without leaves and dark brown in color and this is not an Indian plant. Some people still use it in Pakistan. It is boiled in water and honey is added to lessen the bitter taste. It is believed to have lot of medicinal values.
Aryans then fought among themselves for supremacy. An epic that crystalizes all these fights is Mahabharata. Its the longest poem written in Sanskrit. It is believed to have happened in 3100 BC. Mahabharata tells us variety of things about Indian culture. It has duties, sacrifices, religion, ambition, culture, gods, art, war, weapons, war tactics and every human emotion. Often its said that stories become identity of a nation. Its very evident how Indian society is bound with variety of culture , religion and variety of people speaking different languages and yet united. (Well I know it looks very dramatic to say these things with riots and bombblasts happening in all parts of the country. But this is all political and terrorist activities. I am talking about those Indians who stay in a small colony of a village or a city and might be sharing their dishes or their evening gossips with a neighbor of different religion. I am talking about that Rama who takes a break to offer Namaz :-) )
But is Mahabharata an epic or was it a real story. To find this lets talk about Hastinapur. Today's Hatinapur is a small town in the Doab region of Uttar Pradesh, called Hastinapur, 37 km from Meerut and 110 km from Delhi. Looking at this town you will find it similar to as described in Mahabharata. It was believed to have washed away in flood and you can see that with the layered land found in Hastinapur. What is found inside its grounds is same as on the top. People still use the same stones to make their houses. You can also find the Mahabharata ancient pottery called greywares remains in present Hastinapur. Well I would continue reading on this and posting it. Till then bbye
With these similarities between foreign language and Sanskrit can we surely say that Sanskrit orignated in India. Aryans were believed to speak Sanskrit but Aryans came from deserts of Iran and are believed to settle in different parts on India specially on the coast of Ganges. Rigveda talks lot about Sanskrit and about people who spoke it. There is a mention of a holy drink called Som in Rigveda. This drink is made of a plant called som which is very small without leaves and dark brown in color and this is not an Indian plant. Some people still use it in Pakistan. It is boiled in water and honey is added to lessen the bitter taste. It is believed to have lot of medicinal values.
Aryans then fought among themselves for supremacy. An epic that crystalizes all these fights is Mahabharata. Its the longest poem written in Sanskrit. It is believed to have happened in 3100 BC. Mahabharata tells us variety of things about Indian culture. It has duties, sacrifices, religion, ambition, culture, gods, art, war, weapons, war tactics and every human emotion. Often its said that stories become identity of a nation. Its very evident how Indian society is bound with variety of culture , religion and variety of people speaking different languages and yet united. (Well I know it looks very dramatic to say these things with riots and bombblasts happening in all parts of the country. But this is all political and terrorist activities. I am talking about those Indians who stay in a small colony of a village or a city and might be sharing their dishes or their evening gossips with a neighbor of different religion. I am talking about that Rama who takes a break to offer Namaz :-) )
But is Mahabharata an epic or was it a real story. To find this lets talk about Hastinapur. Today's Hatinapur is a small town in the Doab region of Uttar Pradesh, called Hastinapur, 37 km from Meerut and 110 km from Delhi. Looking at this town you will find it similar to as described in Mahabharata. It was believed to have washed away in flood and you can see that with the layered land found in Hastinapur. What is found inside its grounds is same as on the top. People still use the same stones to make their houses. You can also find the Mahabharata ancient pottery called greywares remains in present Hastinapur. Well I would continue reading on this and posting it. Till then bbye
Saturday, September 13, 2008
Aptitude Questions
1. Every day I struggle at Auto Stand to give exact denomination to Auto Driver. This was recognized by Indian government (as most ppl struggle with denominations). The problem was to print minimum number of different notes, so that with their combination user should be able to give any number below 1000. But condition is to get the number he should use one note only once.
Ex: To get number 8. I can use 1+2+5..... No repitions are not allowed.
2. There are 2 cities A and B, dist. between them is 1000 Km
we have 3000 bananas in city A and a elephant can carry max 1000 bananas at
any given time and it needs to eat a banana every 1 Km.
How Many Max. No. of bananas one can transfer to city B?
Note : the elephant cannot go without bananas to eat.
Answers
1. Here’s my solution – 1, 2, 4, 8, 16, 32, 64, 128, 256, 512. – (10 notes)
2. Let 3 elephants carry it e1, e2, e3. e1 becomes donar till 334 km. e2 then becomes donar till 834 km. Then e3 starts having from his own for 166 km. So he can cary 835 bannanas.
Ex: To get number 8. I can use 1+2+5..... No repitions are not allowed.
2. There are 2 cities A and B, dist. between them is 1000 Km
we have 3000 bananas in city A and a elephant can carry max 1000 bananas at
any given time and it needs to eat a banana every 1 Km.
How Many Max. No. of bananas one can transfer to city B?
Note : the elephant cannot go without bananas to eat.
Answers
1. Here’s my solution – 1, 2, 4, 8, 16, 32, 64, 128, 256, 512. – (10 notes)
2. Let 3 elephants carry it e1, e2, e3. e1 becomes donar till 334 km. e2 then becomes donar till 834 km. Then e3 starts having from his own for 166 km. So he can cary 835 bannanas.
Subscribe to:
Posts (Atom)




