Wednesday, June 15, 2011

Disabling cut, copy, paste, right click and context menu in a textbox on aspx page

In this tutorial you will learn how to disable cut, copy, paste, right click and context menu in asp:textbox. Sometimes due to security point of view you may want to do this. It's quite simple, I often see that developers write javascript code to disable the cut, copy, paste in textbox either textbox is input or server side asp:textbox.
I will not use any single line of javascript code to achieve what I want. Now let's have a look over how to do so.

Disabling cut, copy, paste, right click and context menu in a textbox

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Create div element dynamically using javascript</title>
<style type="text/css">
.myDivClass
{
color: #999;
padding: 10px;
width: 250px;
height: 150px;
border: solid 1px #666;
}
</style>
<script type="text/javascript" language="javascript">
function CreateDivElement() {
var divElement = document.createElement("div");
divElement.id = "myDiv";
divElement.className = "myDivClass";
divElement.innerHTML = "Hello World!";
document.body.appendChild(divElement);
}
function disableCopy() {
    alert("You cannot perform Copy");
    return false;
}
function disablePaste() {
    alert("You cannot perform Paste");
    return false;
}
function disableCut() {
    alert("You cannot perform Cut");
    return false;
}
function disableContextMenu() {
    alert("You cannot perform right click via mouse as well as keyboard");
    return false;
}
 
</script>
</head>
<form runat="server" id="form1">
<body>
<div>
<input id="myBtn" type="button" value="Click Me" onclick="CreateDivElement();" />
<asp:TextBox ID="TextBox1" runat="server" oncopy="return false" onpaste="return false" oncut="return false" oncontextmenu="return false">  
</asp:TextBox>  
<asp:TextBox ID="txtName" runat="server" oncopy="return disableCopy()" onpaste="return disablePaste()" oncut="return disableCut()" oncontextmenu="return disableContextMenu()">  
</asp:TextBox>  
</div>
</body>
</form>
</html>

output:


 copy

learn how to create div element dynamically by using javascript

In this tutorial you will learn how to create div element dynamically by using javascript. We will do this by utilizing the DOM features of javascript. Let's have a look over example given below

Create div element dynamically by using javascript

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Create div element dynamically using javascript</title>
<style type="text/css">
.myDivClass
{
color: #999;
padding: 10px;
width: 250px;
height: 150px;
border: solid 1px #666;
}
</style>
<script type="text/javascript" language="javascript">
function CreateDivElement() {
var divElement = document.createElement("div");
divElement.id = "myDiv";
divElement.className = "myDivClass";
divElement.innerHTML = "Hello World!";
document.body.appendChild(divElement);
}
</script>
</head>
<body>
<div>
<input id="myBtn" type="button" value="Click Me" onclick="CreateDivElement();" />
</div>
</body>
</html>

And if you want to want to create div element inside any other html element of page body then nothing to worry. Let's suppose we have a div in our page which id is testDivlike given below
<div id="testDiv">
</div>
and you want to create div inside this testDiv. Just you have to change the last line of CreateDivElement() function from
document.body.appendChild(divElement);
to
document.getElementById('testDiv').appendChild(divElement);
and that's it. I hope you will like this tutorial.

How to capture web page screenshot in asp.net using c#

In this tutorial you will learn how to capture webpage screenshot in asp.net using c#. You may got a requirement to capture the screenshot when user submits the form , etc. Most of the people purchase third party dll files and use them in their project to capture the screenshot of webpage. It is quite easy in asp.net and there is no need to purchase any dll. Let's have a look over how to do so.

How to capture web page screenshot in asp.net using c#

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Web.Services;
using System.Text;
using System.Windows.Forms;//must be included for capturing screenshot
public partial class capture_webpage_screenshot : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        Graphics graphics = Graphics.FromImage(bitmap as System.Drawing.Image);
        graphics.CopyFromScreen(25, 25, 25, 25, bitmap.Size);
        bitmap.Save(@"c:\screenshot\myscreenshot.bmp", ImageFormat.Bmp);
       
    }
}

The code is ready. Just you have to do the following steps.


  1. Right click the project name
  2. Add reference -> .net tab and then choose system.windows.forms namespace then press ok button.
  3. In .aspx code behind file you have to import the System.Windows.Formsnamespace and that’s it.
By customizing parameters used in CopyFromScreen() function, you can figure out how to capture the small portion of the screen, if required. Create a folder and give folder path in c# code just like above i have given. You can customize the code as well and give the dynamic name of bitmap file every time it is generated.

Query to get data from another database server

In this tutorial we will learn how to get data from another database server. It is quite handy to remain sit on one database server and get data from another database server. Let's have a look over how to do so

