Naveen P.N

Just another Blog to play with programming

Archive for the ‘ASP.NET’ Category

Project-Based Development and Projectless-Based Development

without comments

Introduction

Web Project can be developed of two types

a)      Projectless Based Development

b)      Project Based Development or web project

Project less Based Development

Creating websites of this form is called project less applications. It has its own advantages over Project Based Development.

1

The advantages include

a) It simplifies deployment: We can simply copy the files in the website directory to the web server.

b) It simplifies file management: Adding , deleting and  moving files  is very easy u can just delete, add or move a web page ,there is no need to go through Visual Studio or edit the project file.

c) It simplifies team collaboration: Each team can work independently on different pages.

d)     It simplifies debugging: Creating a web project ,you must recompile the entire application when you change a single file With project less development, each

page is compiled separately, and the page is only compiled when you request it for the first time.

Project Based Development or Web Project

Creating project of this form is called Project Based Development or simply Web Project. If we create application of this form Visual Studio generates a number of extra files, including .csproj and .csproj.user project files and .sln solution file.

2

3

When you build your application, Visual Studio generates temporary files, which is places in the Obj subdirectory, and one or more .pdb files (in the Bin subdirectory) with debugging symbols. None of these files should be deployed to the web server when your web application is complete. Furthermore, none of the C# source code files (files with the extension .cs) should be deployed, because Visual Studio recompiles them into a DLL assembly.

However both are same once they are deployed to the web server,but they differ in their structure .The difference include

a)      Compilation: Web projects are compiled by Visual Studio and not ASP.NET when they run. All the web pages classes is combined into a single assembly that has the name of web project(like web project.dll);

4

b)      Code-behind: The web pages also uses code behind file however they include one additional file with an extension .aspx.designer.cs.

5

c)      Assembly references: In a project less website ,all the assembly references are recorded in the web.config file

Other posts

* Using C# and VB classed together in the App_Code folder :

*   ASP.NET Application Folders

*   Creating Dynamic Linking Library using ASP.NET

*   VB Style Calendar in ASP.NET

* Sending SMS from ASP.NET Application

*   View State

*   What is the difference between Server.Transfer and Response.Redirect?

*   Text to Speech in ASP.NET

*  Using C# and VB classed together in the App_Code folder :
http://naveenpn.wordpress.com/2009/10/23/using-c-and-vb-classed-together-in-the-app_code-folder/
*  ASP.NET Application Folders
http://naveenpn.wordpress.com/2009/10/08/asp-net-application-folders/
*  Creating Dynamic Linking Library using ASP.NET
http://naveenpn.wordpress.com/2009/10/05/creating-dynamic-linking-library-using-asp-net/
*  VB Style Calendar in ASP.NET
http://naveenpn.wordpress.com/2009/09/27/vb-style-calendar-in-asp-net/
*  Sending SMS from ASP.NET Application
http://naveenpn.wordpress.com/2009/09/27/sending-sms-from-asp-net-application/
* View State
http://naveenpn.wordpress.com/2009/09/13/view-state/
*  What is the difference between Server.Transfer and Response.Redirect?
http://naveenpn.wordpress.com/2008/06/11/aspnet/
*  Text to Speech in ASP.NET
http://naveenpn.wordpress.com/2009/08/27/text-to-speech-in-asp-net/
AJAX Control Toolkit
——————–
*  Traditional Page Updates VS AJAX Page Updates
http://naveenpn.wordpress.com/2009/08/29/traditional-page-updates-vs-ajax-page-updates/

Written by Naveen P.N

October 23, 2009 at 1:09 am

Posted in ASP.NET

Using C# and VB classed together in the App_Code folder

with one comment

Introduction

Hi in this article I demonstrate how to use two different classes together in ASP.NET application. As we know ASP.NET application supports more than 24 languages we don’t know how to use two different languages in one single application. One important thing mixing of classes coded in different languages is not allowed with default settings. You can, however, configure your web.config file to get this done. This article is going to explain you that how one can do that.

Creating Sub folders and classes

Create two separate folders one for C# code and one for VB code .Without modifying web.config file if we compile we get an error.

23

231

To solve this we should modify web.config file.

23b

protected void CSharpbtn_Click(object sender, EventArgs e)

{

int a = MathematicsClassCSharp.addition(2, 2);

Label1.Text = a.ToString();

}

