Posts

Showing posts from July, 2012

Accessing data from SharePoint 2010 to an ASP.NET application

Image
In order to access the SharePoint lists and document libraries data from a native ASP.NET application we need to make use of the web services that have been provided. The full list of the web services available from within SharePoint 2010 can be found here http://www.etechplanet.com/blog/sharepoint-web-services-7c-list-of-all-web-services-available-with-wss-30moss-2007.aspx The steps to access the web service are as follows: Create a native ASP.NET web application Add a reference of the web service Create an object of the web site Pass on to the web service object the credentials using which the access will be authorized Retrieve the results using the appropriate method

Making the controls in a page as readonly

Image
In many cases we have found that when a form has been submitted and is being accessed by other users, if the page controls have been set as Enabled false, it is not always clearly visible as IE by default greys out the controls. One solution to over come this issue is to make all the controls as ReadOnly, but one limitation, not all the controls in ASP.NET have the ReadOnly property. In order to overcome this the following code is very useful. Just call the method as " readOnlyInputs(Page.Controls) ".

How can you make an ASP.NET control to do a postback without the client-side or server-side validation being triggered?

Image
In order to make sure that both the validations are not triggered when the control is posted back by setting the following property of the control to false, “ CausesValidation ”.

How can we disable client side script from being triggered in ASP.NET controls?

Image
In order to disable any client side validations to be triggered, for any ASP.NET control the following property needs to be marked as false, “ EnableClientScript ”.

How to make sure that all the validation controls in the page has returned the true value?

Image
In order to make sure that all the values entered by the user in the form is valid and has passed all the validation rules enforced we need to use the following code “ Page.IsValid ”. It returns a Boolean value.

How can we force all the validation control to run?

Image
All the validation controls in the ASP.NET page can be run by using the following code “ Page.Validate() ”. Optionally one can also specify the name of the Validation Group name, as a parameter, to be executed.

How do you show the entire validation error message in a message box on the client side?

Image
In order to display all the validation error messages in a message box on the client side one needs to use the ValidationSummary control, this will display the messages that have been set in the ErrorMessage property of the validation control. If a summary needs to be shown instead of a message box, we just need to set the ShowSummary as true and ShowMessageBox as false .

How many types of validation controls are provided by ASP.NET?

Image
There are mainly six types of validation controls provided by ASP.NET which are as follows: Required Field Validator This validator ensures that the user does not skip a form entry field. Regular Expression Validator This validator is used to match the user's entry against an expression defined by a regular expression. Example of some commonly used regular expressions: URL : (http(s)?://)?([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)? Email : \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* US Zip Code: ^(\d{5})(-\d{4})?$ Reference: http://msdn.microsoft.com/en-us/library/ms972966.aspx Range Validator This validator is used to compare data based on a range provided. Once can change the Type to match the requirements. Compare Validator This validator is used to compare data entered by user in two different fields, for e.g. password One can change the Type or the Operator to match the requirements. Custom Validator Custom validator is used in cases where custom code is needed to...

What is the difference between server-side and client-side validations?

Image
After the user enters data into a web form and clicks on the Submit button the form data is sent to the server as a request, you can perform server-side validation on the data. If the data is incorrect or not valid, you can send back a response stating the same. This is known as server-side validation. If, however, when the user clicks the Submit button, a scripting language that is part of the overall HTML page is initiated to check the validity of the data before it is sent to the server, this is known as client-side validation. In server-side validation a round trip to the server is needed which makes the processing of the page a lot slower than client-side validations where the round trip to the server is omitted and the data is validated on the client side only. It is always recommended to use a client side validation rather than a server side validation.

Administrator wants to make a security check that no one has tampered with the “ViewState”, how can he ensure this?

Image
Microsoft has provided the administrator with two mechanisms for increasing the security of ViewState, which are as follows: Machine Authentication Check (MAC) This is done at the page level where in the page directive we need to add the following: Encrypting the ViewState This is done at the machine level in the machine.config file. In the tag <machineKey" the property "validation" needs to be set to "3DES".

How can one stop an ASP.NET page not to maintain the “ViewState”?

Image
In order to stop a page to maintain its “ViewState” one has to include a page directive in the ASPX page as “EnableViewState="false"”. The same can be applied for any control within the page.

What do you understand by “ViewState”?

Image
“ViewState” is an ASP.NET technique which is used to hold pre-existing data in the controls of the page when the page is posted back otherwise which would have been lost. The “ViewState” data is not stored in the server rather it is transported to and from from the server.  The data contained in the “ViewState” is stored as name value pairs, it actually stores the ID of the control and its value rather than the control itself in an encrypted format.  It is stored in a hidden field called “_VIEWSTATE”. When we look at the page source it will show up something like this Be very diligent in using the ViewState from within the code behind as it only adds to the overhead of the page to be displayed. Code to user the ViewState from within the code behind as shown below

What is the difference between “System.String” and “System.StringBuilder” class?

Image
Both the classes perform the same function of storing a string value. System.String is said to be immutable , the reason being when a value is assigned to the variable it is stored in the memory in a space and when the assigned value is changed, the new value is stored in a new place in the memory and the older instance is deleted. Whereas System.StringBuilder is said to be mutable , the reason being even when the value is changed it is stored in the same memory space. The performance of StringBuilder is better than the String class. One can concatenate strings in StringBuilder by using the method “Append()” whereas in String it can be achieved by using the sign “+=”.

What is the difference between “.ToString()” and “Convert.ToString()”?

As such there is no difference between the two methods; both will be returning the same result. The only difference is that if a NULL value is passed on to the methods, the “.ToString()” will not be able to handle NULL values and will be returning " Null reference exception ".  While the method “Convert.ToString()” will be returning an empty string.

