ASP.net Web Server Controls

February 4, 2011

1. Button Control-
Syntax:
<asp:Button id=”MyButton”     Text=”label”     CommandName=”command”     CommandArgument=”commandargument” CausesValidation=”true | false”     OnClick=”OnClickMethod”     runat=”server”/>

The Button control is used in creating the push buttons on the web page. There are two types of the buttons, Submit and Command buttons. Here the submit button simply posts back to the same page of aspx. Whereas the Command buttons are to help in creating different button controls to perform event handling with the script at the server page.

Here the ID is what used in referring to its properties in the scripting. Text refers to the label on the button to display. OnClick will refer to the method name that will get to the code behind function, in c# or Vb. By default the button click causes the validation for a page as per the given validation conditions for every field in the form. We can disable this by setting “CausesValidation” to false.

Command button uses the “CommandName” to differentiate all the command buttons from each other on the same page. “CommandArgument” shall stay as an additional information for the command button control.

2. CheckBox Control:

Syntax:
<asp:CheckBox id=”CheckBox1″      AutoPostBack=”True|False”     Text=”Label”     TextAlign=”Right|Left”     Checked=”True|False”     OnCheckedChanged=”OnCheckedChangedMethod”     runat=”server”/>

The checkbox control is used in the forms to add a checkbox control allowing the user to select or deselect. This to have a switch between true or false in the control.

Here the Text property to give the caption of the checkbox control. TexAlign property to set the alignment of the caption either to left or right. Checked property to set it by default as checked if set to true. The “onCheckedChanged” is the event handler used to call the method from the code behind.

CheckBoxList control is the one that is used along with multiple checkboxes to have a grouped set of them in a container of a CheckBoxList.

“AutoPostBack”, is to be enabled if we are interested in to have a post back to take place on a click over the checkbox. But by default it is disabled.

3. HyperLink Web Server Control:

Syntax-
<asp:HyperLink id=”HyperLink1″     NavigateUrl=”url”     Text=”HyperLinkText”     ImageUrl=”url”     Target=”window”     runat=”server”/>
or
<asp:HyperLink id=”HyperLink1″””     NavigateUrl=”url”     ImageUrl=”url”     Target=”window”     runat=”server”>   Text</asp:HyperLink>

The HyperLink control is used to create a link that on click moves to other page. Here the NavigateUrl property gives the value of the URL link where the page has to be redirected.Then the Text property is used for the need to display text for the active link or the ImageUrl property set to refer to the URL of the image which is used as the link control. If both Text and ImageUrl are given, then the ImageUrl takes a precedence above the text.

The Target propeerty refers to the value given to refer to the control, what kind of redirection it has to be. The  Target valuue can be -_blank:Display the link in a new window without frames. _parent:Display the page in the frameset parent, _self: Display the page in the same frameset itself. _top:Display the page in a full window without frames.

To be continued…

Advertisements

Constants: Not as easy as u might have thought about it!!!

The Constants as you might have read, ‘a value that can never be changed.’ now it does not end with here. There is something more you need to know about constants in C#.

as we define a constant value,

public const int i=10;

Now here i is an integer value of access public and the use of ‘const’ makes it a constant, i.e a static value through out such that when ever used by any other class and members its value stays unchanged.

You must remember:

  • you can not use constants for your parameters and also in the return value of your methods.
  • a constant is assigned a value during the compile time, and so we can not use a constant to refer to an object as the objects are to deal with the runtime whereas the constants as we know are dealing with the compile time.

Sample Code:

namespace ns_constants

{

public class constsample

{

public const int ciVal1 = 10;

public const int csStrng = “hello friends!”;
public static void Main (string[] args)

{

const string csStrng2 = “you can not change me!!”;

Console.WriteLine(“ciVal1 = {0}”, ciVal1);

Console.WriteLine(“csStrng = {0}”, csStrng);

Console.WriteLine(“csStrng2 = {0}”, csStrng2);

}

}

}

