...the way I did that - learning through examples...

воскресенье, 17 января 2010 г.

Save cookie sample




When a user visits your website, you can use cookies to store user login (storing a password is unsecure!) or user preferences or other information. When the user visits your Web site another time, the application can retrieve the information it stored earlier.

This sample shows how to save cookie and how to retrive it when you visit site another time.

protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["MyCookie"] != null)
txtSavedCookie.Text = "Cookie is: " + Request.Cookies["MyCookie"].Value;
else
txtSavedCookie.Text = "No cookies!";
}

protected void btnSaveCookie_Click(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("MyCookie", txtCookieValue.Text);
Response.AppendCookie(cookie);

txtResult.Text = "Cookie saved!! Close this site and open it again - you will see saved cookie value at the top of this page!";
}

All our simple samples available as Microsoft Visual Studio 2008 website projects.
You can DOWNLOAD this Save cookie sample project from Rapidshare.

DateTime sample




If you want to display current date or time on the website you can use DateTime.Now property.
This sample shows how to display it using 5 different formats.

protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.DayOfWeek.ToString();//return the current day of week
Label2.Text = DateTime.Now.ToLongDateString(); //return the "long date"
Label3.Text = DateTime.Now.ToShortDateString(); //return the "short date"
Label4.Text = DateTime.Now.ToLongTimeString(); //return the "long time"
Label5.Text = DateTime.Now.ToShortTimeString(); //return the "short time"

}

All our simple samples available as Microsoft Visual Studio 2008 website projects.
You can DOWNLOAD this DateTime sample project from Rapidshare.

пятница, 8 января 2010 г.

String trim sample




If you need to trim string and remove extra spaces from the middle of the string
you can use this simple sample:

string s = TextBox1.Text;
s = s.Trim(); // trim left and right spaces

while (s.IndexOf(" ") != -1) // it is 2 spaces in the quotes!!
{
s = s.Remove(s.IndexOf(" "), 1); // remove extra spaces in the middle of the string
}

Label1.Text = s;

It will take any input string from the TextBox1, i.e. like this:

"___aaa_______bbb_________ccc_ddd__eee_",

where "_" is a space symbol, and trim it to the:

"aaa_bbb_ccc_ddd_eee".

All our simple samples available as Microsoft Visual Studio 2008 website projects.
You can DOWNLOAD this String trim sample project from Rapidshare.

понедельник, 4 января 2010 г.

File upload sample



In ASP.NET websites you can upload file on server using the FileUpload web UI control.
(You can drag and drop the control from the Toolbox on the Web page in Design mode.)
The user specifies the file to upload by selecting the file by clicking the Browse button, and then locating it in the Choose File dialog box.
The FileUpload control does not automatically save a file to the server after the user selects the file to upload. You must provide a button that the user clicks to upload the file, in our sample it will be "Upload" button. Here is a code of the Upload button handler from our sample:
if (FileUpload1.HasFile)                          //  check whether the selected file
{
string path = @"C:\Received Files\"; // This specifies the path to the destination
path += FileUpload1.FileName; // add the file name to the path
FileUpload1.PostedFile.SaveAs(path); // ... and save
Label1.Text = FileUpload1.PostedFile.ContentLength.ToString() + " Bytes saved";
}
else
{
Label1.Text = "You have not selected a file to upload";
}

All our simple samples available as Microsoft Visual Studio 2008 website projects.
You can DOWNLOAD this File upload sample project from Rapidshare.