Tuesday, 30 December 2014

Properties For Regular Field Validations

Sometimes we have a need to apply Regular Field Validations(validations for format of entry to be filled in Text Box) in our Text Box. The properties which need to be set for Regular Field Validations are as follows.


<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"  ErrorMessage="Invalid Format" ControlToValidate="txt_emp_name" ForeColor="Red" ValidationExpression="^[a-zA-Z]+$ Display="Dynamic" SetFocusOnError="True" ValidationGroup="sub"></asp:RegularExpressionValidator>


Terms Used:
ErrorMessage:              Message to be displayed to show error.
SetFocusOnError:        So that cursor remains on the textbox which has error.
ValidationGroup:         It should be same as validation of button on whose click event action   on the textbox will be take.
ControlToValidate:      Name of Textbox on which validation is to be applied.                     

ForeColor:                   Color in which error is displayed.


Summary
In this article I try to expose the Regular Field Validation. 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.

Monday, 29 December 2014

AUTO-CAPS ON

Sometime we had a requirement that fields in TextBox should be written in capital letters(BLOCK Letters) no matter caps lock is on or not(Like in bank forms).
For this we can use following simple code. I am providing you two options by using javascript and without javascript.

Without Javascript:  

<asp:TextBox ID="TextBox1" runat="server" class="form-control" 
style= "text-transform:uppercase;"></asp:TextBox>


By Using Javascript:

In .aspx page

<script type="text/javascript">
function toUpper(txt)
{
document.getElementById(txt).value=document.getElementById(txt).value.toUpperCase();
return true; 
}
</script>

TextBox property

<asp:TextBox ID="TextBox1" onkeyup="return toUpper(this.id)" runat="server"></asp:TextBox>





Summary
In this article I try to expose CAPS/Upper Case property of TextBox . 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.

Friday, 26 December 2014

To expire session after some time and redirect your page to login page.

Sometime we had a requirement that if our page is not used for some time than session should be expire we should be redirected to login page. for this we can use following code.

Step 1=> In web.config page write...

<system.web>
<sessionState timeout="5" mode="InProc" cookieless="false">
</sessionState>
</system.web>

Step2=> In master page write(On Page Load)...

Response.AppendHeader("Refresh", Convert.ToString((Session.Timeout * 60)) + ";URL=Default.aspx");

Summary
In this article I try to expose the session expiration. 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.

Wednesday, 24 December 2014

Disable back button

Sometimes we want that our page should not go back to previous page. Or back button should be disabled after log out. This can be done by using following JavaScript.
Write this code on the page on which you want to disable back button.

<script type="text/javascript">
function noBack() { window.history.forward() }
noBack();
window.onload = noBack;
window.onpageshow = function (evt) { if (evt.persisted) noBack() }
window.onunload = function () { void (0) }
</script>

Summary
In this article I try to expose how to disable back button. 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.

Monday, 22 December 2014

Autocomplete-Off

Sometimes we want that data should not be filled automatically in the textbox, specially in the case of confidential data. 

We can turn off auto-fill for our complete form by setting autocomplete attribute value to off as shown below:

<form id="Form1" method="post" runat="server" autocomplete="off">
.
.
</form> 

We can also turn off auto-fill for a particular TextBox by setting autocomplete attribute value to off as shown below(on .aspx page):

<asp:TextBox Runat="server" ID="txtConfidential" autocomplete="off"></asp:TextBox>

We can also do this from code behind also like as(on .cs page):

txtConfidential.Attributes.Add("autocomplete", "off");


Summary

In this article I try to expose the TextBox Autocomplete-Off property. 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.

Saturday, 20 December 2014

Textbox color change(In case of validation error, mouse hover)

Sometimes we want the TextBox color to be changed on mouse hover or for showing validation errors. We can achieve this functionality by using following code.

Background Color Change on Mouse Hover In Textbox or Dropdown

·       Through javascript
In .aspx

<script type="text/javascript">
   
        function ChangeBackground() {
         window.event.srcElement.style.background = "yellow";
       }

       function RestoreBackground() {
   window.event.srcElement.style.background = "#FFFFFF";       }
  
       function CheckKey() {

         alert("The key code is" + window.event.keyCode);
         }
       
     </script>


In textbox properties(Add these) 

onmouseover="ChangeBackground();" 
onmouseout="RestoreBackground();"

In .cs page (on Pageload)

TextBox1.Attributes.Add("onmouseover","ChangeBackground();");
TextBox1.Attributes.Add("onmouseout","RestoreBackground();");




Border Color Change on Button click In Textbox

·       Through javascript

In .aspx
<script type="text/javascript">
        function DoClick(txtId) {
            var textbox = document.getElementById(txtId);
            if (textbox.value == "") {
                textbox.style.borderColor = '#ff0000';
            } 
              else {
                     textbox.style.borderColor = '#ffffff';
                   }
            }
 </script>


In .cs page (on Pageload)


Button1.OnClientClick = "DoClick('" + TextBox1.ClientID + "')";



Border Color Change when you keyup (textbox blank) In Textbox

Add this textbox

<asp:TextBox runat="server" style="border: 1px solid #7F9DB9" onkeyup="if(this.value=='')this.style.borderColor='#ff0000';
else this.style.borderColor='#ffffff';" Text="Type" />



Background Color Change when you focus on textbox


Add this into textbox property

onfocus = "this.style.background = '#99FF99'" // greencolor


Summary
In this article I try to expose the TextBox color change property. 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.

Friday, 19 December 2014

Disable Right Click

Disable right click on images

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

Note
         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.

   <html>
     <head>
      ...
      </head>
      <body oncontextmenu="return false;" >
      ...
      </body>
   </html>


Show alert on right click

   <script type="text/javascript">
  function disableRightClick()
  {
  alert("Sorry, right click is not allowed !!");
  return false;
  }
  </script>
  <body oncontextmenu=" return disableRightClick();">
  ...
</body>


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.