Read, With the Name of Your Lord Who Created

Posts Tagged ‘.NET’

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 »

Advertisements

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: , , , , | 67 Comments »

Populate Web Part Melalui Share Point 2007

Posted by triaslama on November 28, 2007

Setalah pada artikel sebelumnya telah dibahas tentang bagaimana langkah – langkah konfigurasi yang diperlukan untuk membuat web part dengan visual studio 2005 dan share point 2007, maka pada artikel ini akan dibahas tentang langkah – langkah yang diperlukan untuk menyiapkan web part yang telah dibuat agar dapat ditempatkan pada suatu halaman web ASP.NET.
Untuk dapat men-deploy / mem-populate web part, kita harus melakukan login sebagai administrator. Selanjutnya ketikkan alamat server pada browser, masukkan user name sehingga akan memunculkan halaman dengan menu Site Actions pada bagian kanan atas.

Untuk mem-populate suatu web part, klik bagian Site Actions > Site Settings > Modify All Settings. Hal ini ditunjukkan pada gambar berikut:

Modif All Settings

Setelah itu akan muncul halaman Site Settings sebagai berikut:

sitesetting_edited2.jpg

Pada halaman Site Settings tersebut pilih Galleries → Web Parts (ditunjukkan oleh anak panah pada gambar diatas).

Setelah link Web Parts tersebut kita pilih maka selanjutnya kita akan memasuki halaman Web Part Gallery. Pada halaman tersebut akan terlihat beberapa web part yang telah dibuat, untuk membuat web part baru dari kode program yang kita tulis, klik tombol New seperti ditunjukkan pada gambar berikut ini:

webpartgallery.jpg

Setelah tombol New di-klik, selanjutnya kita akan memasuki halaman Web Part Gallery: New Web Parts. Pada halaman ini kita dapat menambahkan web part baru yang selanjutnya web part yang ditambahkan tersebut akan ditampilkan pada halaman Web Part Gallery.

Untuk menambahkan satu atau lebih web part baru beri check list pada check box yang tersedia pada samping kiri dari web part yang akan ditambahkan selanjutnya klik tombol Populate Gallery. Hal ini dicontohkan pada gambar berikut ini:

newwebpartgallery.jpg

 

Setelah tombol Populate Gallery di-klik maka proses populate web part telah selesai.

Note: web part yang terdapat pada halaman Web Part Gallery: New Web Parts akan bertambah setiap kali kita melakukan build melalui Visual Studio yang output path-nya telah diarahkan ke lokasi drive server. Selanjutnya web part yang kita buat akan diberi nama dengan nama yang terdapat pada text box File Name, secara default web part akan diberi nama sama dengan nama kelas yang kita buat. Ganti nama default yang diberikan jika kita menginginkan nama yang lain. Untuk mengganti (meng-override) web part yang pernah kita populate sebelumnya beri check list pada check box Overwrite if file already exists?

 

Posted in SharePoint | Tagged: , , , , , , | Leave a Comment »

Pembuatan Web Part Menggunakan Visual Studio 2005 Dan Share Point 2007

Posted by triaslama on November 27, 2007

Pembuatan dan Konfigurasi Web Part

Untuk membuat web part pada share point 2007 menggunakan Visual Studio 2005 diperlukan beberapa langkah konfigurasi sederhana. Pertama kali buat project baru pada Visual Studio 2005, pilih Create Project, selanjutnya pada Visual Studio installed templates pilih Class Library. Berikut ini langkah – langkah yang harus dilakukan untuk men-deploy web part melalui Visual Studio 2005:

  1. Pada file AssemblyInfo.cs, tambahkan code berikut:

[assembly:AllowPartiallyTrustedCallers()]

Assembly ini terletak di namespace System.Security, jadi pada bagian deklarasi namespace file AssemblyInfo.cs tambahkan namespace tersebut seperti contoh berikut:

using System.Reflection;

using System.Runtime.CompilerServices;

using System.Runtime.InteropServices;

using System.Security;

2. Pada bagian ‘Solution Explorer’ Visual Studio klik kanan Properties > Open, sehingga akan muncul jendela properties. Pada bagian Build pilih output path dengan mengarahkannya pada drive server, hal ini ditunjukkan pada gambar berikut ini:

buildjpg.jpg

Dengan mengarahkan build pada output path tersebut berarti setiap kali build atau compile untuk program yang kita buat maka proses build tersebut akan dilakukan di-drive yang disebukan pada output path tersebut (output path pada gambar diatas adalah drive tempat dimana share point 2007 berada).

3. Masih pada jendela Properties, pada tab Signing pilih checkbox untuk membuat suatu key assembly dan beri nama key tersebut, hal ini dicontohkan gambar berikut:

signing.jpg

4. Langkah selanjutnya adalah mendaftarkan assembly yang kita buat pada file web.config. File ini terletak pada virtual directory server (dalam contoh ini file web.config terletak pada drive: Z:\Inetpub\wwwroot\wss \VirtualDirectories\80). Pada web.config tambahkan beberapa informasi mengenai assembly yang dihasilkan dari kode yang telah kita tulis. Informasi – informasi tersebut meliputi: nama assembly, PublicKeyToken, namespace, dll.

 

Note: untuk memperoleh informasi mengenai assembly yang dihasilkan oleh program (meliputi nama assembly, versi, serta PublicKeyToken) gunakan suatu tools, misalnya Lutz Roeder’s .NET Reflector. Hal ini dicontohkan melalui gambar berikut ini:

lutzreflector.jpg

 

Jika langkah – langkah konfigurasi diatas telah dilakukan dengan benar, maka selanjutnya kita dapat men-deploy web part yang kita buat pada halaman web ASP.NET.

Note: kesalahan mengubah web.config dapat mengakibatkan keseluruhan aplikasi tidak berjalan, jadi selalu siapkan back-up web.config & selalu hati – hati dalam mengubah web.config.

Berikut ini contoh elemen yang harus ditambahkan dalam web.config untuk men-deploy suatu web part:

<SafeControls>

<SafeControl Assembly=”KebijakanProgramDanKegiatanDepkeu, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f1a7843868cd0ddf” Namespace=”KebijakanProgramDanKegiatanDepkeu” TypeName=”*” Safe=”True” AllowRemoteDesigner=”True” />

</SafeControls>

 

Jadi intinya perlu dilakukan penambahan tag <SafeControls /> yang berisikan beberapa informasi mengenai assembly yang akan di-deploy sebagai web part.

Pada bagian selanjutnya -Insya Allah- akan dilakukan pembahasan mengenai bagaimana mem-populate suatu web part pada halaman ASP.NET. Sampai jumpa lagi.

 

 

 

 

 

Posted in SharePoint | Tagged: , , , , , , , | Leave a Comment »

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 »