HI WELCOME TO SIRIS
Showing posts with label ASP.net. Show all posts
Showing posts with label ASP.net. Show all posts

Difference between Response.Redirect and Server.Transfer

In ASP.Net Technology both "Server" and "Response" are objects of ASP.Net. Server.Transfer and Response.Redirect both are used to transfer a user from one page to another. But there is some remarkable differences between both the objects which are as follow.

Response.Redirect

  1. Response.Redirect() will send you to a new page, update the address bar and add it to the Browser History. On your browser you can click back.
  2. It redirects the request to some plain HTML pages on our server or to some other web server.
  3. It causes additional roundtrips to the server on each request.
  4. It doesn’t preserve Query String and Form Variables from the original request.
  5. It enables to see the new redirected URL where it is redirected in the browser (and be able to bookmark it if it’s necessary).
  6. Response. Redirect simply sends a message down to the (HTTP 302) browser.

Server.Transfer

  1. Server.Transfer() does not change the address bar, we cannot hit back.One should use Server.Transfer() when he/she doesn’t want the user to see where he is going. Sometime on a "loading" type page.
  2. It transfers current page request to another .aspx page on the same server.
  3. It preserves server resources and avoids the unnecessary roundtrips to the server.
  4. It preserves Query String and Form Variables (optionally).
  5. It doesn’t show the real URL where it redirects the request in the users Web Browser.
  6. Server.Transfer happens without the browser knowing anything, the browser request a page, but the server returns the content of another.
What do you think?
In this article I have tried to explain the difference between Response.Redirect and Server.Transfer also I explain some scenario when and where we should use each one. i hope the article will be helpful for you. Comments and criticize will be welcome.

Turn off Asp.net Custom Errors in Web.config


Some times after hosting web application on the server, we get unexpected error as shown in the below fig. But we did get the detailed message for the unexpected errror. In this article, I would like to share how can we get detailed message for the unexpected error.

This type of unexpected error may occurs on local or remote server. In asp.net, we can find the exact error message by setting mode="Off" with in customErrors tag in web.config of our application. This is the way by which we can find out the exact error in our web application.
  1. <system.web>
  2. <customErrors mode="Off">
  3. </customErrors>
  4. ...
  5. ...
  6. </system.web>
When we set the customErrors mode="Off" then we can easily track the error in the application as shown in the fig.

In Asp.net, there are three error modes to trace an error. These modes decide whether or not an error message is displayed. RemoteOnly mode is default mode for displaying error messages.
  1. Off Mode

    This mode is responsible for displaying error mesage on local and remote server in case of an error.
  2. On Mode

    This mode is responsible for displaying custom error page with message on local and remote server in case of an error. By using this mode, we can show our own custom error messages page for specific errors on local and remote server.
  3. RemoteOnly

    This mode is responsible for displaying error mesage on remote server only in case of an error. By using this mode, we can show our own custom error messages page for specific errors on remote server only.
What do you think?
I hope you will enjoy these tricks while programming with Asp.Net. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Disable right click on web page and images


Sometimes it is required to prevent your web page images from being copied by another one. You can secure your images by disabling right click or by disabling image context menu on images only. This is an effective approach to prevent your images from being copied or stolen. Here I am going to share the tricks how can you achieve this. This trick will disable right click only on images. Add the below event handler to the img or image tag as shown below:

Disable right click on images

  1. <asp:Image ID="img1" runat="server" ImageUrl=""../ImgLoc/1.png"" oncontextmenu="return false;" />
  2. <img alt="MyImage" src="../ImgLoc/2.png" oncontextmenu="return false;"/>

Note

  1. It is impossible to prevent images from being stolen completely since there are so many tools and software through which any one can steal yours images. But something is better than nothing.

Disable right click on web page

Similarly we can disable right click on whole page by adding oncontextmenu handler in body tag of webpage. Now this will disable right click on each and every control of a webpage.
  1. <html>
  2. <head>
  3. ...
  4. </head>
  5. <body oncontextmenu="return false;" >
  6. ...
  7. </body>
  8. </html>

Show alert on right click

  1. <script type="text/javascript">
  2. function disableRightClick()
  3. {
  4. alert("Sorry, right click is not allowed !!");
  5. return false;
  6. }
  7. </script>
  8. <body oncontextmenu=" return disableRightClick();">
  9. ...
  10. </body>
Summary
In this article, I explain how can you disable right click on webpage and images. I hope after reading this article you will be able to do this. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

Stop auto-fill in browsers for textbox

Today’s browsers like Chrome, Firefox, Internet Explorer and Safari has functionality of auto complete values in TextBoxes. If you have enabled this features in your browser, then each and every time when you start to enter value in TextBox you get a drop down of prefilled values in that TextBox. This feature of browser can be disabled by the programming for a specific web form like payment form and other confidential information form of a web application.
In chrome browser, we can enable auto-fill as shown below:

