Friday, July 15, 2011

call JavaScript - jQuery code from ASP.NET Server-Side

Default.aspx
--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Register Client Script Block with Updatepanel</title>
    <script src="JavaScript/jquery.min.js" type="text/javascript"></script>
</head>
<body>
    <form id="theForm" runat="server">
    <div>
        <asp:ScriptManager ID="sm" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="upPnl" runat="server">
            <ContentTemplate>
                <asp:Button ID="btnPostback" runat="server" Text="Standard Postback" OnClick="btnPostback_Click" />
                <asp:Button ID="btnAsynchPostback" runat="server" Text="Asynchronous Postback" OnClick="btnAsynchPostback_Click" />
            <asp:Label ID="lbl1" runat="server"></asp:Label>
            </ContentTemplate>
            <Triggers>
                <asp:PostBackTrigger ControlID="btnPostback" />
            </Triggers>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>
Default.aspx.cs
--------------------
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.Text;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnPostback_Click(object sender, EventArgs e)
    {
        runjQueryCode("alert('After a standard postback.')");
        lbl1.Text = "This is  postback";
    }
    protected void btnAsynchPostback_Click(object sender, EventArgs e)
    {
        runjQueryCode("alert('After an asynchronous postback.')");
        lbl1.Text = "This is Asynchrounous postback";
    }
    private string getjQueryCode(string jsCodetoRun)
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("$(document).ready(function() {");
        sb.AppendLine(jsCodetoRun);
        sb.AppendLine(" });");
        return sb.ToString();
    }
    private void runjQueryCode(string jsCodetoRun)
    {
        ScriptManager requestSM = ScriptManager.GetCurrent(this);
        if (requestSM != null && requestSM.IsInAsyncPostBack)
        {
            ScriptManager.RegisterClientScriptBlock(this,
                                                    typeof(Page),
                                                    Guid.NewGuid().ToString(),
                                                    getjQueryCode(jsCodetoRun),
                                                    true);
        }
        else
        {
            ClientScript.RegisterClientScriptBlock(typeof(Page),
                                                   Guid.NewGuid().ToString(),
                                                   getjQueryCode(jsCodetoRun),
                                                   true);
        }
    }
}

Output:

-------------- javascript postback

Thursday, July 7, 2011

Passing parameters to javascripts from code behind

Default.aspx
------------------
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>    
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="lblValue" runat="server"></asp:Label>
    <asp:HiddenField ID="hdn1" runat="server" />
    </div>
    </form>
</body>
</html>
Default.aspx.cs
------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Random rand = new Random((int)DateTime.Now.Ticks);
        
        hdn1.Value =  "ooty";
        Response.Write("<script> alert('" + hdn1.Value +"');</script>");
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("<script type='text/javascript'>");
        sb.Append("var ws_wsid = '9ad5fdccd9224b53a60db7369ab5b733';");
        sb.Append("var ws_address ='" + hdn1.Value + "' ;var ws_width = '600';var ws_height = '460';var ws_layout = 'horizontal';var ws_transit_score = 'true';");
        sb.Append("var ws_commute = 'true';");
        sb.Append("var ws_map_modules = 'all';");
        sb.Append("</script><style type='text/css'>#ws-walkscore-tile{position:relative;text-align:left}#ws-walkscore-tile *{float:none;}#ws-footer a,#ws-footer a:link{font:11px/14px Verdana,Arial,Helvetica,sans-serif;margin-right:6px;white-space:nowrap;padding:0;color:#000;font-weight:bold;text-decoration:none}#ws-footer a:hover{color:#777;text-decoration:none}#ws-footer a:active{color:#b14900}</style><div id='ws-walkscore-tile'><div id='ws-footer' style='position:absolute;top:442px;left:8px;width:588px'><form id='ws-form'><a id='ws-a' href='http://www.walkscore.com/' target='_blank'>What's Your Walk Score?</a><input type='text' id='ws-street' style='position:absolute;top:0px;left:170px;width:386px' /><input type='image' id='ws-go' src='http://cdn.walkscore.com/images/tile/go-button.gif' height='15' width='22' border='0' alt='get my Walk Score' style='position:absolute;top:0px;right:0px' /></form></div></div><script type='text/javascript' src='http://www.walkscore.com/tile/show-walkscore-tile.php'></script>");
         sb.ToString();
         Response.Write(sb.ToString());
    }
}

