I am attempting to multiply the values of dropDown to the user's inputted value in daysRenting. I am getting a format error with respect to the data types being used. dropDown uses doubles and daysRenting is an int. My problem lies in this line of code... How can I rewrite this to convert my data into the forms needed? The result will be a double. dropDown values are a daily charge for a selected car.
protected void button_Click(object sender, EventArgs e)
{
int i = Convert.ToInt32(dropDown.SelectedItem.Value.ToString()) * Convert.ToInt32(daysRenting.Text.ToString());
totalCost.Text = i.ToString();
}
Here's the full code for reference.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Assignment5.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="dropDown" runat="server" AutoPostBack="True" OnSelectedIndexChanged="dropDown_SelectedIndexChanged">
<asp:ListItem Value="null">Pick A Car Type</asp:ListItem>
<asp:ListItem Value="29.99">Compact</asp:ListItem>
<asp:ListItem Value="34.99">Intermediate</asp:ListItem>
<asp:ListItem Value="24.99">Economy</asp:ListItem>
<asp:ListItem Value="44.99">Standard</asp:ListItem>
<asp:ListItem Value="49.99">Full Size</asp:ListItem>
</asp:DropDownList>
<br/><br/>
Days Renting:<asp:TextBox ID="daysRenting" width="150px" runat="server" </asp:TextBox>
<br /><br/>
</div>
<asp:Button ID="button" runat="server" Text="Submit" OnClick="button_Click" />
<br/><br/>
Total Cost:<asp:TextBox ID="totalCost" ReadOnly="true" runat="server" OnTextChanged="totalCost_TextChanged"></asp:TextBox>
</form>
</body>
</html>
and here's the .cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Assignment5
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void dropDown_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void button_Click(object sender, EventArgs e)
{
int i = Convert.ToInt32(dropDown.SelectedItem.Value.ToString()) * Convert.ToInt32(daysRenting.Text.ToString());
totalCost.Text = i.ToString();
}
protected void totalCost_TextChanged(object sender, EventArgs e)
{
}
}
}