protected void VBbtn_Click(object sender, EventArgs e)

{

int a1 = MathematicsClassVB.addition(1,1);

Label1.Text = a1.ToString();

}

Written by Naveen P.N

October 23, 2009 at 12:24 am

Posted in ASP.NET

Disabling Copy Paste in a TextBox

without comments

Hi this article is more or less similar to the last article “Restricting the mouse click on a web page” the logic is the same with little intelligence. In this article I will disable the Copy, Paste and Ctrl Functionality of a TextBox, before reading this article I request you to please check the previous article for better understanding.

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title>Untitled Page</title>
<script type=”text/javascript” src=”Restrict Copy and Paste.js”>
</script>
</head>
<body>
<form id=”form1″ runat=”server”>
<pre>
Right Click Disabled   : <asp:TextBox ID=”TextBoxCopy” runat=”server”
onMouseDown=”DisableRightClick(event)”>
</asp:TextBox>
Ctrl Key Disabled      : <asp:TextBox ID=”TextBox2″ runat=”server”
onKeyDown=”return DisableCtrlKey(event)”>
</asp:TextBox><br />
Copy and Paste Disabled: <asp:TextBox ID=”TextBox1″ runat=”server”
oncopy=”return false” onpaste=”return false”                    oncut=”return false”>       </asp:TextBox>
</pre>
</form>
</body>
</html>

Restrict Copy and Paste.aspx

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head runat=”server”>

<title>Untitled Page</title>

<script type=”text/javascript” src=”Restrict Copy and Paste.js”>

</script>

</head>

<body>

<form id=”form1″ runat=”server”>

<pre>

Right Click Disabled   : <asp:TextBox ID=”TextBoxCopy” runat=”server”

onMouseDown=”DisableRightClick(event)”>

</asp:TextBox>

Ctrl Key Disabled      : <asp:TextBox ID=”TextBox2″ runat=”server”

onKeyDown=”return DisableCtrlKey(event)”>

</asp:TextBox><br />

Copy and Paste Disabled: <asp:TextBox ID=”TextBox1″ runat=”server”

oncopy=”return false” onpaste=”return false” oncut=”return false”> </asp:TextBox>

</pre>

</form>

</body>

var buttonright = 2; // button right

function DisableRightClick(event)

{

//For mouse right click

if (event.button==buttonright)

{

alert(“Right Clicking not allowed!”);

}

}

</html>

Restrict Copy and Paste.js

function DisableCtrlKey(e)

{

var code = (document.all) ? event.keyCode:e.which;

var message = “Ctrl key functionality is disabled!”;// look for CTRL key press

if (parseInt(code)==17)

{

alert(message);

window.event.returnValue = false;

}

}


text1


text2


Download Code with Article

Written by Naveen P.N

October 8, 2009 at 11:19 pm

Posted in ASP.NET

Restricting Mouse Click on a web page

without comments

Hi in this article I will explain how to restrict mouse click on a web page ,so that user will not save images just like orkut which restrict user from saving images.Actually this idea I got from my friends project whose one of the module was restricting user from mouse click.I though to share the small snippet with you people ……….check out and please give your valuable suggestion.

Restrict Mouse Right Click on a web page1

The trick is very simple ,it is all done with little JavaScript .

DisableRightClick.js
DisableRightClick.js

document.onmousedown = mouseDown;

var buttonright = 2; // button right

var msg =“Right Click is not Allowed”;

function mouseDown(e)

{

if (event.button==buttonright)

{

alert(msg);

}

}

DisableRightClick.aspx

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head runat=”server”>

<title>Untitled Page</title>

<script type=”text/javascript” src=”DisableRightClick.js.js”>

</script>

</head>

<body>

<form id=”form1″ runat=”server”>

<div>

</div>

</form>

<p>

<img alt=”" src=”images/naveen.gif” style=”width: 107px; height: 118px” /></p>

</body>

</html>


Restrict Mouse Right Click on a web page2

Dowlnload Code with Article

Written by Naveen P.N

October 8, 2009 at 10:58 pm

ASP.NET Application Folders

without comments

Hi in this article I will explain application folders in ASP.NET 2.0.You would have observed when you create ASP.NET Application you can add folders called application folders, these are special folders in asp.net each one has special purpose and meaning. ASP.NET 2.0 has the capability to automatically precompiled ASP.NET application whereas in 1.0 and 1.1 frameworks everything was compiling into DLL. This is not the case in ASP.NET 2.0 framework this is because of its defined folder structure Let us now examine the usage of each folder in detail.
There are 6 folders which can be added
a) \App_Code
b) \App_Data
c) \App_Themes
d) \App_GlobalResources
e) \App_localResources
f) \App_WebReferences
g) \App_Browsers

Hi in this article I will explain application folders in ASP.NET 2.0.You would have observed when you create ASP.NET Application you can add folders called application folders, these are special folders in asp.net each one has special purpose and meaning. ASP.NET 2.0 has the capability to automatically precompiled ASP.NET application whereas in 1.0 and 1.1 frameworks everything was compiling into DLL. This is not the case in ASP.NET 2.0 framework this is because of its defined folder structure Let us now examine the usage of each folder in detail.

There are 6 folders which can be added

a) \App_Code

b) \App_Data

c) \App_Themes

d) \App_GlobalResources

e) \App_localResources

f) \App_WebReferences

g) \App_Browsers

app1
\App_Code

The \App_Code folder is used to store classes, .wsdl (Web Service Description Language), and typed dataset The main use of \App_Code folder is any of the files used in this folder will be available to all the pages in the solutions. One nice thing whenever you place something inside the folder VS 2005 detects this and compiles it if is a class and creates XML web service proxy class (.wsdl).
Add the Calculator class into \App_Code folder, and we will refer this class in our solution

Calculator.cs

public class Calculator

{

public int add(int a, int b)

{

return (a + b);

}

public int sub(int a, int b)

{

return (a – b);

}

}

Default.aspx

protected void Page_Load(object sender, EventArgs e)

{

Calculator calc = new Calculator();

int a = calc.add(1, 2);

Label1.Text = a.ToString();

int b = calc.sub(2, 1);

Label2.Text = b.ToString();

}

We can add class file of both .vb and .cs but little trick as to be done, we have to configure web.config file.

<compilation>

<codeSubDirectories>

<add directoryName=”VB”></add>

<add directoryName=”CS”></add>

</codeSubDirectories>

</compilation>

\App_Data

This folder stores the data stores which is utilized by the application.It can contain Microsoft SQL Express files (.mdf files), Microsoft Access files (.mdb files), XML files, and more.

\App_Themes

Themes can provide common look and feel.It can contain .skin file ,css files , images.All these elements is called as theme

\App_Global Resources

Resource files are nothing but no executable text associated with the program,Typically resources are images,icons ,text and auxillary files.Resource files are  string tables that can serve as data dictionaries for your applications when these applications require changes to content based on things such as changes in culture. You can add Assembly Resource Files (.resx) to this folder, and they are dynamically compiled and made part of the solution for use by all your .aspx pages in the application.

Now let us create two resource files one for English and other for French.

  • English-Resource.resx :It is the default language file using American English

app2

  • French-Resource.resx: The second file is for the same text, but in Finnish Language

app3

When someone with a browser culture of fi-FI invokes the page, he sees the information that comes from this file (French-Resource.resx). Everyone else who comes to the site gets the information that comes from the other file (English-Resource.resx).

protected void Button1_Click(object sender, EventArgs e)

{

Label2.Text = Resources.Resource.Response + “” + TextBox1.Text;

}

By changing the language setting of Internet Explorer you can have the response in that language

app4

App_LocalResource

As \App_GlobalResources folder, is used application-wide. If you are not interested in constructing application-wide resources, then we should go for App_LocalResource whose resources can be used for a single .aspx page only.

\App_WebReference

The App_WebReferences folder is a new name for the previous Web References folder used in previous versions of ASP.NET. Now you can use the \App_WebReferences folder and have automatic access to the remote Web services referenced from your application.

\App_Browsers

The App_Browsers folder holds browser files, which are XML files used to describe characteristics and capabilities of these browsers. We can find a list of globally accessible browser files in the CONFIG\Browsers folder under the installation path. In addition, if you want to change any part of these default browser definition files, just copy the appropriate browser file from the Browsers folder to your application’s \App_Browsers folder and change the definition.

Download Code with Article>

Written by Naveen P.N

October 8, 2009 at 10:35 pm

Posted in ASP.NET

Creating Dynamic Linking Library using ASP.NET

without comments

Hi in this I  article I will  describes what exactly a dll is and how to create a dll file in ASP.NET and use the code.

DLL stands for Dynamic Linking Library which contains code and data which can be used by other application .The main advantage of creating dll is the project can be modularized i.e. the project can be divided into several modules each of the module is linked or called by their respective dll. Windows OS implemented some of the files has dll

1)       ActiveX(.ocx)

2)       Control Panel (.cpl)

3)       Device Driver (.drv)

The main advantage of creating dll’s are

  • The program can be divided into modules so it will easy for a software developer to integrate.
  • It is easy to deploy and installation.
  • Many application can share single copy of dll.

Creating dll using ASP.NET

Let us create a basic class of mathematics which contain some of the maths functions like add,sub,multiply.

Open your Visual Studio and migrate File ->New Project ->Class Library

dll-1

Public Class Class1

Public Function add(ByVal a As Integer, ByVal b As Integer) As Integer

Return a + b

End Function

Public Function subtract(ByVal a As Integer, ByVal b As Integer) As Integer

Return a – b

End Function

Public Function multiply(ByVal a As Integer, ByVal b As Integer) As Integer

Return a * b

End Function

End Class
Once you build a dll file is created ,in the debug folder ,now we will import this dll file into our Windows project.dll2

Create a new Windows Project File -> New Project-> Windows Forms Application.

dll-3

To make the class library available in the client application, a reference to the .dll needs to be added.To do so Select project ->Add Reference ->Browser ->bin \Debug->OK

dll33

Using this code we can access the data which is actually the dll.

Imports Mathematics

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim obj1 As New Class1

Dim result As Integer

result = obj1.add(TextBox1.Text, TextBox2.Text)

MessageBox.Show(“” + result.ToString)

End Sub

End Class
Download

Written by Naveen P.N

October 5, 2009 at 1:17 am

VB Style Calendar in ASP.NET

without comments

Hi in this article I will create the old style calendar control which was there in VB 6.0 using little bit of JavaScript check out……..Actually this idea was not mine it was from my professor,any how check out………………

Default.aspx

<%@ Page Language=”VB” AutoEventWireup=”false” CodeFile=”Default.aspx.vb” 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 id=”Head1″ runat=”server”>

<script type=”text/javascript”>

function displayCalendar()

{

var datePicker = document.getElementById(‘datePicker’);

datePicker.style.display = ‘block’;

}

</script>

<style type=”text/css”>

#datePicker

{

display:none;

position:absolute;

border:solid 2px black;

background-color:white;

}

.content

{

width:400px;

background-color:white;

margin:auto;

padding:10px;

}

html

{

background-color:silver;

}

</style>

<title>Calendar with JavaScript</title>

</head>

<body>

<form id=”form1″ runat=”server”>

<div>

<asp:Label

id=”lblEventDate”

Text=”Event Date:”

AssociatedControlID=”txtEventDate”

Runat=”server” />

<asp:TextBox

id=”txtEventDate”

Runat=”server” />

<img src=”Images/Calendar.gif” onclick=”displayCalendar()”

style=”width: 16px; height: 17px” />

<div id=”datePicker”>

<asp:Calendar

id=”calEventDate”

Runat=”server” />

</div>

<br />

<asp:Button

id=”btnSubmit”

Text=”Submit”

Runat=”server”  />

<hr />

<asp:Label

id=”lblResult”

Runat=”server” />

</div>

</form>

</body>

</html>

Default.aspx.vb

Partial Class _Default

Inherits System.Web.UI.Page

Protected Sub calEventDate_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles calEventDate.SelectionChanged

txtEventDate.Text = calEventDate.SelectedDate.ToString(“d”)

End Sub

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

lblResult.Text = “You picked: ” & txtEventDate.Text

End Sub

End Class

vb cal
Download Code

Written by Naveen P.N

September 27, 2009 at 12:47 am

Sending SMS from ASP.NET Application

with 2 comments

Hi In this article I am creating an application from which we can send free sms just like www.way2sms.com [Remember you just have registered in way2sms.com before trying this code]. It involes a little trick which I will explain when I am free please excuse for that and I am not explain much about web service I am directly sharing my code which I have implemented for my project check out and please give your feedback.