This is it. Let me know your comments and feed backs.

This post of mine is to give a quick run on the types available in C# and to list those which are CLS(Common Language Specification) compliant on the .NET framework.

As we all know the data types supported under C# are,

Signed Byte – System.SByte
Unsigned Byte – System.Byte

Signed short integer – System.Int16
Unsigned short integer – System.UInt16

Signed integer – System.Int32
Unsigned integer – System.UInt32

Signed Long integer – Syste.Int64
Unsigned Long integer – System.UInt64

Character – System.Char

Which are CLS compliant?
Amongst all these only few are CLS compliant, as defined to be used in a way that can be made to support the .NET’s feature of portability and inter language integration.

In the above set of types, we have the following only to be defined as CTS or CLS compliant to make it inter operable between different coding languages,

Unsigned Byte – System.Byte

Signed short integer – System.Int16

Signed integer – System.Int32

Signed Long integer – System.Int64

Character – System.Char

How do we get to define signed and Unsigned??

If you are likely to define an Unsigned value integer,

uint i = 24U; //this says that the i=24 and is Unsigned

int j = 22U; //this gives a compile time error as we can see ‘i’ is not an Unsigned

integer.

boolean b = true; // booleans can be true or false

So we use U, for unsigned, what about the others?

So here we go the other value types,

Integers:
Long –> L
Unsigned –> U
Unsigned Long –> UL or LU

Real:
Float: F
Double: D
Decimal: M

Note: ‘Null’ is a reference type, whereas the above mentioned are value types

A yet another topic and a great article on C#(ASP.net) to share,

The Properties in C#:

The usual way of haing fields made them unsafe to be accessed (read and write) by any external class that had an access to them. We can either make these variables unsafe through a access modifier of “Private”, but then they can not be used in the other classes.

To have a safer access to the member variables, where we can set the right to read or write of any field in C#, we have got an extraordinary feature added up as “Properties”

csharp Properties Example

Sample Code:

using System;

namespace SampleProgram

{

public class Employee

{

private int emp_id = -1; //field with private access

//Property for the field emp_id as ID

public int ID

{

get { return emp_id; }

set { emp_id = value; }

}

private string emp_name = string.Empty;//field with private access with in this class

//Property for the field emp_name as NAME

public string NAME

{

get { return emp_name; }

set { emp_name = value; }

}

}

//Lets define the other class to initiate the Main method

public class EmployeeManager

{

public static void Main(String[] args)

{

Employee emp = new Employee();

emp.ID = 1011; //initializing id with the property

emp.NAME = “Mahaveer”; //initializing name with the property

Console.WriteLine(“Employee Id:{0}, Employee Name:{1}”, emp.ID,emp.NAME);

Console.ReadKey();

}

}

}

Advantage of using Properties You might ask a question Isn’t it better to make a field public than providing its property with both set { } and get { } block? After all the property will allow the user to both read and modify the field so why not use public field instead?

Not always! Properties are not just to provide access to the fields; rather, they are supposed to provide controlled access to the fields of our class. As the state of the class depends upon the values of its fields, using properties we can assure that no invalid (or unacceptable) value is assigned to the fields.

Here we go with one more big big advantage of having the Properties are to have a control over the freedom to allow either write or read alone right to the class using this property with the object instantiated,

Lets see to an example for Read Only Properties:

public class Employee

{

private int emp_id = -1; //field with private access

//Property for the field emp_id as ID

public int ID

{

get { return emp_id; }

}

private string emp_name = string.Empty;//field with private access with in this

class

//Property for the field emp_name as NAME

public string NAME

{

get { return emp_name; }

}

//Constructor for initializing the fields

public Employee(int employeeId, string employeeName)

{

emp_id = employeeId;

emp_name = employeeName;

}

}

//Lets define the other class to initiate the Main method

public class EmployeeManager