walkscore

Sunday, July 3, 2011

Dynamically Load User Control in Asp.Net

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Panel.aspx.cs" Inherits="Panel" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc2" %>
<%@ Register Src="~/UserControl/Menu.ascx" TagName="Menu" TagPrefix="cc1" %>
 <%@ Reference Control="~/UserControl/Emp1.ascx"  %>
   <%@ Reference Control="~/UserControl/Emp2.ascx" %>
  <%@ Reference Control="~/UserControl/Emp3.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
    .Button
       {
        background-color: #316ac4;
        width:75px;  
        height:25px;          
        padding: 0;
        color:Black;
        cursor:pointer;
        color:White;
        font-weight:bold; 
        margin-top:0px;
         
         -moz-border-radius: 15px;
         -webkit-border-radius: 15px;
         -khtml-border-radius: 15px;
          behavior: url(/border-radius.htc);  border-radius: 15px;
          
         }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <asp:ScriptManager ID="sc1" runat="server"></asp:ScriptManager>
        <table border="0" width="700px" bgcolor="#316ac4">
        <tr>
        <td>
         <asp:UpdatePanel ID="UpdatePanel4" runat="server">
       <ContentTemplate>
         <asp:Button ID="Button1" runat="server" Text="Home"  CssClass="Button" OnClick="Button1_Click"/>
          <asp:Button ID="Button2" runat="server" Text="Contact"  CssClass="Button" OnClick="Button2_Click"/>
 <asp:Button ID="Button3" runat="server" Text="Phone"  CssClass="Button" OnClick="Button3_Click"/>
        </ContentTemplate>
        </asp:UpdatePanel>
        </td>
        </tr>
     </table>
       
       <table border="1"  width="700px" height="500px">
       <tr>
       <td>
       <asp:Panel ID="Panelmain" runat="server">
       <asp:UpdatePanel ID="UpdatMain" runat="server">
       <ContentTemplate>
       <asp:Panel ID="Panel1" runat="server">
         <asp:UpdatePanel ID="UpdatePanel1" runat="server">
          <ContentTemplate>
           <%-- Add Text Here--%>
            
        </ContentTemplate>
          </asp:UpdatePanel>
           
       </asp:Panel>
       
       <asp:Panel ID="Panel2" runat="server">
        <asp:UpdatePanel ID="UpdatePanel2" runat="server">
          <ContentTemplate>
               <%-- Add Text Here--%>
          </ContentTemplate>
        </asp:UpdatePanel>
       </asp:Panel>
       
       
        <asp:Panel ID="Panel3" runat="server">
        <asp:UpdatePanel ID="UpdatePanel3" runat="server">
          <ContentTemplate>
                <%-- Add Text Here--%>
          </ContentTemplate>
        </asp:UpdatePanel>
       </asp:Panel>
       
       </ContentTemplate>
       </asp:UpdatePanel>
       
       </asp:Panel>
       </td>
       </tr>
       </table>
        
    </div>
    </form>
</body>
</html>
Default.aspx.cs
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;
public partial class Panel : System.Web.UI.Page
{
    protected ASP.usercontrol_emp1_ascx control1;
    protected ASP.usercontrol_emp2_ascx control2;
    protected ASP.usercontrol_emp3_ascx control3;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
            Panel3.Visible = false;
        } 
    }
    //Button1_Click
    protected void Button1_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        Panel2.Visible = false;
        Panel3.Visible = false;
        control1 = (ASP.usercontrol_emp1_ascx)LoadControl("~/UserControl/Emp1.ascx");
        UpdatePanel1.ContentTemplateContainer.Controls.Add(control1);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = true;
        Panel3.Visible = false;
        control2 = (ASP.usercontrol_emp2_ascx)LoadControl("~/UserControl/Emp2.ascx");
        UpdatePanel2.ContentTemplateContainer.Controls.Add(control2);
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = true;
        control3 = (ASP.usercontrol_emp3_ascx)LoadControl("~/UserControl/Emp3.ascx");
        UpdatePanel3.ContentTemplateContainer.Controls.Add(control3);
    }
}
Output:

dynamic

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.