
Quote of the day
Wednesday, December 21, 2011
Thursday, September 08, 2011
prevent post back on asp.net button click

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>hello world</title>
<script type="text/javascript">
function helloWorld(){
var firstAlert = "This is my first alert function called. I'm happy to learn Javascript.... happy programming";
alert(firstAlert);
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSample" runat="server" Text="Click Me" OnClientClick="helloWorld();" />
</div>
</form>
</body>
</html>
There are 2 types of controls in asp.net.
1. Server Side Controls - which is to call c# or vb functions and to perform server side operations like to call database, use dataTable, dataSet, etc
2. Client Side Controls - to perform client side operations
The above code uses asp.net Button which alerts a text in javascript. But if you note, after the alert, the request goes to the server because it is a server control. All server controls by default go to the server even if there is no event written for it. How to prevent this?
You can prevent an asp.net button hitting the server using javascript "return false" method. We have a property in asp.net button "OnClientClick" where we call the javascript method. We can return false in the method which it is calling so that there won't be any post back occured.
In the above example, "helloWorld" method alerts and finally returns false, which prevents to hit the server. You can also write this "return false" in the method property after calling the "helloWorld()" method as below:
<asp:button id="btnSample" runat="server" text="Click Me" onclientclick="helloWorld(); return false"></asp:button >
happy telling,
achu
Thursday, January 06, 2011
print * in vertical
I remember one day in the year 2006, my cousin Senthil Kumaran asked me to write a program in C language to print *(stars) vertically like below:
Ha ha, really I didn't even try or know actually. Later I learned few programming languages of my own and got help from others including Senthil dha.
I have a little free time these days and was thinking to do something useful. So started writing string manipulating programs using c#. Today, this vertical star striked in my mind. As I don't have Turbo C or C++, I did it in Microsoft C#.
Below is the code for printing the *s vertically based on the numbers entered in the textbox:
Output:
My output has '+' instead of ' ' (space), because I've used asp.net label to display the stars and it does not accepts multiple spaces.However, it supports in c# console application.
My trick was to print spaces before printing * in each line till the count. For example, if the number entered is 5, the first line would print 4 spaces and then a *, the next line would print 3 spaces and then 2 *s and so on.
Console application code:
Console application output:
I'm not sure about other ways of printing. If anybody knows, then let me know.
Wednesday, January 05, 2011
swapping numbers without temp variable in c#
We all know, swapping numbers with temp variable is possible and easy. Recently I just rememberd of one of the interview questions "swap 2 numbers without using temp variable". I started thinking and did that in c#.
Following is the code which swaps 2 numbers without using temp variable

Output:

I was struggling by trying with different formulae, but nothing worked. I didn't think that we can recalculate first variable for resulting the output.
I got the idea from this post answered by this member.
Following is the code which swaps 2 numbers without using temp variable
Output:
I was struggling by trying with different formulae, but nothing worked. I didn't think that we can recalculate first variable for resulting the output.
I got the idea from this post answered by this member.
Tuesday, January 04, 2011
fibonacci series using c#
Following code generates fibonacci series using c#. The output is displayed in asp.net Label. The textbox gets the no. of fibonacci series.
take distinct values using c#
Following code displays only the distinct values entered in a textbox using (,)comma separator.
super happy coding.....
Output:
super happy coding.....
reverse words in a sentence using c#
Following code reverses the words in a sentence. It is done in c#. I've used asp.net Label to display the output.
Subscribe to:
Posts (Atom)