3 Replies - 1040 Views - Last Post: 02 April 2009 - 09:33 AM Rate Topic: -----

#1 sam.adams61  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 283
  • Joined: 14-July 08

Text Book code not running as it should.

Post icon  Posted 01 April 2009 - 07:54 PM

I have the following code from a project in a text book I'm using, and I have a slight problem...the line of code marked with the **** is not running as it ought, nor is the method named Show_GraphServerClick. I would greatly appreciate some help concerning this, & if it's needful, I can post the .aspx code also. Many thanks.

public partial class CurrencyConverter : System.Web.UI.Page
{
	protected void Convert_ServerClick(object sender, EventArgs e)
	{
		decimal amount = Decimal.Parse(US.Value);

		if (amount <= 0)
		{
			Result.Style["color"] = "Red";
		****Result.InnerText = "Specify a positive number";****
		}
		else
		{
			Result.Style["color"] = "Black";
		}

		//Retrieve the selected ListItem object by it's index number.
		ListItem item = Currency.Items[Currency.SelectedIndex];

		decimal newAmount = amount * Decimal.Parse(item.Value);
		Result.InnerText = amount.ToString() + " U.S. dollars = ";
		Result.InnerText += newAmount.ToString() + " " + item.Text;
		
	   
	}

	protected void Page_Load(Object sender, EventArgs e)
	{
		if (this.IsPostBack == false)
		{
			//The HtmlSelect control accepts text or ListItem objects.
			Currency.Items.Add(new ListItem("Euros", "0.85"));
			Currency.Items.Add(new ListItem("Japanese Yen", "110.33"));
			Currency.Items.Add(new ListItem("Canadian Dollar", "1.2"));
		}
		Graph.Visible = false;
	}

	protected void ShowGraph_ServerClick(Object sender, EventArgs e)
	{
		Graph.Src = "Pic" + Currency.SelectedIndex.ToString() + ".png";
		Graph.Visible = true;
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Text Book code not running as it should.

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Text Book code not running as it should.

Posted 01 April 2009 - 10:23 PM

You say "Not working as it should", can we get a little more in depth description of the issue? Thanks :)
Was This Post Helpful? 0
  • +
  • -

#3 sam.adams61  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 283
  • Joined: 14-July 08

Re: Text Book code not running as it should.

Posted 02 April 2009 - 06:51 AM

View PostPsychoCoder, on 2 Apr, 2009 - 04:23 AM, said:

You say "Not working as it should", can we get a little more in depth description of the issue? Thanks :)


If the amount entered is less than or equal to zero, the style attribute "color" works fine & the html control is indeed Red. However, the next line of code does not run at all & instead I get the last two lines of code running and being displayed in the color Red!! The final result then is, for example, ' 0 U.S. dollars = 0 Euros' (colored red).
Thanks for your response.
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Text Book code not running as it should.

Posted 02 April 2009 - 09:33 AM

View Postsam.adams61, on 2 Apr, 2009 - 08:51 AM, said:

View PostPsychoCoder, on 2 Apr, 2009 - 04:23 AM, said:

You say "Not working as it should", can we get a little more in depth description of the issue? Thanks :)


If the amount entered is less than or equal to zero, the style attribute "color" works fine & the html control is indeed Red. However, the next line of code does not run at all & instead I get the last two lines of code running and being displayed in the color Red!! The final result then is, for example, ' 0 U.S. dollars = 0 Euros' (colored red).
Thanks for your response.


that is because that code is run everytime, no matter what the amount is. move this code into the "else" block...

//Retrieve the selected ListItem object by it's index number.
		ListItem item = Currency.Items[Currency.SelectedIndex];

		decimal newAmount = amount * Decimal.Parse(item.Value);
		Result.InnerText = amount.ToString() + " U.S. dollars = ";
		Result.InnerText += newAmount.ToString() + " " + item.Text;


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1