Read, With the Name of Your Lord Who Created

Archive for the ‘.NET’ Category

The Differences of ASP.NET Forms and Windows Forms in a Nutshell

Posted by triaslama on June 24, 2008

I think its quite interesting if we compare two things that has equalities and differences. Now we will talk about Forms (WebForms and WinForms).

As we already know WebForms uses browser as presentation (ASP.NET Forms is no exception) and Windows Forms has its own presentation. So we may agree that WebForms has the strength through its availability and WinForms through its rich user interface.

In this post I will talk about the differences, what I get from both forms (ASP.NET Forms and Windows Forms) at a glance. I hope this post not tedious for those who already familiar with ASP.NET and Windows Forms.

Base Class
An ASP.NET Form inherits Page class (contained in System.Web.UI namespace). Look at the following example:
Presentation page (Welcome.aspx):

    <%@ Page Language=”C#” debug=”true” CodeFile=”Welcome.aspx.cs” Inherits=”Welcome.HelloWorldClass” %>

    <html>
    <head>
    <title>Welcome To ASP.NET Form</title>
    <script lang=”C#” runat=”server”>
    </script>
    </head>
    <body>
    <form runat=”server”>
    <center><asp:Label id=”label” Font-Size=”25px” style=”font-weight:bold;” runat=”server” /></center>
    </form>
    </body>
    </html>

 

This is the code behind (Welcome.aspx.cs):

Read the rest of this entry »

Advertisements

Posted in .NET, ASP.NET, C#, Programming | Tagged: , , , , | 8 Comments »

CSS in ASP.NET Server Controls, Declaratively vs. Programmatically

Posted by triaslama on May 7, 2008

ASP.NET controls has a bunch of attributes that take in order the look and feel of every control. For example TextBox server control, if we want the TextBox color turn into specific color we specify the ForeColor attribute to the name of Color defined in Color structure. If we want the TextBox text font appear in Georgia font, set Font-Name attribute to Georgia. ASP.NET TextBox script for above requirements will look something like this:

<asp:TextBox id="txtTest" ForeColor="Red" Font-Name="Georgia" runat="server" />

It looks simple, right?

But wait, for the one familiar with ASP.NET it doesn’t matter, but for everyone who already familiar with CSS maybe there is a bit problem because the attribute name of ASP.NET server controls slightly different with attribute in CSS file. So sometimes we may prefer that the looks and feels of ASP.NET controls ruled within CSS file. And yes, we can do this!

In ASP.NET information within a CSS file can be accessed either declaratively or programmatically, its up to you which one to choose. To make a CSS file visible within our page put this declaration inside our <head></head> tag:

<link rel="stylesheet" type="text/css" href="[filelocation/css_filename.css]" />

Read the rest of this entry »

Posted in .NET, ASP.NET, HTML / CSS | Tagged: , , , , , | 28 Comments »

Interacting With Get and Post Methods in ASP.NET

Posted by triaslama on May 1, 2008

There are two common ways to pass data from one page to another, using http Get and Post methods. In Get method, data passed to server using URL encoding. So with Get method data added in URL as query string. For more information on how to retrieve query string values, you can read my previous post here. When we use Post method the URL is still intact, because data passed in HTTP body.

In correlation with ASP.NET, retrieving data passed through HTTP Get and Post methods is quite simple. If data passed with Get method we need the following code to retrieve the data:

Page.Request.QueryString[<param>];

If data passed with Post method we need the following code to retrieve the data:

Page.Request.Form[<param>];

Maybe the problem will more suitable addressed with hand in code practice, so let’s try a simple code! I named the HTML file as methods_source.htm:

    <html>
    <head>
    <title>Using Http Get And Post</title>
    </head>
    <body>
    <form id=”frm_get” action=”get_recv.aspx” target=”_blank” method=”GET” >
    <table>
    <tr>
    <td>Name: </td> <td><input type=”text” id=”txtName” name=”name” /></td>
    </tr>
    <tr>
    <td>Address: </td> <td><input type=”text” id=”txtAddr” name=”addr” /></td>
    </tr>
    <tr>
    <td></td> <td><input type=”submit” value=”Send Using Get” /></td>
    </tr>
    </table>
    </form>
    <p />
    <form id=”frm_post” action=”post_recv.aspx” target=”_blank” method=”POST” >
    <table>
    <tr>
    <td>Name 2: </td>
    <td><input type=”text” id=”txtName2″ name=”name2″ /> </td>
    </tr>
    <tr>
    <td>Address 2: </td>
    <td><input type=”text” id=”txtAddr2″ name=”addr2″ /> </td>
    </tr>
    <tr>
    <td></td>
    <td><input type=”submit” value=”Send Using Post” /> </td>
    </tr>
    </table>
    </form>
    </body>
    </html>