sms

SMS.aspx

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”SMS.aspx.cs” Inherits=”SMS” %>

<!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>

</head>

<body>

<form id=”form1″ runat=”server”>

<div>

<b>Sending SMS from your ASP.NET Application<br />

<br />

<br />

<asp:Label ID=”Label1″ runat=”server” Text=”User Name”></asp:Label>

<asp:TextBox ID=”TextBox4″ runat=”server”></asp:TextBox>

<br />

<br />

<asp:TextBox ID=”TextBox5″ runat=”server”></asp:TextBox>

<br />

<br />

<asp:Label ID=”Label3″ runat=”server” Text=”Moble Number”></asp:Label>

&nbsp;&nbsp;&nbsp;

<asp:TextBox ID=”TextBox3″ runat=”server”></asp:TextBox>

<br />

<br />

<center>Enter your text :</center>        </b></div>

<p style=”width: 698px”>

<b        <asp:TextBox ID=”TextBox6″ runat=”server” Height=”105px” Width=”296px”></asp:TextBox>

</b>

</p>

<asp:Label ID=”Label4″ runat=”server”></asp:Label>

<asp:Button ID=”Button1″ runat=”server” onclick=”Button1_Click” Text=”Send” />

</form>

</body>

</html>

SMS.aspx.cs

using System;

using System.Data;

using System.Configuration;

using System.Collections;

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 SMS : System.Web.UI.Page

{

protected void Button1_Click(object sender, EventArgs e)

{

JavaAdd.SendSMS js = new JavaAdd.SendSMS();

string username, password, number, data;

data = TextBox6.Text;

username = TextBox6.Text;

password = TextBox5.Text;

number = TextBox3.Text;

Label4.Text = js.sendSMSToMany(username, password, number, data);

}

}

Download Code

Written by Naveen P.N

September 27, 2009 at 12:37 am

Automatic Update with the Timer

without comments

Timer control can be used to update page automaticly . Instead of requiring the user to perform an action in order to trigger updates, the Timer control will initiate the postback automatically.

The Timer control can execute client-side events at precise intervals, such as refreshing an UpdatePanel every x seconds.

Timer-Demo.aspx

<body>

<form id=”form1″ runat=”server”>

<div>

</div>

<asp:ScriptManager ID=”ScriptManager1″ runat=”server”>

</asp:ScriptManager>

<br />

<asp:UpdatePanel ID=”UpdatePanel1″ runat=”server”>

<ContentTemplate>

<asp:Timer ID=”Timer1″ runat=”server” Interval=”100″ >

</asp:Timer>

<div style=”border-style:solid;background-color:Aqua;”>

<asp:Label ID=”Label1″ runat=”server”></asp:Label>

</div>

<asp:Button ID=”Button1″ runat=”server” Text=”Button” />

<br />

</ContentTemplate>

</asp:UpdatePanel>

protected void Page_Load(object sender, EventArgs e)

{

Label1.Text = DateTime.Now.ToString();

}

automatic update using timer

Written by Naveen P.N

September 13, 2009 at 2:11 am

View State

with one comment

The HTTP protocol, the fundamental protocol of the World Wide Web, is a stateless protocol. Each time you request a web page from a website, from the website’s perspective, you

are a completely new person.

The view state preserves the state (value) between postbacks this is called view state.

Consider these two programs to exactly explain what is view state

ViewState-Enable

In this when ever a user clicks a button the counter value increases because the previous value will be stored in view state in this way the HTTP protocol remembers the value of button.

<asp:Button ID=”Button1″ runat=”server”  onclick=”Button1_Click” Text=”0″ Width=”32px” />

protected void Button1_Click(object sender, EventArgs e)

{

Button1.Text = (Int32.Parse(Button1.Text) + 1).ToString();

}

ViewState-Disable

In this when ever a user clicks a button the counter value will not increases because the previous valueis not stores in view state because it is disabled ,so the HTTP protocol doesn’t remember the value.

<asp:Button ID=”Button1” runat=”server” EnableViewState=”False”

onclick=”Button1_Click” Text=”0″ Width=”32px” />

protected void Button1_Click(object sender, EventArgs e)

{

Button1.Text = (Int32.Parse(Button1.Text) + 1).ToString();

}

Written by Naveen P.N

September 13, 2009 at 1:38 am