Yasserzaid’s Weblog

November 2, 2009

Close Popup window When Press Esc Key

Filed under: ASP.Net, Javascript — Tags: — yasserzaid @ 8:17 pm

Hi

try this Example to Close Popup window When Press Esc Key using Javascript

<script type=”text/javascript” language=”javascript”>
        function doClose(e) // note: takes the event as an arg (IE doesn’t)
        {
            if (!e) e = window.event; // fix IE

            if (e.keyCode) // IE
            {
                if (e.keyCode == “27″) window.close();
            }
            else if (e.charCode) // Netscape/Firefox/Opera
            {
                if (e.keyCode == “27″) window.close();
            }
        }
        document.onkeypress = doClose;
</script>

Hope this helps

Good Luck

October 14, 2009

Disable and Restrict Copy Cut and Paste from on a Textbox

Filed under: ASP.Net, Javascript — yasserzaid @ 4:08 pm

Hi

try this example to Disable and Restrict Copy Cut and Paste from on a textbox using Javascript

You will need two javascript functions for this:

    function noCopyMouse(e) {
        var isRight = (e.button) ? (e.button == 2) : (e.which == 3);
       
        if(isRight) {
            alert(‘You are prompted to type this twice for a reason!’);
            return false;
        }
        return true;
    }

    function noCopyKey(e) {
        var forbiddenKeys = new Array(‘c’,'x’,'v’);
        var keyCode = (e.keyCode) ? e.keyCode : e.which;
        var isCtrl;

        if(window.event)
            isCtrl = e.ctrlKey
        else
            isCtrl = (window.Event) ? ((e.modifiers & Event.CTRL_MASK) == Event.CTRL_MASK) : false;
   
        if(isCtrl) {
            for(i = 0; i < forbiddenKeys.length; i++) {
                if(forbiddenKeys[i] == String.fromCharCode(keyCode).toLowerCase()) {
                    alert(‘You are prompted to type this twice for a reason!’);
                    return false;
                }
            }
        }
        return true;
    } 
 
And a wee bit of code-behind to handle the two events for the textbox(es):

Textbox1.Attributes.Add(“onmousedown”, “return noCopyMouse(event);”)
 Textbox1.Attributes.Add(“onkeydown”, “return noCopyKey(event);”)

 Hope this helps

Good Luck

October 3, 2009

Dynamic font size change using javascript

Filed under: ASP.Net, Javascript — Tags: — yasserzaid @ 3:05 pm

Hi