What is Event Bubbling?

Image
Server controls like Data Grid, Data List and Repeater can have other child controls inside them. These child control do not raise their events by themselves, rather they pass the event to the parent container, which is then passed on to the page as “ ItemCommand ” event of the parent container. As the child control send events to parent it is termed as event bubbling. Code for the front end Code to be used in the back end

Explain the code behind steps to connect to a database

Image
To connect to a database from code behind firstly we need to read the connection string provided in the web.config file and create a SQLConnection object using the same. The code is as follows: In order to use this code the following directives needs to be added to the page:

How can we specify the database connection string in the “web.config” file?

Image
The database connection string is specified in the <connectinString> tag of the web.config file

What is the difference between “web.config” and “machine.config” file?

The “machine.config” file is created when Visual Studio .NET is installed, where as the “web.config” is created when ever an ASP.NET Web Application is created “machine.config” file is known as the machine/server level configuration file whereas “web.config” file is known as the application level configuration file There will be only one “machine.config” file per machine whereas there will be one “web.config file per web application i.e. there can be more than one per machine “machine.config” file is at the highest level of the configuration hierarchy whereas the “web.config” file inherits the settings from the “machine.config” file The “machine.config” file resides in the path “C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\CONFIG” whereas the “web.config” file resides in the root directory of the web application

What is use of the "appSettings" section in "web.config" file?

Image
The <appSettings> element of the web.config file is a place to store server names, file paths and other miscellaneous settings needed by an application to perform a specific task. The <appSettings> value can be accessed from the code behind using the following code: In order to use the ConfigurationManager class, one needs to add the following directive in the code behind:

What is the name of the ASP.NET worker process in a server machine?

The name of the worker process in a server machine is “ w3wp.exe ”.

What is the name of the ASP.NET worker process in machines other than the server?

The name of the worker process in machines other than the server is “ aspnet_wp.exe ”.

What is the difference between Managed Code and Unmanaged Code?

Unmanaged code is the code that is directly executed by the Operating System. The unmanaged code is architecture specific it can be run only in the machine where it has been complied, the complier transforms the code into the machine language which is then executed, in case it needs to be run in a different machine it needs to be complied again. In unmanaged code the developer needs to take care of the security, memory management etc. Few e.g. are VB 6.0, C++, C etc. On the other hand the Managed Code is the code that is not targeted directly to the Operating System rather to the Managed Runtime Execution Environment . For the .NET Framework it is known as CLR (Common Language Runtime) . When a managed code is complied it is transformed into MSIL (Microsoft Intermediate Language, currently known as CIL (Common Intermediate Language)) and an executable is created and also the meta data is created which describes the characteristics of the methods and properties. The CLI specif...

What is the difference between Server.Transfer() and Response.Redirect()?

Response.Redirect() can jump from one server to another but Server.Transfer() navigates within the same server only The URL of the page does not change in case of Server.Transfer(), as it copies the requested page on the current page, so it is difficult to recognize the current page if developers are using Server.Transfer() and require the current page URL Response.Redirect() takes many round trips to the server so it is little bit slower in comparison with Server.Transfer() The Server.Transfer() method has a second parameter—" preserveForm ". If you set this to “ True ”, using a statement such as Server.Transfer("WebForm2.aspx", True) , the existing query string and any form variables will still be available to the page you are transferring to. The values can be retrieved using the method “ Request.Form("<control name>") ”

What is the difference between ASP and ASP.NET?

ASP stands for Active Server Pages and ASP.NET is the next generation of ASP. After ASP.NET was introduced the older version came to be known as Classic ASP ASP used to use a simple scripting language, VB Script, with the advent of ASP.NET a more powerful, robust and object oriented language is being used such as C#, VB.NET, J# etc In ASP there were no server side controls, one had to write all the HTML tags manually, in ASP.NET a rich set of controls are being provided knows as Server and HTML controls. It is very easy to drag and drop the controls on to the page and VS.NET would automatically generate the default tags ASP uses VB Scripts which cannot be complied , the script used to be interpreted when the page was being executed. Whereas ASP.NET uses modern languages likes C# and VB.NET which are complied into Microsoft Intermediate Language (MSIL) ASP uses ADO to connect to database whereas ASP.NET uses the next generation of ADO called ADO.NET to connect to database With ...

What do you understand by “PostBack”?

ASP.NET is a server-side and not a client-side technology, events that occur on a page are handled by code running on the server. For this to work, ASP.NET uses the mechanism of “ Postback ”. When an event is triggered, for instance a button is clicked; the page is submitted back to the server for processing, along with the information about the event and any existing data on the page (via view state).

Explain the “AutoPostBack” feature in ASP.NET?

Image
“ AutoPostBack ” is the mechanism, by which the page will be posted back to the server automatically based on some events in the web controls contained within a page.  In some of the web controls, the property called “ AutoPostBack ”, which if set to “ True ”, will send the request to the server when an event happens in the control. The default the value is always set to “ False ”. Whenever the “ AutoPostBack ” attribute to “ True ” for any of the web controls, the .NET framework will automatically insert the following few lines of code to the HTML generated to implement this functionality: A JavaScript method with name __doPostBack (eventtarget, eventargument) This method is inserted to the HTML source to implement the “AutoPostBack” functionality, it will submit the form when ever called. The two parameters in this method i.e. eventtarget and eventargument do the actual work of selecting the control to fire the event. This method will set the value of th...

How can you identify that the page has been a “PostBack”?

Image
We can identify that the page has been posted back by using the “ Page ” class property called “ IsPostBack ” in the “ Page_Load ” method of the page. The property returns a “ Boolean ” value stating whether the page has been posted back or not based on a client request.