Monday, December 31, 2007

Maintaining Control Values Between Pages

Here is a quick example of how you can maintain the values of controls when switching back and forth between different web pages by making use of Session variables. You can use ASP.NET Session variables to pass the values from one web page to another, but if you need to return to the previous page via a button for example the web page will loose the previous values it had. You can maintain those values just as if you had clicked your browser's back button by using Session variables.

Below is a short example that demonstrates this.

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="FNLabel" runat="server" Text="First Name"></asp:Label>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<asp:Label ID="LNLabel" runat="server" Text="Last Name"></asp:Label><br />
<asp:TextBox ID="FNTextBox" runat="server"></asp:TextBox>
<asp:TextBox ID="LNTextBox" runat="server"></asp:TextBox>&nbsp;<br />
<br />
<asp:DropDownList ID="IncomeDropDownList" runat="server" AutoPostBack="True">
<asp:ListItem>10K -30K</asp:ListItem>
<asp:ListItem>31K - 50K</asp:ListItem>
<asp:ListItem>51K - 70K</asp:ListItem>
<asp:ListItem>71K - 90K</asp:ListItem>
<asp:ListItem>91K+</asp:ListItem>
</asp:DropDownList><br />
<br />
<asp:RadioButtonList ID="ResidenceRadioButtonList" runat="server" AutoPostBack="True">
<asp:ListItem>Apartment</asp:ListItem>
<asp:ListItem>Mobile Home</asp:ListItem>
<asp:ListItem>Townhouse</asp:ListItem>
<asp:ListItem>Single Family House</asp:ListItem>
</asp:RadioButtonList><br />
<asp:Calendar ID="AcctStartCalendar" runat="server" BackColor="White" BorderColor="#999999" CellPadding="4" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="180px" Width="200px">
<SelectedDayStyle BackColor="Green" Font-Bold="True" ForeColor="White" />
<TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
<SelectorStyle BackColor="#CCCCCC" />
<WeekendDayStyle BackColor="#FFFFCC" />
<OtherMonthDayStyle ForeColor="#808080" />
<NextPrevStyle VerticalAlign="Bottom" />
<DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
<TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
</asp:Calendar>
<br />
<asp:Button ID="SubmitButton"
runat="server" OnClick="Button1_Click" Text="Submit" /></div>
<br />
</form>
</body>
</html>
Default.aspx.cs
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
LoadExistingValues();
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["value1"] = FNTextBox.Text;
Session["value2"] = LNTextBox.Text;
Session["value3"] = IncomeDropDownList.SelectedValue;
Session["value4"] = ResidenceRadioButtonList.SelectedValue;
Session["value5"] = AcctStartCalendar.SelectedDate;

Response.Redirect("~/Page2.aspx");
}

private void LoadExistingValues()
{
if (!Page.IsPostBack)
{
if (Session["value1"] != null)
{
FNTextBox.Text = Session["value1"].ToString();
LNTextBox.Text = Session["value2"].ToString();
IncomeDropDownList.SelectedValue = Session["value3"].ToString();
ResidenceRadioButtonList.SelectedValue = Session["value4"].ToString();

AcctStartCalendar.SelectedDate = Convert.ToDateTime((Session["value5"]));
}
}
}
}
Page2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Page2.aspx.cs" Inherits="Page2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="FNLabel" runat="server"></asp:Label>,
<asp:Label ID="LNLabel" runat="server"></asp:Label><br />
<br />
<asp:Label ID="IncomeLabel" runat="server"></asp:Label>
<br />
<asp:Label ID="ResidenceLabel" runat="server"></asp:Label><br />
<br />
<asp:Calendar ID="AcctSelectionCalendar" runat="server"></asp:Calendar>
<br />
<br />
&nbsp;
<asp:Button ID="PreButton" runat="server" OnClick="Button1_Click" Text="Return to previous page" /></div>
</form>
</body>
</html>
Page2.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Page2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FNLabel.Text = (Session["value1"]).ToString();
LNLabel.Text = (Session["value2"]).ToString();
IncomeLabel.Text = (Session["value3"]).ToString();
ResidenceLabel.Text = (Session["value4"]).ToString();
AcctSelectionCalendar.SelectedDate = Convert.ToDateTime((Session["value5"]));

}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/Default.aspx", true);
}
}
This little example should show you how easy it is to maintain the values of controls when navigating back and forth between pages. I have not experimented extensively with other controls, so further tweaking may be required.



Wednesday, December 12, 2007

Running ASP.NET 2.0 in Integrated Mode on IIS 7