select * from [database_server_name].[database_name].dbo.tbl_name

If the database name or database server name contains special characters such as -,_ then you have to put their name inside square brackets[]like i did above.
Note:- For getting data from one database server in another database server, both should be connected each other via link server.

How to get date for every sunday in a month using asp.net with c#

In this programming tutorial you will learn how to get the date for every sunday in a month using asp.net with c#. There are multiple methods to achieve our goal. Let`s have a look over them.

How to get date for every sunday in a month in asp.net using c#

Method 1

using System.Globalization;//Donot forget to declare this namespace
 protected void Page_Load(object sender, EventArgs e)
    {
 DateTime currentDateTime=System.DateTime.Now;
        int year=currentDateTime.Year;
        int months=currentDateTime.Month;
        GetDatesOfSundays(year, months, DayOfWeek.Sunday);
    }
    protected void GetDatesOfSundays(int year, int month, DayOfWeek dayName)
    {
        CultureInfo ci = new CultureInfo("en-US");
        for (int i = 1; i <= ci.Calendar.GetDaysInMonth(year, month); i++)
        {
            if (new DateTime(year, month, i).DayOfWeek == dayName)
                Response.Write(i.ToString() + ",");
        }
    }

Output
1,8,15,22,29

 


Method 2

protected void Page_Load(object sender, EventArgs e)
    {
     Response.Write(GetDatesOfSundays(System.DateTime.Now));
    }
private string GetDatesOfSundays(DateTime DatMonth)
    {
        string sReturn = "";
        int iDayOffset = DatMonth.Day - 1;
        DatMonth = DatMonth.AddDays(System.Convert.ToDouble(-DatMonth.Day + 1));
        DateTime DatMonth2 = DatMonth.AddMonths(1).AddDays(System.Convert.ToDouble(-1));
        while (DatMonth < DatMonth2)
        {
            if (DatMonth.DayOfWeek == System.DayOfWeek.Sunday)
            {
                if (sReturn.Length > 0) sReturn += ",";
                sReturn += DatMonth.ToShortDateString();
            }
            DatMonth = DatMonth.AddDays(1.0);
        }
        return sReturn;
    } 

Output
5/1/2011,5/8/2011,5/15/2011,5/22/2011,5/29/2011


So these are the methods to find the sundays in a month using asp.net with c#.

Query to get the total no of tables in a ms sql server database

In this programming tutorial you will learn the query to get the total no of tables in ms sql server database. Its quite easy. Lets have a look over the query mentioned below.

-- This query will give you the total no of user created tables
Select Count(*) As TableCount
From   Information_Schema.Tables
Where  Table_Type = 'BASE TABLE'

Query to get the total no of columns in a given table in ms sql server database

In this programming tutorial you will learn the query to get the total no of columns in a given table in ms sql server database. Its quite easy in ms sql server. Lets have a look over the query given below

Select Count(*) As ColumnCount
From   Information_Schema.Columns
Where Table_Name = 'mytbl_name' 

Query to get the all table names with their total number of columns stored in ms sql server

In this programming tutorial you will learn the query to get the all table names with their total number of columns stored in ms sql server. Its quite easy. Lets have a look over the query given below.

select table_name,count(*) as total_columns from information_schema.COLUMNS
GROUP BY table_name order by table_name asc 

 







Share:






















Query to get the all column names of a given table in ms sql server

In this programming tutorial you will learn the query to get the all column names of a given table in ms sql server. Its quite easy. Lets have a look over the query

select column_name from information_schema.COLUMNS 
where table_name='yourtbl_name' 
order by column_name asc





Share this post :






















Wednesday, June 1, 2011

find out the second highest salary in Sql?

create table Sal(salary int)

insert into Sal values(1000)
insert into Sal values(2000)
insert into Sal values(3000)
insert into Sal values(4000)
insert into Sal values(5000)

SELECT MIN(salary) FROM Sal WHERE
salary IN (SELECT  distinct TOP 2
salary FROM Sal ORDER BY salary DESC)

find out the third highest salary in Sql?

create table Sal(salary int)

insert into Sal values(1000)
insert into Sal values(2000)
insert into Sal values(3000)
insert into Sal values(4000)
insert into Sal values(5000)

SELECT MIN(salary) FROM Sal WHERE
salary IN (SELECT  distinct TOP 3
salary FROM Sal ORDER BY salary DESC)

                        (or)

(SELECT MAX(salary)  FROM Sal
WHERE salary < (SELECT MAX(salary) FROM Sal
where salary < (SELECT MAX(salary) FROM Sal
where salary < (SELECT MAX(salary) FROM Sal))))

 

Output:

sql output

Share this post :