Archive

Archive for the ‘JavaScript’ Category

GridView Data Print In Asp.net 2.0

July 26, 2008 73 comments

here is the steps:

1) create simple web application (here i used asp.net with C#).

2) put following control in to Page.

1) Gridview (id = GridView1)

2) html button (id =Button1)

<inputid=”Button1″type=”button”value=”Print”language=”javascript”onclick=”return Button1_onclick()” />
<
asp:GridViewID=”GridView1″runat
=”server”>
</
asp:GridView>

3)  fill data into GridView

//here i used the list you  can bind with database also
System.Collections.Generic.List<string> obj = newSystem.Collections.Generic.List<string>();

for(inti = 0; i < 10; i++)
{
obj.Add(i.ToString());
}
GridView1.DataSource = obj;
GridView1.DataBind();
4) now write the print function. click on the Button1 or write Java Script.

<script language="javascript" type="text/javascript">
<!--
function Button1_onclick() {
//open new window set the height and width =0,set windows position at bottom
var a = window.open ('','','left =' + screen.width + ',top=' + screen.height + ',width=0,height=0,toolbar=0,scrollbars=0,status=0');
//write gridview data into newly open window
a.document.write(document.getElementById('<%= GridView1.ClientID %>').innerHTML);
a.document.close();
a.focus();
//call print
a.print();
a.close();
return false;
}
// -->
</script>

Thanks

Disabled Right Click On Web Site

July 4, 2008 7 comments

steps :

1) create simple application (html).

2) put the below javascript in to the code.

<script language="JavaScript">
      // on right click appear this message
      var DisplayMsg="Right Click Disabled";
      //return message
      function clickBYIE()
      {
          if(document.all)
          {
              alert(DisplayMsg);
              return false;
          }
      }
      //return message
      function clickBYNS(e)
      {
          if (document.layers||(document.getElementById&&!document.all))
          {
              if (e.which==2||e.which==3)
              {
               alert(DisplayMsg);
               return false;
              }
          }
      }

     //set the function as per browser
      if (document.layers)
      {
          document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickBYNS;
      }
      else
      {
          document.onmouseup=clickBYNS;document.oncontextmenu=clickBYIE;
      }
      //disabled the right click on the browser
       document.oncontextmenu=new Function("return false")
  </script>
run the code and check.
thnx

Window Resize Using JavaScript

April 19, 2008 Leave a comment

steps :

1) Create the Simple Web Application.

2) in the body written below function for resize the window.

<body onload="Maximize();">

3) in the JavaScript put the below code.

<script language=”javascript”>
function Maximize()
{
//move the window to 0,0 co-ordinate
window.moveTo(0,0);
//width is the screen width but minus 25 from the height
//reson to minus 25 Taskbar
//resize the window
window.resizeTo(screen.width,screen.height-25);
}
</script>

Thanks.

HightLight Error Message In JavaScript

April 16, 2008 Leave a comment

Steps:

1) create simple web application (here i am use the asp.net 2.0).

2) In that application put the below code in the designer view.

<table>
<
tr
>
<
td
>
Enter User Name
</td
>
<
td
>
<
asp:TextBoxID=”txtUserName”runat=”server”onblur=”return HighLightErrorMsg(this.id,’lblUserError’,'red’);”></asp:TextBox></td
>
<
td
>
<
divstyle=”visibility: hidden; background-color: Red; width: 200px;”id
=”lblUserError”>
Please Enter UserName
</div
>
</
td
>
</
tr
>
<
tr
>
<
td
>
Enter Password
</td
>
<
td
>
<
asp:TextBoxID=”txtPassword”runat=”server”onblur=”return HighLightErrorMsg(this.id,’lblPassword’,'red’);”></asp:TextBox
>
</
td
>
<
td
>
<
divstyle=”visibility: hidden; background-color: Red; width: 200px;”id
=”lblPassword”>
Please Enter Password
</div
>
</
td
>
</
tr
>
<
tr
>
<
tdcolspan
=”2″>
<
center
>
<
asp:ButtonID=”btnSubmit”runat=”server”OnClick=”btnSubmit_Click”Text
=”Submit”
OnClientClick
=”return CheckControl();” />
</
center
>
</
td
>
</
tr
>
</
table>

3) In that page write below javascript.

<script language="javascript">

  function CheckControl()
{
//get the id of username textbox
var objUserName=document.getElementById('txtUserName').id;
//get the id of username error message
var objUserError =document.getElementById('lblUserError').id;
//get the id of password textbox
var objPassword =document.getElementById('txtPassword').id;
//get the id of password error message
var objPasswordError =document.getElementById('lblPassword').id;
//check control and highlight message
if(HighLightErrorMsg(objUserName,objUserError,'red' )==false)
{
return false;
}
else if(HighLightErrorMsg(objPassword,objPasswordError,'red')==false)
{
return false;
}
return true;
}

//id-->control to validate
//errorid --> div to display error message
//color --> error color
function HighLightErrorMsg(id,errorid,color)
{
//check the control value
if(document.getElementById(id).value !="")
{
//hide the error message
document.getElementById(errorid).style.visibility ="hidden";
return true;
}
var bkcolor=color;
if(bkcolor=='red')
bkcolor ='green';
else
bkcolor ='red';
//set the background color
document.getElementById(errorid).style.backgroundColor =bkcolor;
//visibility set
document.getElementById(errorid).style.visibility ="visible";
//if error at that time focus on the particular control
document.getElementById(id).focus();
//every 5 sec call and change the background color
window.setTimeout("HighLightErrorMsg('"+ id + "','"+ errorid +"','"+bkcolor+"');",500);
return false;
}

</script>

Thanks

Follow

Get every new post delivered to your Inbox.

Join 244 other followers