Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Friday, 10 February 2012

Javascript Value To Asp.net Page

Accessing HTML control/Javascript Function Value In Asp.net Page 

Some time situation comes where some Javascript effect/jquery or css make work on html control but not in .net control.
then what to do?Here is the solution to make html control interaction with asp.net control.So that there value can easily access by your asp.net control.



In Normal Cases

Just Make Input Text(html control) runat="server".

<input id="Text1"  runat="server" type="text" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
   

Now If You Want To transffer Text1 Value To Label1 Just Do This In Your C# Code(In Any Event)


Label1.Text =  Text1.Value;



But SomeTime We Have Fuctions Where We Have Value In Cloud And We Have To Catch Then On Fly.
------------------------------------------------------------------------------------------------------------------
So Here We Start...


Let say you've a html textbox with name txtbox and it has some value...so first thing is we have to save its value to a variable name it C.So that we are sure that the txtbox value is now in a variable and we can pass it to any one.

Next thing is ASP.NET has a control name hiddenControl ,just drag it in your design Page(Because Its Behave As A Mediator Between Html & .Net Control Easily) And finaly.just put that C value in hiddencontrol1



html Control----->var C----------->HiddenControl----->Now Value Is In Your ASP Page!





In HTML

For An Instance Lets Say You Are Firing An Client Side Event(javascript function) Like Below....

<html>
<script>
function yourown()
{
var C=document.getElementById("txtbox").value;
document.getElementById('<%=HiddenField1.ClientID%>').value=C;
return true;
}
 </script>
</html>




In C#

Now Understand The Below Code Where We Are Storing final Value Under HiddenField1 To A string In C# Page

String S=HiddenField1.Value;
Response.Write(S);//Show On Screen
//OR
Label1.Text=S; //Show On Label






Saturday, 19 November 2011

C# Validation for Email,Contact No and AutoText in TextBox



 Regex Validation
1)for email:

public static bool IsValidEmail(string Email)
      {
          return Regex.IsMatch(Email, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
      }


2)for phone no:
ex. 800-555-555

^[2-9]\d{2}-\d{3}-\d{4}$ 




3)Auto text




IN ASPX
  <head runat="server">
    <title></title>
  
    <script type = "text/javascript">
        var defaultText = "Enter your text here";
        function WaterMark(txt, evt) {
            if (txt.value.length == 0 && evt.type == "blur") {
                txt.style.color = "gray";
                txt.value = defaultText;
            }
            if (txt.value == defaultText && evt.type == "focus") {
                txt.style.color = "black";
                txt.value = "";
            }
        }
</script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
  
<asp:TextBox ID="TextBox1" runat="server" Text = "Enter your text here"

    ForeColor = "Gray" onblur = "WaterMark(this, event);"

    onfocus = "WaterMark(this, event);">

</asp:TextBox>

    </div>
    </form>
</body>




IN C#
 protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Attributes.Add("onblur", "WaterMark(this, event);");

        TextBox1.Attributes.Add("onfocus", "WaterMark(this, event);");
    }















Only INTEGER/./BACKSPACE Allowed



<script language="javascript" type="text/javascript" >
        function checkForInt(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode;
            return ((charCode ==08||charCode ==46 || charCode >= 48 && charCode <= 57));
        }
     
</script>







  <asp:TextBox ID="TextBoxMax" runat="server"
                        Font-Names="Tahoma" Font-Size="9pt" Width="150px"  onkeypress="return checkForInt(event)"></asp:TextBox>





charCode ==08 allow you to use backspace too.



Sunday, 6 November 2011

Common Javascript Solutions



only number validation
    <script>
function checkForInt(evt)
{
var charCode = ( evt.which ) ? evt.which : event.keyCode;
return ( charCode >= 48 && charCode <= 57 );
}
</script>


 onkeypress="return checkForInt(event)"

--------------------------------------------------


function checknum()
{
if((event.keyCode >=48 )&&(event.keyCode<=57))
{
return true;
}
else
{
alert("Please Enter Only Numeric Values..");
return false ;
}
}


onkeypress="return checknum();"


-------------------------------------------
function checklength(id)
{
if  (id.value.length <11)
{
return chknum();
}
else
{
alert("Mobile Number cant not be greater than 11 Digits:::");
return false;
}
}


-------------------------------------------------
function blank()
{
if(document.getElementById("<%=TextBox1.ClientID %>").value=="")
{
alert("Cant be Blank not be blank!");
document.getElementById("<%=TextBox1.ClientID %>").focus();
return false ;
}
return true;
}







-----------------------------------------------------------------
               Email Validations 
function email()
{


  var at="@"
  var dot="."
  
  var str=document.getElementById("<%=EmailIDTextBox.ClientID%>").value;
  var lstr=str.length;
  var lat=str.indexOf(at);
  var ldot=str.indexOf(dot);
  var pat=/^.+@.+\..{2,3}$/;
  
  if(str.indexOf(at)==-1 ||str.indexOf(at)==0 ||str.indexOf(at)==lstr)
  {
  alert("Invalid EmailId..")
  document.getElementById("<%=EmailIDTextBox.ClientID %>").focus();
  return false;
  }
  if(str.indexOf(dot)==-1||str.indexOf(dot)==0||str.indexOf(dot)==lstr )
  {
  alert("Invalid EmailId..")
  document.getElementById("<%=EmailIDTextBox.ClientID %>").focus();
  return false;
  }
  


return true ;
}


                                   or  






Email Validation
 if(document.getElementById("<%=EmailTextBox.ClientID%>").value=="")
     {
      alert("Please Enter EMail Id....")
      return false
     }
     else
      {
      var at="@"
      var dot="."
      var str=document.getElementById("<%=EmailTextBox.ClientID%>").value;
      var lstr=str.length;
      var lat=str.indexOf(at);
      var ldot=str.indexOf(dot);
      var pat=/^.+@.+\..{2,3}$/;

      if(str.indexOf(at)==-1 ||str.indexOf(at)==0 ||str.indexOf(at)==lstr)
      {
      alert("Invalid Email Id.. Please Try Again..")
      return false;
      }
      if(str.indexOf(dot)==-1||str.indexOf(dot)==0||str.indexOf(dot)==lstr )
      {
      alert("Invalid Email Id.. Please Try Again..")
      return false;
      }
     }



                         (both are same)


----------------------------------


function date()
{
if(document.getElementById("<%=.ClientID %>").value=="")
{
alert("Please Select The Date From Calendar");


return false ;
}
return true;
}

Print Only Grid View in ASP.net

ASP.net How to Print Only GridView < div id ="gridviewDiv" >   < asp:GridView ID ="gridViewToPri...