I just wanted to share this blog link that discusses the changes you'll need to be aware of when you running your ASP.NET 2.0 applications in Integrated Mode on IIS 7 on Vista or Windows Server 2008.

http://mvolo.com/blogs/serverside/archive/2007/12/08/IIS-7.0-Breaking-Changes-ASP.NET-2.0-applications-Integrated-mode.aspx

Friday, October 26, 2007

.NET Daily Tips

I stumbled across this website and thought I'd share it with everyone. Visit this site frequently for some little known tips you might not have ever known about regarding .NET

http://dotnettipoftheday.org/tips/

Tuesday, September 11, 2007

Resetting Visual Studio 2005 to Original Settings

NOTE: Issuing the following command will clear any custom and environmental settings.

You may come across a situation where your Visual Studio 2005 Environment is non-functional in one manner or another. This recently happened to me when I was trying to reset the environment to the General Environment settings using the Import and Export Settings Wizard. During the process I encountered an error which left the IDE in a state in which it would try to Initialize the Toolbox and after a few seconds close Visual Studio. Thus I was unable to access any of the ToolBox controls.

After a quick search I came across a command that you run from the Visual Studio Command Prompt to reset the IDE to its original install settings. If after exhausting all possible repair options you need to run this command please backup any custom and/or environmental settings you do not want to lose prior to its use.

Then perform the following:

1. Click Start and then select All Programs.
2. Select Microsoft Visual Studio 2005.
3. Select Visual Studio Tools and then Visual Studio 2005 Command Prompt.
4. Enter devenv /resetuserdata.

You will not receive any confirmation of reset completion, so you can use task manager to monitor the devenv process for its completion.

Friday, August 10, 2007

Symbols, Accents, Punctuation, and Foreign Characters

Have you ever needed to type special symbols or characters such as the Registered Trademark Symbol, Copyright Symbol, or perhaps an Umlaut or the Math Division Symbol in a document or web page? Trying to remember the codes that represent them or having to reference some web page that list them can be really impractical. There is a much easier way to access these symbols.

Many people are unaware of a small program that comes with Windows and Linux Distributions that provides them for you along with their associated codes. This same type of program I'm sure comes with OSX as well as other Operating Systems; however, I am unable to verify this due to my lack of access to the numerous number of Operating Systems available.

In Windows 2000/XP/Vista

1. Click the Start Button.
2. Select All Programs.
3. Select Accessories.
4. Select System Tools.
5. Select Character Map.

In Ubuntu Linux (Other Linux Distros may have similar steps)

1. Click Applications from the Task Bar.
2. Select Accessories.
3. Select Character Map.

Whenever you need to make use of a special symbol or foreign character just to mention a couple types; this is a very simple way that is readily available to you. No need to search for the codes for such symbols via the Internet or consult some other reference material.

Wednesday, July 25, 2007

Accessing Master Page Controls via Content Pages

There are a couple of ways to access the controls of Master Pages in ASP.NET 2.0 from within your content pages. You can do this in a non-strongly typed manner or a strongly type manner. I will provide a sample below that demonstrates each approach.

Non-Strongly Typed

Lets say your Master Page contains a Label that displays the current Date. Within your content page use the FindControl method to locate the control in the Master Page whose property you want to access. Note you must perform an explicit cast.

Label lbl = (Label)Master.FindControl("DateLabel");
lbl.Text = DateTime.Today.ToLongDateString();

Strongly Typed

In the code-behind of your Master Page create a property that accesses the control you want to manipulate.

public string GetCurrentDate
{
get { return DateLabel.Text; }
set { DateLabel.Text = value; }
}


In the HTML source of your content page add the following beneath the Page <%@ Page %><%@ Page %><%@ Page %>directive. Add MasterType before TypeName.


< typename="Site">

Replace "Site" with the class name of your Master Page. You could also re-write the above using VirtualPath in place of TypeName. NOTE: Blogger will not display the percent signs that are between the <> as well it also Strips out the MasterType Directive.

To do this just replace the MasterPage.master with the name of your Master Page.

Finally in the code-behind of your content page you can set the DateLabel to the current date on your Master Page as below:

this.Master.GetCurrentDate = DateTime.Today.ToLongDateString();

Thursday, July 12, 2007

Free Web Services and SQL Connection Strings

I wanted to share the following resources I came across hopefully if you ever need them they'll be helpful to you.

Web Services

http://www.webservicex.net/WCF/default.aspx
http://www.xmethods.net/
http://www.webservicelist.com/

SQL Connection Strings

http://www.connectionstrings.com/
http://www.carlprothman.net/Default.aspx?tabid=81