try this example to change font size dynamically using javascript:-

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
    <title>Change Font Size on the fly</title>
    <script type=”text/javascript”>
    <!–
        var min=12;
        var max=24;
        function changeFontSize(_chngValue) {
           var p = document.getElementsByTagName(‘p’);
           for(i=0;i<p.length;i++) {
              var s =p[i].style.fontSize?parseInt(p[i].style.fontSize.replace(“px”,”")):min;
              if(_chngValue==”higher”){
                if(s!=max)s += 1;
              }
              else if(_chngValue==”lower”){
                if(s!=min)s -= 1;
              }
              p[i].style.fontSize = s+”px”;
           }
        }
    //–>
    </script>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <div>
    <p>Any Test Here</p>
    <input onclick=”changeFontSize(‘higher’);” type=”button” value=”+”  />
    <input onclick=”changeFontSize(‘lower’);”  type=”button” value=”-” />
    </div>
    </form>
</body>
</html>

Hope this helps

Good Luck

Make a DropdownList visible using javasript

Filed under: ASP.Net, Javascript — yasserzaid @ 12:59 am

Hi

try this example to Make a DropdownList visible using javasript :-

<html xmlns=”http://www.w3.org/1999/xhtml“>
<head runat=”server”>
    <title>Show DDL</title>
    <script type=”text/javascript”>
    function showDropDown(){
        document.getElementById(‘<%=DropDown1.ClientID %>’).style.display=’block’;
        document.getElementById(‘<%=DropDown2.ClientID %>’).style.display=’block’;
        return false;   
    }
    </script>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <div>
    <asp:DropDownList ID=”DropDown1″ runat=”server” style=”display:none;”></asp:DropDownList>
    <br />
    <asp:DropDownList ID=”DropDown2″ runat=”server” style=”display:none;”></asp:DropDownList>
    <br />
    <input type=”button” onclick=”return showDropDown();” value=”ShowDDL” />
    </div>
    </form>
</body>
</html>

Hope this helps

Good Luck

October 2, 2009

Limit length of text in Gridview BoundField

Filed under: ASP.Net, Javascript — yasserzaid @ 3:48 pm

Hi

try this example to Limit length of text in BoundField :-

<script type=”text/javascript”> 
function isMaxLen(o){ 
 var nMaxLen=o.getAttribute? parseInt(o.getAttribute(“maxlength”)):”"; 
 if(o.getAttribute && o.value.length>nMaxLen){ 
 o.value=o.value.substring(0,nMaxLen) 
 } 

</script>

<div>
        <asp:SqlDataSource ID=”SqlDataSource1″ runat=”server” ConnectionString=”<%$ ConnectionStrings:ConnectionString %>”
            DeleteCommand=”DELETE FROM [country] WHERE [countryid] = @countryid” InsertCommand=”INSERT INTO [country]

([countryid], [countryname]) VALUES (@countryid, @countryname)”
            SelectCommand=”SELECT * FROM [country]” UpdateCommand=”UPDATE [country] SET [countryname] = @countryname WHERE

[countryid] = @countryid”>
            <DeleteParameters>
                <asp:Parameter Name=”countryid” Type=”Int64″ />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name=”countryname” Type=”String” />
                <asp:Parameter Name=”countryid” Type=”Int64″ />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name=”countryid” Type=”Int64″ />
                <asp:Parameter Name=”countryname” Type=”String” />
            </InsertParameters>
        </asp:SqlDataSource>
        <asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False” DataKeyNames=”countryid”
            DataSourceID=”SqlDataSource1″ OnRowDataBound=”GridView1_RowDataBound”>
            <Columns>
                <asp:CommandField ShowEditButton=”True” />
                <asp:BoundField DataField=”countryid” HeaderText=”countryid” ReadOnly=”True” SortExpression=”countryid” />
                <asp:BoundField DataField=”countryname” HeaderText=”countryname” SortExpression=”countryname” />
            </Columns>
        </asp:GridView>
   
    </div>

and in code behind :-

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Edit)
            {
                TextBox work =  e.Row.Cells[2].Controls[0] as TextBox;
                work.Attributes.Add(“onkeydown”, “isMaxLen(this)”);
                work.Attributes.Add(“maxlength”,”9″);
            }
        }
    }

Hope this helps

Good Luck

September 20, 2009

Printing DIV Contents using Print Preview

Filed under: ASP.Net, Javascript — yasserzaid @ 3:14 pm

Hi

try this example to Print DIV Contents using Print Preview

<form id=”Form1″ method=”post” runat=”server”>
 <div id=”Div1″>
  Printable content
 </div>
 <input type=”button” value=”Print” onclick=”JavaScript:printPartOfPage(‘Div1′);”>

<script type=”text/javascript”>
<!–
function printPartOfPage(elementId)
{
 var printContent = document.getElementById(elementId);
 var windowUrl = ‘about:blank’;
 var uniqueName = new Date();
 var windowName = ‘Print’ + uniqueName.getTime();
 var printWindow = window.open(windowUrl, windowName, ‘left=50000,top=50000,width=0,height=0′);

 printWindow.document.write(printContent.innerHTML);
 printWindow.document.close();
 printWindow.focus();
 printWindow.print();
 printWindow.close();
}
// –>
</script>

</form>
//—— another way
<form id=”Form1″ method=”post” runat=”server”>
 <div id=”Div1″>
  Printable content
 </div>
 <input type=”button” value=”Print” onclick=”JavaScript:printPreviewDiv(‘Div1′);”>

 <script type=”text/javascript”>
 <!–
 function printPreviewDiv(elementId)
 {
  var printContent = document.getElementById(elementId);
  var windowUrl = ‘about:blank’;
  var uniqueName = new Date();
  var windowName = ‘Print’ + uniqueName.getTime();
  var printWindow = window.open(windowUrl, windowName, ‘left=50000,top=50000,width=0,height=0′);
  var printPreviewObject = ‘<object id=”printPreviewElement” width=”0″ height=”0″ classid=”CLSID:8856F961-340A-11D0-A96B-00C04FD705A2″></object>’;

  printWindow.document.write(printContent.innerHTML);
  printWindow.document.write(printPreviewObject);
  printWindow.document.write(‘<script language=JavaScript>’);
  printWindow.document.write(‘printPreviewElement.ExecWB(7, 2);’);
  printWindow.document.write(‘printPreviewElement.outerHTML = “”;’);
  printWindow.document.write(‘</script>’);
  printWindow.document.close();
  printWindow.focus();
  printWindow.close();
 }
 // –>
 </script>