Suppose we have a below form for online payment of product by credit card or debit card then it is mandatory to stop auto complete functionality of browser so that browser doesn’t save the confidential information of a customer’s credit card or debit card.

We can turn off auto-fill for our complete form by setting autocomplete attribute value to off as shown below:
  1. <form id="Form1" method="post" runat="server" autocomplete="off">
  2. .
  3. .
  4. </form>
We can also turn off auto-fill for a particular TextBox by setting autocomplete attribute value to off as shown below:
  1. <asp:TextBox Runat="server" ID="txtConfidential" autocomplete="off"></asp:TextBox>
We can also do this from code behind also like as:
  1. txtConfidential.Attributes.Add("autocomplete", "off");
After doing one of above code you will see that there is no auto-fill.

Summary
In this article, I explain how can you stop auto-complete in TextBox by programming. I hope you will use this trick in your web form. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

Difference between disabled and read only attributes

Sometimes, we required to display form fields in non-editable mode. We can achieve this functionality by setting read-only or disabled attribute to form fields (textbox, label, checkbox, text area). Before using both these attributes, we should understand the key differences between read only and disabled attributes.

Disabled attribute

  1. Disabled form fields or elements values don’t post to the server for processing.
  2. Disabled form fields or elements don’t get focus.
  3. Disabled form fields or elements are skipped while tab navigation.
  4. Some browsers (Like IE) provide default style (Gray out or emboss text) for disabled form fields or elements.

Read Only Attribute

  1. Read Only form fields or elements values post to the server for processing.
  2. Read Only form fields or elements get focus.
  3. Read Only form fields or elements are included while tab navigation.
  4. Some browsers do not provide default style for Read-Only form fields or elements.

Note

  1. We can’t set readonly attribute to all the form fields or elements. The <SELECT> , <OPTION> , and<BUTTON> elements do not have readonly attributes while they can have disabled attributes.
  2. In asp.net, when you changed the value of ReadOnly Textbox with the help of javascript or jquery then on server side you will get the oldest value not the changed value. To get changed value from ReadOnly TextBox, make the TextBox Read-Only from code-behind by adding readonly attribute to TextBox like as txtReadonly.Attibutes.Add("readonly","readonly");

Set Disabled Attribute

ASP.NET TextBox
  1. <asp:TextBox ID="txtDisabled" Enabled="false" runat="server"></asp:TextBox>
HTML Input
  1. <input name="txtDisabled" type="text" id="txtDisabled" disabled="disabled"/>

Set Read Only Attribute

ASP.NET TextBox
  1. <asp:TextBox ID="txtReadonly" ReadOnly="true" runat="server"></asp:TextBox>
HTML Input
  1. <input name="txtReadonly" type="text" id="txtReadonly" readonly="readonly"/>

CSS For read only and disabled attribute input element

Since some browser don’t provide default style for disabled element. Hence to display disabled elements on all browsers in same look and feel, we can use below css.
  1. input[disabled]{background-color:#F0F0F0; color:#303030;}
Similarly we can change the look and feel of readonly element by using below css.
  1. input[readonly]{background-color:#F0F0F0 ;color:#303030 }
Summary
In this article I try to expose the disabled and readonly attributes. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

Do proper alignment of validator control's error messages using Display property


In Asp.net, sometimes we need to validate a TextBox and its input by more than one validator controls. Suppose you Want to validate a TextBox that must contain a valid Email address then we required two validator controls - Required Field and Regular Expression as shown below:

Validator without Display Property (By default Display is Static)

When we doesn’t set the display property of both the below validator controls then error message of second validator control will display on the page just after the space that is taken by first validator error message as shown below :
  1. <form id="form1" runat="server">
  2. <div>
  3. Enter Email ID : <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
  4. <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="txtEmail" runat="server" ForeColor="Red" ErrorMessage="Please Enter Email ID"></asp:RequiredFieldValidator>
  5. <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmail" ForeColor="Red" ErrorMessage="Please Enter Correct Email ID"></asp:RegularExpressionValidator>
  6. </div>
  7. <br />
  8. <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
  9. </form>


Validator with Display Property

When we set the display property of both the below validator controls then error message of second validator control will display on the page just after the TextBox as shown below :
  1. <form id="form1" runat="server">
  2. <div>
  3. Enter Email ID : <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
  4. <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="txtEmail" runat="server" Display="Dynamic" ForeColor="Red" ErrorMessage="Please Enter
  5. Email ID"></asp:RequiredFieldValidator>
  6. <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmail" Display="Dynamic" ForeColor="Red" ErrorMessage="Please
  7. Enter Correct Email ID"></asp:RegularExpressionValidator>
  8. </div>
  9. <br />
  10. <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
  11. </form>

Summary
In this article I try to explain how can you align the error messages of validator controls properly using Display property. I hope after reading this article you will be able to do proper alignment of your validator control's error messages. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.