Read the rest of this entry »

Posted in .NET, ASP.NET | Tagged: , , , , | 80 Comments »

Calling Client Script Events in ASP.NET Server Controls

Posted by triaslama on April 25, 2008

ASP.NET has its own controls (WebControls or HtmlControls) that rendered as elements in a page. ASP.NET server controls declared with ‘asp’ tag (<asp:[control_type] />) and contains runat=”server” attribute. ASP.NET server controls has its own attribute related to a specific control (such as onclick and text for click event and button text respectively).

For example: if we want a click event associated with a button of ASP.NET server control we use this script in our ASP.NET page:

<asp:Button id="btn" onclick="btn_Click" text="Test" runat="server" />

The click event received in onclick attribute and will be handled by btn_Click method. btn_Click is a method that will be called everytime a click occurs in this button control. We can write btn_Click method with any languages that supported by .NET framework.

But how if we want to call client script within ASP.NET server controls? Fortunately it can be accomplished to. Suppose that I have a button of ASP.NET server control but I want a click event of this button handled by Javascript function (in client side) rather than any of .NET languages (in server side).

In this example I use two buttons of ASP.NET server controls. The first button will have click event handled using server script and click event of second button handled via client script. I embed the javascript code inline inside the page, but we can pull it out and place it in separate *.js file then this file is referenced inside our ASP.NET page (expage.aspx):

Read the rest of this entry »

Posted in .NET, ASP.NET, Javascript | Tagged: , , , , , , | 20 Comments »

Simple Date Time Format in .NET

Posted by triaslama on December 10, 2007

DateTime format it’s a simple but interesting enough to get my attention. The problem begin when I must work with the DateTime type and I encountered several exception that raised because of date time formatting.

For explanatory purpose here I give a brief introduction on date time formatting in .NET platform:

Format Specifier

Description

d

Displays the specified date of the month (short form).

D

Displays the specified date of the month (long form).

dd

Displays the specified date of the month (measured the number between 1 and 31 -inclusive-). If the number is a single digit only, then the formatting preceding with zero number.

ddd

Displays abbreviated name of the specified date.

dddd (or more)

Displays full name of the specified date.

MM

Displays the specified month (two digits).

MMM

Displays abbreviated name of the specified month.

MMMM (or more)

Displays full name of the specified month.

t

Displays specified date and time (short form).

f

Displays specified date and time (long form, time format excluding second digits).

F

Displays specified date and time (long form, time format including second digits).

yy

Displays two digits number of the specified year, the first two digits are omitted.

yyyy

Displays the specified year.

HH

Displays hour for the specified DateTime in am/pm format.

hh

Displays hour for the specified DateTime in 24 hours format.

mm

Displays minute for the specified DateTime (range: 0-59).

ss

Displays second for the specified DateTime (range: 0-59).

Please refers to the .NET documentation for the more complete date time formatting, I exclude several formats here.