{

public static void Main(String[] args)

{

Employee emp = new Employee(1011, “Mahaveer”);

Console.WriteLine(“Employee Id:{0}, Employee Name:{1}”, emp.ID, emp.NAME);

Console.ReadKey();

}

}

Here in the above example as you can find the fields are not given a set in the property, that makes it a read only property, hence these read only Properties can not initialized directly. Therefore we have them initialized in the “Custom Constructor” Employee(int employeeId,string employeeName).

As I said above, we can have both read opnly and write only Properties, I suppose you guys might have even got an idea of how to get this, just have a look at the below example that can make you understand it much easyly,

Write Only Properties example:

public class Employee

{

private int emp_id = -1; //field with private access

//Property for the field emp_id as ID

public int ID

{

set { emp_id = value; }

}

private string emp_name = string.Empty;//field with private access with in this class

//Property for the field emp_name as NAME

public string NAME

{

set { emp_name = value; }

}

//Method to display the Employee fields

public void DisplayEmployee()

{

Console.WriteLine(“Emplyee ID:{0}, Employee Nmae: {1}”, emp_id, emp_name);

}

}

//Lets define the other class to initiate the Main method

public class EmployeeManager

{

public static void Main(String[] args)

{

Employee emp = new Employee();

emp.ID = 1011;

emp.NAME = “Mahaveer”;

emp.DisplayEmployee();//To display the field values

Console.ReadKey();

}

}

This again as given above, allows the programmer only to have a write performed on the property, and so we have a method defined for displaying the Property that are written and so we get the output that displays the exact value that taken up by the Write only property.

Hope the examples have given a clear idea of what are the advantage of having our Properties defined for the fields. For any more queries on this you can get back to me.Keep visiting my blog for more interesting topics on C# and ASP.net.

ASP and ASP.net are completely different in the sense you do not need to know the ASP to kick start ASP.net. ASP.net does uses the OO languages C# and VB.net for the scripting, which is absent in classic ASP. This way the ASP.net needs a knowledge in C# or in VB.net.

Also we can write the ASP page for the server side execution of any functionality like fectching and validating datas from the server data base. In case of any client side coding like giving the user a message or validating the fields can be done through the scripting languages like Javascript or VBscript.

ASP.net is compiled

Since ASP uses vb script, there is no compilation. All ASP pages are interpreted when th page is executed.

ASP.NET uses modern .NET languages like C#, VB.NET etc. They can be compiled to efficient Microsoft Intermediate Language (MSIL). When you compile an ASP.NET application, the server side code is compiled to assemblies. These assemblies are loaded at run time which makes the ASP.NET pages perform better than classic ASP.

The ADO.net

Classic ASP uses a technology called ADO to connect and work with databases. ASP.NET uses the ADO.NET technology (which is the next generation of ADO).

Event Driven Programming

It is so easy to drag and drop a button control and double click on the button to write the event handler for the button click event. When you click on the button at turn time, it will execute whatever code you have written in the event handler.

This type of event driven programming was not available with classic ASP. You cannot drag and drop a button and write a ‘on click’ event handler with ASP.

With ASP.NET, this is changed. It works pretty much like your Visual Basic program. You can write event handlers for several events like button click event, text changed event etc. However, there is big difference between the way event handling works in regular Visual basic and ASP.NET. In ASP.NET, a page is loaded in the client browser. And the server may be in another location (may be in another country). When you click on the button in an ASP.NET page, the ‘click event’ handler has to be executed on the server, not on the client.

Also how shall the server made to know that an event has occured such that the ASP.net will execute the code for the relative event? This is made possible through client side script (Javascript or VBscript) such that script sends a message to the server that an event has occured and the respective event handler will execute on the serevr side. This is how the ASP.net page does works and funtions.

ASP.net very secure

The ASP.net’s ability to have the execution on the server side and on the client side only the HTML of the page layout is visible and not the script that gets compiled on the server, and so the end user is completely unaware of the functionality that happen on the back end and is given no access on the scripting code at any cost. Thus making the ASP.net more secure.

%d bloggers like this: