正文

Hash Table

(2007-09-08 21:34:25) 下一個
MCAD/MCSD DEVELOPING WEB APPLICATION WITH VISUAL BASIC.NET and VISUAL C#.NET is a very good book to start web programming with, While http://msdn2.microsoft.com/en-us/library/ is a very good website for reference.

Well, they want a form or table, where user can enter data, say, 3 columns and 4 rows.

Notification type Notification information Preference
Text Message ____________________ _________
Fax ____________________ _________
Tel ____________________ _________

Button

After entering information, user will click the button and a message will be sent into some table in sql server database. Preference is a number 1-5. Duplicated numbers are not allowed. We can’t have both Tel and Fax as 1. Message should be sorted, like: 1 Notification information Notification type. 2 …. Null Preference is allowed.When users click the button, I create a hash table and put data into hash table. Hash table is a collection of key/value pair where, given a key it can retrieve the corresponding value. Hash table throw exception at duplicated value. So when duplicated values are entered, catch block use a piece of javascript(asp.net can’t pop up windows) to pop up a window to inform users. Hash table won’t sort. So I put the key of hash table in an arraylist, and use sort() method of arraylist to sort the key and retrieve its corresponding value and append it to string.

Here, I simply print the message instead of inserting into database.
The lab contains 3 files. Default.aspx, Default.aspx.cs, and Web.Config. Web.Config is the default one. I didn’t do anything to it.

Default.aspx.cs:

using System;
using System.Data;
using System.Configuration;
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;
using System.Text;
using System.Collections;

public partial class _Default : System.Web.UI. Page

{

protected void Page_Load( object sender, EventArgs e)

{

}

protected void btn1_Click( object sender, EventArgs e)

{
string Preference = "" ;
StringBuilder sb = new StringBuilder ();

string TxtMessage = TxtBox1.Text;
string CellPhone = TxtBox2.Text;
string EmailContact = TxtBox3.Text;
string WorkPhone = TxtBox4.Text;
string Fax = TxtBox5.Text;

string pref1 = ". TextMessage: " + TxtMessage + "; " ;
string pref2 = ". CellPhone: " + CellPhone + "; " ;
string pref3 = ". Email: " + EmailContact + "; " ;
string pref4 = ". WorkPhone: " + WorkPhone + "; " ;
string pref5 = ". Fax: " + Fax + "; " ;

Hashtable Zhash = new Hashtable ();
try

{

if (DropDownList1.SelectedItem.Text != null &
DropDownList1.SelectedItem.Text != "" )

Zhash.Add(DropDownList1.SelectedItem.Text, pref1);

if (DropDownList2.SelectedItem.Text != null &
DropDownList2.SelectedItem.Text != "" )

Zhash.Add(DropDownList2.SelectedItem.Text, pref2);

if (DropDownList3.SelectedItem.Text != null &
DropDownList3.SelectedItem.Text != "" )

Zhash.Add(DropDownList3.SelectedItem.Text, pref3);

if (DropDownList4.SelectedItem.Text != null &
DropDownList4.SelectedItem.Text != "" )

Zhash.Add(DropDownList4.SelectedItem.Text, pref4);

if (DropDownList5.SelectedItem.Text != null &
DropDownList5.SelectedItem.Text != "" )

Zhash.Add(DropDownList5.SelectedItem.Text, pref5);

ArrayList sorter = new ArrayList (Zhash.Keys);

sorter.Sort();

foreach ( string key in sorter)

{

sb.Append(key);
sb.Append(Zhash[key]);

}

Preference = sb.ToString();
Response.Write(Preference);

}

catch ( ArgumentException ex)

{

string JString = " " ;
Page.ClientScript.RegisterStartupScript( this .GetType(), "PopupScript" , JString);

}

}

}

Can't post aspx page. It shows as a table instead of code. I just create a table drag and drop textboxes and dropdownlists into table cells.  In the catch block above, "string JString" should be initialized. Instead of showing the code it pops up a window here. When I first did the work, it shows the code but doesn't pop up windows. I feel I am both a bad blogger  and a bad programmer.  :) Will find a way to post it.




[ 打印 ]
閱讀 ()評論 (0)
評論
目前還沒有任何評論
登錄後才可評論.