The above formatting is done using DateTime.ToString() method. ToString method from DateTime class has four overloads. One of them receives string as a parameter and then we can do date time formatting like the following (written in C#):

using System;


namespace Learning_Materials
{

class SimpleDateTimeFormats

{

static void Main(string[] args)

{

DateTime date = DateTime.Now;

// print out the date time using default format

Console.WriteLine(date.ToString());

// otherwise, specify the other formats

Console.WriteLine(date.ToString(“d”));

Console.WriteLine(date.ToString(“dd/MM/yyyy”)); // Indonesian format (date/month/year).

Console.WriteLine(date.ToString(“dd-MMMM-yyyy hh:mm:ss”)); // Indonesian format (date-month(fullname)- year hour(24 hours):minutes:seconds)

Console.WriteLine(date.ToString(“MM/dd/yyyy”)); // en-us format (month/date/year).

Console.WriteLine(date.ToString(“MMMM dd, yyyy HH:mm:ss”)); // en-us format (month date, year hours(am/pm format):minutes:seconds).

}

}

}

 

Here we go, sometimes missing the date time format can be so sucks. I ever do this mistake when I must insert user input in string format to the MS SQL Server database. the input was in date/month/year format (standard format for Indonesian date) but the SQL Server format was month/date/year, finally the exception happen.

Hopefully that I will found more interesting things in my coding experience and I can learn more from my mistakes (smile face). Regards and see you later.

Tri Sugiyantowo (triaslama)

 

Posted in .NET | Tagged: , , , , | 66 Comments »

Pengenalan Web Part

Posted by triaslama on November 5, 2007

Ini adalah pengalaman yang saya peroleh baru – baru ini, berkaitan dengan dengan penempatan saya di proyek departemen keuangan. Sebelumnya saya ucapkan terima kasih kepada temen – temen se-tim atas bantuannya selama ini (mas Aan, mas Reno, Anthony-kalau ada yg keliru tolong koreksi ya.-).

Web part merupakan suatu library (file dengan ekstensi *.dll), yang dapat dipasang/ ditempatkan pada halaman web ASP.NET. Jadi suatu webpart akan menempati wilayah atau bagian tertentu dari halaman web ASP.NET. Konsekuensi dari hal ini adalah penggunaan web part dapat meningkatkan reusability, misalnya web part A dapat dipasang pada halaman web_A dan halaman web_B dan juga halaman web_A dapat terdiri lebih dari satu web part misalnya halaman web_A terdiri dari web part A dan web part B.

Dalam .NET, untuk bisa menjadi suatu web part maka suatu kelas harus mewarisi dari kelas yang bernama WebPart(kelas ini terdapat dalam namespace System.Web.UI.WebControls.WebParts). Listing sederhananya dapat kita lihat dalam cuplikan kode berikut ini:

public class FirstWebPart : System.Web.UI.WebControls.WebParts.WebPart

Sedangkan berikut ini syntaks yang diperlukan untuk membuat suatu program ‘hello world’ dari suatu web part:

using System;
using System.Web;
using System.Web.UI.WebControls.WebParts;

namespace TestWebPart
{
    public class FirstWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {

    // mengganti implementasi method Render dari kelas WebPart
       protected override void Render(HtmlTextWriter writer)
       {
           writer.Write("Hello, World...!!!");
       }
    }
}

Tapi yang harus diperhatikan karena kelas ini dimaksudkan sebagai library maka dia tidak dapat dijalankan secara langsung, harus ada suatu halaman web yang menggunakannya dan halaman web tersebut dipanggil lewat browser barulah kita akan melihat hasilnya.
Masih banyak hal yang harus diperhatikan dalam kaitannya dengan penggunaan web part, diantaranya bagaimana menggunakannya melalui halaman web dan juga konfigurasi yang diperlukan. Sampai jumpa lagi

Posted in .NET | Tagged: , , , , , | Leave a Comment »

.NET Remoting, a First Maneuver

Posted by triaslama on February 21, 2007

After my previous simple article in introduction and preliminary of .NET remoting, now let’s get coding, cause quite often that the code makes us more understand about some concepts than a thousand of words.

We will make a simple, cannonical program, call this ‘hello world’ program that executed remotely, the program consist of, at least, three portions, server, remotable object, and client.

The server and client communicate through a channel, and when a request for a remote object come the proxy is created, this proxy will acts as if remote object.

An important thing here that we will try not distributed all of the implementation, but give the clients an interface (or abstract base class that in some cases behaves like interface). So here we define an interface for the remote object, the code will look like this :

// IRemoteObject.cs

using System;

namespace SimpleLibrary

{
public interface IRemoteObject
{
string HelloWorld();
}
}

Compile the above program : csc /t:library IRemoteObject.cs
Put IRemoteObject.cs in a folder and named this folder as ‘Server’.
Next step is let’s write the implementation of IRemoteObject, the important note here is that implementation will reside in server, because distributes the full implementation are often bad ideas, why? first, that make the aims of distributed programming futile, second, distributes full implementation will take us in more problem of versioning, third distribute full library implementation make possible to disassembly the library using disassembler tools (ie. ildasm), and more reasons that ensure us to give the client program only what they need, I mean as much as possible give the interfaces not implementations.
The implementation is quite simple, of course because this is a ‘hello world’ program, remember this is first maneuver :

// SimpleLibrary.cs

using System;

namespace SimpleLibrary
{
public class RemoteObject : MarshalByRefObject, IRemoteObject
{
public RemoteObject()
{
Console.WriteLine(“RemoteObject.ctor()”);
}

public string HelloWorld()
{
return “Hello, world!!!”;
}
}
}

Compile the above program : csc /t:library SimpleLibrary.cs

Next lets build an host application for that program :

//Puts the following code in folder that was created (Server), and named Server.cs //csc /r:IRemoteObject.dll,SimpleLibrary.dll Server.cs

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using SimpleLibrary;

namespace ServerSide
{
class Server
{
static void Main()
{
HttpChannel chnl = new HttpChannel(8888);
ChannelServices.RegisterChannel(chnl);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(RemoteObject),
“first.rem”,
WellKnownObjectMode.SingleCall
);

// keeps the server running until key press

Console.ReadLine();
}
}
}

Now, the next is build the client that will consume our remote object, for simplicity we will build the client as console application, but we can build the client on the other form (ie. WinForms, with some modifications). Create a folder and named it Client, put the Client.cs (code that will consume remote object). But before go through the client code we need to copy the IRemoteObjcet.dll to the Client folder.

// Client.cs [csc /r:IRemoteObject.dll Client.cs]

using System;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Http;

using SimpleLibrary;

namespace ClientSide

{

class Client

{
static void Main()

{

IRemoteObject remObj = (IRemoteObject) Activator.GetObject(typeof(IRemoteObject), “http://localhost:8888/first.rem&#8221;);
Console.WriteLine(remObj.HelloWorld());

}

}

}

To run the programs, open a command prompt that point to the location where the server.exe resides (in the above example inside Server folder), simply write Server.exe and run it.

Open the other command prompt that point to the location of client.exe (in example in Client folder), write client.exe and run it.

The important thing to be noted here is that the server must run first before the client, or we will get an exception.

Well, the simple ‘hello world’ remoting program has accomplished…

Posted in .NET, Programming | Tagged: , , , , | 4 Comments »

Basic .NET Remoting Stuffs

Posted by triaslama on February 14, 2007

Remoting can be thinked in three building blocks. Here are the brief explanations each of them.

1. Remotable objects, this is object that can be called across application domain boundaries, the most common object are not remotable, we must make an object remotable by value or by reference. By value object can be achieved by marked them as Serializable (with [Serializable] attribute), or that object implements ISerializable. By reference can be achieved by derived them from MarshalByRefObject.

2. Host for remotable objects, the host application in .NET remoting can be vary, the host can be console applications, windows applications, windows service, or ASP.Net applications. The host applications can be thinked as listener program that controls the access to remotable objects and waiting for client requests.

3. Client applications, the clients can be vary, that is the programs that consumes the remotable objects.

In much situations the .net remoting consists of these three things. How the simple scenario of remoting works? Wait for the next of my posts, Insya Allah.

Posted in .NET, Programming | Tagged: , , , , | Leave a Comment »

Another Interesting Stuff : .NET Remoting

Posted by triaslama on February 3, 2007

Preliminary

.NET Remoting is a kind of Inter Process Communication (IPC), that intended to communicate among processes on .net environment. This works much easily in .net, the scenario is like this, we have a listener program, and a client that will initiate communication.

Communication in .net remoting accomplished through what is called a proxy. A proxy is an object that impersonates the remote object (that is object in another process), so the client will behaves as if they have the remote object, here I called ‘impersonates’ because the client doesn’t really has the remote object, the client could be only has the metadata or the interfaces but not implementations.

The concepts of .net remoting actually similar with Web Services, but they both not actually same, .net remoting is more intended to communicate among .net processes, processes that communicate located on different Application domains (can be in the same computer, or on separate computer that connected through network).

Yeah, the article above is the first sight on .net remoting, and I hope can get more interesting stuffs…

Posted in .NET, Programming | Tagged: , , , , | Leave a Comment »