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"];
}