Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Thursday, April 4, 2013

Capturing the Enter key to cause a button click


Let's explore two solutions to this problem. First, let us look at using JavaScript. With this solution, each textbox needs to have some JavaScript attached to the onKeyPress JavaScript event. I usually do this in the pageload method in the code-behind.

if (!IsPostBack)
{
   txtboxFirstName.Attributes.Add("onKeyPress",
                   "doClick('" + btnSearch.ClientID + "',event)");
 
}


Note: You pass into the JavaScript method the button's ClientID so that the JavaScript method can find the button and call its Click method.

Next, we need a JavaScript method called doClick in the ASP.NET form.

<SCRIPT type=text/javascript>
    function doClick(buttonName,e)
    {
        //the purpose of this function is to allow the enter key to
        //point to the correct button to click.
        var key;

         if(window.event)
              key = window.event.keyCode;     //IE
         else
              key = e.which;     //firefox
   
        if (key == 13)
        {
            //Get the button the user wants to have clicked
            var btn = document.getElementById(buttonName);
            if (btn != null)
            { //If we find the button click it
                btn.click();
                event.keyCode = 0
            }
        }
   }
</SCRIPT>


The next solution is using a Panel control. In this case, the panel is doing all the work.

 Collapse | Copy Code
<asp:Panel ID="panSearch" runat="server"
       DefaultButton="btnSearch" Width="100%" >
    <table width="100%">
   
    <tr>
     <td>First Name</td>
     <td ><asp:TextBox ID="txtboxFirstName"
           runat="server" ></asp:TextBox></td>
    </tr>
    ...

Friday, December 14, 2012

How to Print webpage ?

If you want to print same page then just write the javascript.


   function print() {
                window.print();
        }

if you want to print another webpage from the current page then write below scrpt



   function print(var str) {
               printWindow = window.open(  str,"mywindow");
               setTimeout('printWindow.print()', 3000);
               setTimeout('printWindow.close()', 3000);

        }

Tuesday, December 11, 2012

How to add JavaScript at runtime using C#

string Script=@”function ShowName() { var txt=document.getElementById(‘txtName’); alert(‘Your name is: ‘+txt.value); }”; 

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), “ShowName”, Script, true);

Monday, May 7, 2012

Call javascript Function from aspx.cs page..

.aspx page

<script type="text/javascript">
        function alertMe() {
            alert('Hello');
        }
    </script>

aspx.cs Page

 if (!ClientScript.IsStartupScriptRegistered("alert"))
 {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alertMe();", true);
}

Friday, September 9, 2011

how to resize the textbox height dynamically or how to change or increase the textbox height dynamically based on text using javascript








Introduction:

In this Post I will explain how to  increase the textbox height dynamically based on text writing into text box.

Description:

I have one textbox the requirement is like if any user enters text in texbox that height of textbox needs to be adjusting with text.I have written one JavaScript function to adjust textbox height here one more thing is that we have  to set the TextMode="MultiLine" property for textbox to adjust textbox height

Lets see the aspx Page


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Automatic Resize TextBox</title>
<script type="text/javascript">
function SettingHeight(txtdesc) {
txtDescription.style.height = txtDescription.scrollHeight + "px";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtDescription" runat= "server" TextMode="MultiLine" onkeyup="SettingHeight(this);"onkeydown="SettingHeight(this);" />
</form>
</body>
</html>


Demo