Wednesday, February 25, 2009

Save and retrieve information from the session (by default expires in 20)

Saving in the session

protected void LoginButton_Click(object sender, EventArgs e)
{
string username = NameTextBox.Text;

//HttpCookie cookie = new HttpCookie("UserInfo");
//cookie["Username"] = username;
//cookie.Expires = DateTime.Now.AddDays(1);
//Response.Cookies.Add(cookie);

Session["Username"] = username;
Response.Redirect("OrderPage.aspx");
}


Retrieving

protected void ProceedButton_Click(object sender, EventArgs e)
{
string username = (string)Session["Username"];
string product = ProductTextBox.Text;
Session["Product"] = product;

string url = string.Format("CustomerDetails.aspx");
Response.Redirect(url);
}

Save and retrieve information from the cookie (specifying an expire time)

Saving information in the cookie and specifying an expire time

protected void LoginButton_Click(object sender, EventArgs e)
{
string username = NameTextBox.Text;

HttpCookie cookie = new HttpCookie("UserInfo");
cookie["Username"] = username;
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);

}


Retrieving information

protected void Page_Load(object sender, EventArgs e)

{
HttpCookie cookie = Request.Cookies["UserInfo"];
if(cookie!=null)
{
string username = cookie["Username"];
if (username != null)
{
Response.Redirect("OrderPage.aspx?Username=" + username);
return;
}
}
Response.Redirect("Login.aspx");
}


Tuesday, February 24, 2009

Saving value in view state and populating the text box from view state

Saving value on Button Click in view state

protected void Copy_Click(object sender, EventArgs e)
{
foreach(HtmlTableRow row in table1.Rows)
{
foreach (HtmlTableCell cell in row.Cells)
{
Control control = cell.Controls[0];
if (control is TextBox)
{
ViewState[control.ID] = ((TextBox)control).Text;
}
}
}
}


and

Populating the text box from view state on Button Click

protected void Paste_Click(object sender, EventArgs e)
{

foreach (HtmlTableRow row in table1.Rows)
{
foreach (HtmlTableCell cell in row.Cells)
{
Control control = cell.Controls[0];
if (control is TextBox)
{
string value = (string) ViewState[control.ID];
if(value != null)
{
((TextBox) control).Text = value;
}

}
}
}

}

Tuesday, February 10, 2009

Handling conflicts between Themes and Styles

By default Themes overwrite styles in your page. Theme="NameofYourSkin" But if you change it to StyleSheetTheme="NameofYourSkin" it will not

If you would like to specify theme in your web.config file you need to write nex line of code 

Sunday, February 1, 2009

Defining and Using Application settings

Definining application settings


Open web.config file of your application Find: tag
Type settings you need, For example:








Using application settings


You have to use in you code behind file reference to this namespace
using System.Configuration;


protected void Page_Load(object sender, EventArgs e)
{
string copyright = ConfigurationManager.AppSettings["CopyrightMessage"];
string email = ConfigurationManager.AppSettings["ContactEmail"];
}

Saturday, January 31, 2009

Navigating a Website

Redirectig to a different webpage

protected void LoginButton_Click(object sender, EventArgs e)
{
string username = NameTextBox.Text;
Response.Redirect("~/orderpage.aspx?username=" + username);
}

Pass Information between Pages with the QueryString

protected void CheckoutButton_Click(object sender, EventArgs e)

{
string stuff = ProductTextBox.Text;
string username = Request.QueryString["username"];
string url = "~/summarypage.aspx?username=" + username + "&product=" + stuff; Response.Redirect(url);
}

Retrieving QueryString Information with Variables

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string username = Request.QueryString["username"];
WelcomeLabel.Text += ", " + username;
}
}

protected void CheckoutButton_Click(object sender, EventArgs e)
{
string stuff = ProductTextBox.Text;
string username = Request.QueryString["username"];
string url = "~/summarypage.aspx?username=" + username + "&product=" + stuff; Response.Redirect(url);
}

Using String.Format to build Proper String

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string username = Request.QueryString["username"];
string product = Request.QueryString["product"];
WelcomeLabel.Text = String.Format("Hey thanks {0}, your {1} is on its way!!!", username, product);
}
}

Friday, January 30, 2009

Command Name properties in C#

Bad Practices
When you call "Button object" by "Text" properties;

Best Practices
When you call "Button object" by "Command Name" properties;

Saturday, January 3, 2009

Assignment and Initializers

// Declare variables.
int errorCode;
string myName;

// Assign values.
errorCode = 10;
myName = "Eugene";

or

int errorCode = 10;
string myName = "Eugene";

declare a variable

// Declare an integer variable named errorCode.
int errorCode;
// Declare a string variable named myName.
string myName;

Commenting in C#

// A single-line C# comment.


/* A multiline
C# comment. */




XML-based commenting syntax

///
/// This application provides web pages
/// for my e-commerce site.
///





“IDesign C# Coding Standard”
http://www.idesign.net/