Feb 15, 2014

how to call javascript function on button click in asp.net

Create a function like
<script type="text/javascript" language="javascript">
 function ValidateForm()
    {
           var Form=document.getElementById('frmDemo');
           alert(Form.txtbox1.value);
           alert(Form.txtbox2.value);
           if(parseFloat(Form.txtbox1.value)<parseFloat(Form.txtbox2.value))
            {
                alert('hello all');
                document.getElementById('txtbox1').focus();
                return false;
            }
//or
            if(parseFloat(Form.txtbox1.value)==parseFloat(Form.txtbox2.value))
            {
                alert('hello all');
                document.getElementById('txtbox1').focus();
                return false;
            }
//or
            if(parseFloat(Form.txtbox1.value)<parseFloat(Form.txtbox2.value))
            {
                alert('hello all');
                document.getElementById('txtbox1').focus();
                return false;
            }
    }
 
</script>
now we can call like 
<asp:Button ID="BtnSubmit" Text="Proceed" runat="server" OnClientClick="return ValidateForm();"/>


Feb 14, 2014

JQUERY INTERVIEW QUESTIONS

1).What is difference between jQuery's ready and holdReady?
jQuery's ready is an event which gets triggered automatically when DOM is ready while holdReady is a signal/flag to hold this triggering. holdReady was included in 1.6 version and it works only if used before the execution/triggering of ready event. Once ready event is fired, it has nothing to do. It is useful in dynamically loading scripts before the ready starts. It release ready event execution when used with a true parameter.

2).What is Jquery $.ajax() method?
The Jquery ajax() method is used to perform an AJAX (asynchronous HTTP) request.

3).Name any four paremeter of Jquery ajax method?
url : Specifies the URL to send the request to. Default is the current page
type : Specifies the type of request. (GET or POST)
data : Specifies data to be sent to the server
cache: A Boolean value indicating whether the browser should cache the requested pages. Default is true
beforeSend(xhr): A function to run before the request is sent

4).When can you use jQuery?
JQuery can be used to perform
1.Call methods on specific events
2.Traverse the documents
3.For apply CSS
4.Manipulation purpose and
5.To add effects too.
6.For apply animations
7.For give atractive look (dialogbox etc)
8.For asynchronous calls ($.ajax())



5).How to select combobox selecte value and text using Jquery?
Example:
var StateID = $("#StateCbx").val(); // Or you can use it $("#iStateID").val();
var StateName = $("#StateCbx option:selected").text();
alert("Selected combobox text is= " + StateName + " and value is= " + StateID);

6).JQuery html() method works for both HTML and XML documents?
No, It only works for HTML

7).Can you call C# codebehind method using Jquery?
Yes

8).How can you call a method inside code-behind using jQuery?
By $.ajax and by declaring method a WebMethod

9).What is the use of jQuery.data()?
jQuery’s data method gives us the ability to associate arbitrary data with DOM nodes and JavaScript objects. This makes our code more concise and clean.

10).Is jQuery a W3C standard?
No

11).What is the use of jquery .each() function?
Basically, the jQuery .each() function is used to loop through each element of the target jQuery object. Very useful for multi element DOM manipulation, looping arrays and object properties.
Example:-
In this example alert box will open 3 times because dom contain 3 <li> tags

Code:
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("li").each(function(){
      alert($(this).text())
    });
  });
});
</script>

<ul>
<li>Coffee</li>
<li>Milk</li>
<li>Soda</li>
</ul>