</form>

 Hope this helps

Good Luck

September 1, 2009

Show Popup Calender in DIV

Filed under: ASP.Net, Javascript — yasserzaid @ 9:56 pm

Hi

try this example to show Popup calender in DIV using Javascript

<div id=”dateField” style=”display:none;”>
  <asp:Calendar id=”calDate”
  OnSelectionChanged=”calDate_SelectionChanged”
  Runat=”server” />
</div>

<asp:TextBox id=”txtDate” Runat=”server” />
<img src=”cal.png” onclick=”popupCalendar()” />

<script type=”text/javascript”>
  function popupCalendar()
  {
    var dateField = document.getElementById(‘dateField’);

    // toggle the div
    if (dateField.style.display == ‘none’)
        dateField.style.display = ‘block’;
    else
        dateField.style.display = ‘none’;
  }
</script>
in code behind ;-

protected void calDate_SelectionChanged(object sender, EventArgs e)
{
  txtDate.Text = calDate.SelectedDate.ToString(“d”);
}

Hope this helps

Good Luck

August 31, 2009

Disable a Button When Clicked once on an ASP.NET Page Containing Validation Controls

Filed under: ASP.Net, Javascript — yasserzaid @ 11:45 am

Hi

try this example to Disable a Button When Clicked once on an ASP.NET Page Containing Validation Controls

<asp:Button ID=”LoginButton” runat=”server” Text=”Login” OnClientClick=”DisableButton(this)” />
<span id=”loading” style=”display: none;”>Logging in…</span>

<script language=”javascript” type=”text/javascript”>
function DisableButton(button)
{
    Page_ClientValidate();
    if (Page_IsValid)
    {
        button.style.display = ‘none’;
        document.getElementById(‘loading’).style.display = ‘inline’;
    }
}
</script>

 Hope this helps

Good Luck

August 16, 2009

Show Dynamic DIV as Tooltip

Filed under: ASP.Net, Javascript — yasserzaid @ 7:44 pm

Hi

try this example :-

JAVASCRIPT CODE

function ShowDiv(divid,iframe, state)
{
 var DivRef = document.getElementById(divid);
 var IfrRef = document.getElementById(iframe);
 if(state)
 {
  DivRef.style.display = “block”;
  IfrRef.style.width = DivRef.offsetWidth;
  IfrRef.style.height = DivRef.offsetHeight;
  IfrRef.style.top = DivRef.style.top;
  IfrRef.style.left = DivRef.style.left;
  IfrRef.style.zIndex = DivRef.style.zIndex – 1;
  IfrRef.style.display = “block”;
 }
 else
 {
  DivRef.style.display = “none”;
  IfrRef.style.display = “none”;
 }
}

<a href=”#” onclick=”ShowDiv(‘div’,'iframe’, true); return false;”>
<img src=”image.gif” border=”0″ style=”margin: 0px;” /></a>
<div id=”div”>div message</div>
<iframe id=”iframe” scrolling=”no” frameborder=”0″></iframe>

 Hope this helps

Good Luck

August 14, 2009

Disable a button after single click

Filed under: ASP.Net, Javascript — yasserzaid @ 11:55 pm

Hi

try this example to disable button after single click using javascript

<asp:button id=”submitButton” runat=”server” text=”Submit” OnClientClick=”return buttonOnClick(this);”></asp:button>

<script type=”text/javascript”>
<!–
function buttonOnClick(elementRef)
{
 document.body.style.cursor = ‘wait’;
 var isValid = true;
 if ( typeof(Page_ClientValidate) == ‘function’ )
  isValid = Page_ClientValidate(”);
 if ( isValid == true )
 {
  elementRef.value=’Please wait…’;
  elementRef.disabled = true;
  <%= ClientScript.GetPostBackEventReference(submitButton, string.Empty) %>;
 }
 return isValid;
}
// –>
</script>

Hope this helps

Good Luck

Older Posts »

Blog at WordPress.com.