`12).If you have a server control(asp.net server control, Button) and on the click of button you want to call a jquery function, So how you will call a jquery function without postback?
ASP.NET provides the OnClientClick property to handle button clicks. You can use this property on Button, LinkButton and ImageButton. The same OnClientClick property also allows you to cancel a postback.
So I can use OnClientClick property and Jquery function will return false.
Example

Code:
<script type="text/javascript">
    function callMe()
    {
    alert('Hello');
        return false;
    }
</script>
<asp:Button ID="Button1" runat="server" OnClientClick="return callMe();" Text="Button" />

13).What is the use of .Size() method in Jquery?
Jquery's .size() method returns number of element in the object. That means that you can count the number of elements within an object.

14).What is the difference between jquery.size() and jquery.length?
Jquery.size() and jquery.length both returns the number of element found in the object. But, jquery.length is faster than jquery.size() because size() is a method but length is a property.

15).How you can debug Jquery code/What are the technique to debug jquery?
Add the keyword "debugger;" to the line from where we want to start the debugging and then run the Visual Studio in Debug mode by pressing F5 or using the Debug button.

16).Difference between jQuery-x.x.x.js and jQuery.x.x.x min.js?
jQuery-x.x.x.js = Pretty and easy to read Smile Read this one.
jQuery.x.x.x min.js = Looks like jibberish! But has a smaller file size. Put this one on your site for fast loading and less size.

17).How to get the server response from an AJAX request using Jquery?
When invoking functions that have asynchronous behavior We must provide a callback function to capture the desired result. This is especially important with AJAX in the browser because when a remote request is made, it is indeterminate when the response will be received.
Below an example of making an AJAX call and alerting the response (or error):

Code:
$.ajax({
url: 'pcdsEmpRecords.php',
success: function(response) {
alert(response);
},
error: function(xhr) {
alert('Error! Status = ' + xhr.status);
}
});

18).Do we need to add the JQuery file both at the Master page and Content page as well?
No, if the Jquery file has been added to the master page then we can access the content page directly without adding any reference to it.
This can be done using this simple example
<script type="text/javascript" src="jQuery-1.4.1-min.js"></script>

19).Difference between onload() and document.ready() function used in jQuery?
We can add more than one document.ready() function in a page.
we can have only one onload function.
Document.ready() function is called as soon as DOM is loaded.
body.onload() function is called when everything (DOM, images)gets loaded on the page.

STATE MANAGEMENT QUESTIONS

1).What is state management?
Ans: State management is the process by which you maintain state and page information over multiple requests for the same or different pages. 

2).Http is stateless, What does this mean?
Ans: Stateless protocol is a communications protocol that treats each request as an independent transaction that is unrelated to any previous request so that the communication consists of independent pairs of requests and responses.

3).What is Session?
Ans: We know that Http is stateless, means when we open a webpage and fill some information and then move to next page then the data which we have entered will lost.
It happed do to Http protocol stateless nature. So here session come into existence, Session provide us the way of storing data in server memory. So you can store your page data into server
memory and retrieve it back during page postbacks.

4).What are the Advantage and disadvantage of Session?
Ans: Advantages:
Session provide us the way of maintain user state/data.
It is very easy to implement.
One big advantage of session is that we can store any kind of object in it. :eg, datatabe, dataset.. etc
By using session we don't need to worry about data collesp, because it store every client data separately.
Session is secure and transparent from the user.
Disadvantages:
Performance overhead in case of large volumes of data/user, because session data is stored in server memory.
Overhead involved in serializing and de-serializing session data, because in the case of StateServer and SQLServer session modes, we need to serialize the objects before storing them.

5).What is Session ID in Asp.net?
Ans: Asp.Net use 120 bit identifier to track each session. This is secure enough and can't be reverse engineered. When client communicate with server, only session id is transmitted, between them. When client request for data, ASP.NET looks on to session ID and retrieves corresponding data.

6).By default where the sessions ID's are stored ?
Ans: By default, the unique identifier for a session is stored in a non-expiring session cookie in the browser. You can specify that session identifiers not be stored in a cookie by setting the cookieless attribute to true in the sessionState configuration element.
We can also configure our application to store it in the url by specifying a "cookieless" session
The ASP Session cookie has this format:-
ASPSESSIONIDACSSDCCC=APHELKLDMNKNIOJONJACDHFN


7).Where does session stored if cookie is disabled on client’s machine?
Ans: If you want to disable the use of cookies in your ASP.NET application and still make use of session state, you can configure your application to store the session identifier in the URL instead of a cookie by setting the cookieless attribute of the sessionState configuration element to true, or to UseUri, in the Web.config file for your application.
The following code example shows a Web.config file that configures session state to use cookieless session identifiers.
Code:
<configuration>
  <system.web>
    <sessionState
      cookieless="true"
      regenerateExpiredSessionId="true"
      timeout="30" />
  </system.web>
</configuration>


8).Can you describe all the property set in web.config under session state?
Ans:
Code:
<configuration>
  <sessionstate
      mode="inproc"
      cookieless="false"
      timeout="20"
      sqlconnectionstring="data source=127.0.0.1;user id=<user id>;password=<password>"
      server="127.0.0.1"
      port="42424"
  />
</configuration>
Mode: The mode setting supports three options: inproc, sqlserver, and stateserver. As stated earlier, ASP.NET supports two modes: in process and out of process. There are also two options for out-of-process state management: memory based (stateserver), and SQL Server based (sqlserver). We'll discuss implementing these options shortly.
Cookieless: The cookieless option for ASP.NET is configured with this simple Boolean setting.
Timeout: This option controls the length of time a session is considered valid. The session timeout is a sliding value; on each request the timeout period is set to the current time plus the timeout value
Sqlconnectionstring: The sqlconnectionstring identifies the database connection string that names the database used for mode sqlserver.
Server: In the out-of-process mode stateserver, it names the server that is running the required Windows NT service: ASPState.
Port: The port setting, which accompanies the server setting, identifies the port number that corresponds to the server setting for mode stateserver.

9).What are Session Events?
Ans: There are two types of session events available in ASP.NET:
Session_Start
Session_End
You can handle both these events in the global.asax file of your web application. When a new session initiates, the session_start event is raised, and the Session_End event raised when a session is abandoned or expires.

10).How you can disable session?
Ans: If we set session Mode="off" in web.config, session will be disabled in the application. For this, we need to configure web.config the following way:
Code:
<configuration>
  <sessionstate  Mode="off"/>
</configuration>