📄 cardetail.aspx
字号:
btnCancel.Enabled = true;
// show Save button
btnSave.Visible = true;
}
else {
// display the error details in the page
lblTerms.Text = " ";
lblFinanceResult.Text = "<b>" + sError + "</b>";
lblFinanceInfo.Text = "";
// hide Save button
btnSave.Visible = false;
}
}
// -------------------------------------------------------------------
// function to calculate payment terms from current user selections
String CalculateFinanceTerms() {
Decimal fInterestRate;
Decimal fBasePrice, fTotalInterest, fMonthlyPayment;
Decimal fTotalPrice, fPaymentPer1000;
int iMonths;
// get interest rate from web.config file
try {
fInterestRate = Decimal.Parse(ConfigurationSettings.AppSettings["XroxCarsInterestRate"]);
}
catch (Exception e) {
return "Interest Rate Error in web.config file";
}
// get basic price including extras from prices array
try {
fBasePrice = Decimal.Parse(aCurrentPrice[0]);
}
catch (Exception e) {
return "Basic Price Error in Session Array";
}
// see which payment option is selected
if (optByAmount.Checked) { // paying a specified amount each month
try {
// get monthly payment from text box as a Decimal number
fMonthlyPayment = Decimal.Parse(txtMonthPayment.Text);
}
catch (Exception e) {
return "The value for the monthly payment is not a valid number.";
}
// calculate the number of payments required and other values
// parameter fMonthlyPayment is ByRef and will be updated within
// the function to show actual payment required
iMonths = GetNumberPayments(fBasePrice, ref fMonthlyPayment);
// see if the resulting term is acceptable
if ((iMonths == 0) || (iMonths > 120)) {
return "The monthly payment specified is not sufficient to repay the loan.";
}
}
else { // paying over a specified number of months
try {
// get values from drop-down list
iMonths = Int32.Parse(lstFinanceMonths.SelectedItem.Text);
fPaymentPer1000 = Decimal.Parse(lstFinanceMonths.SelectedItem.Value);
// calculate payment for this vehicle price
fMonthlyPayment = fPaymentPer1000 * fBasePrice / 1000;
}
catch (Exception e) {
return "The value for the number of months is not a valid number.";
}
}
// calculate total price
fTotalPrice = fMonthlyPayment * iMonths;
fTotalInterest = fTotalPrice - fBasePrice;
// fill in the finance terms section of the array of values
aCurrentPrice[1] = fInterestRate.ToString();
aCurrentPrice[2] = fTotalInterest.ToString("#,##0.00");
aCurrentPrice[3] = fTotalPrice.ToString("#,##0.00");
aCurrentPrice[4] = iMonths.ToString();
aCurrentPrice[5] = fMonthlyPayment.ToString("#,##0.00");
// update Session with new array values
Session[sPricesSessionKey] = aCurrentPrice;
return ""; // empty string indicates all worked OK
}
// -------------------------------------------------------------------
// get number of payments required given desired monthly payment
// by searching through PMT values in DataSet table (alternatively
// could do it by iterating the contents of the DropDownList)
// Note that fMonthlyPayment parameter is ByRef and the value will
// be updated within function to actual payment required
int GetNumberPayments(Decimal fBasePrice,ref Decimal fMonthlyPayment) {
Decimal fPaymentPer1000;
// calculate the maximum payment per 1000 acceptable to client
fPaymentPer1000 = fMonthlyPayment * 1000 / fBasePrice;
// create suitable filter and sort criteria to get array of rows
// where first contains highest acceptable payment per 1000
String sFilter = "Payment <= " + fPaymentPer1000;
String sSort = "Payment DESC";
try {
// get reference to table in DataSet and select matching rows
DataTable oDTFinance = oCarsDS.Tables["FinancePMTRates"];
DataRow[] aRows = oDTFinance.Select(sFilter, sSort);
// calculate payment for this vehicle price using actual
// payment required from second column of data row
fMonthlyPayment = (Decimal)aRows[0]["Payment"] * fBasePrice / 1000;
// extract number of months from first column of row
return (int)aRows[0]["Months"];
}
catch (Exception e) {
// return zero if cannot calculate the result
// will occur if payment is too low (no data rows selected)
return 0;
}
}
// -------------------------------------------------------------------
// routine to clear any current finance terms for "Cancel" button
void ClearFinanceTerms(object oSender, EventArgs oArgs) {
// clear the finance terms section of the array of values
aCurrentPrice[1] = "";
aCurrentPrice[2] = "";
aCurrentPrice[3] = "";
aCurrentPrice[4] = "";
aCurrentPrice[5] = "";
// update Session with new array values
Session[sPricesSessionKey] = aCurrentPrice;
// clear any current finance terms text from page
lblTerms.Text = " ";
lblFinanceResult.Text = "Select the payment options you prefer and click <b>Calculate</b>.";
lblFinanceInfo.Text = "";
// disable the "Cancel" button
btnCancel.Enabled = false;
}
// -------------------------------------------------------------------
// routine to update price as optional extras are selected
void SelectExtras(object oSender, EventArgs oArgs) {
CalculateCarPrice();
}
// -------------------------------------------------------------------
// routine to select the color chosen by the user and update car image
void SelectColor(object oSender, ImageClickEventArgs oArgs) {
HtmlInputImage oCtrl = (HtmlInputImage)oSender;
tclColor.BgColor = oCtrl.Alt;
CalculateCarPrice();
}
// -------------------------------------------------------------------
// routine to show range of colors when a Standard/Metallic is selected
void SelectColorType(object oSender, EventArgs oArgs) {
// get reference to table in the DataSet
DataTable oDTColor = oCarsDS.Tables["CarColors"];
// fill the DataList of colors depending on setting of
// the Standard/Metallic radio buttons
DataView oDTView = oDTColor.DefaultView;
oDTView.RowFilter = "IsMetallic = " + optColorType.SelectedIndex;
dlsColors.DataSource = oDTView;
dlsColors.DataBind();
oDTView.RowFilter = "";
}
// -------------------------------------------------------------------
// routine to select row in DataGrid when an engine is selected
void SelectEngine(object oSender, EventArgs oArgs) {
dgrEngine.SelectedIndex = optEngine.SelectedIndex;
CalculateCarPrice();
}
// -------------------------------------------------------------------
// routine to interact with tree view controls on "Details" page so
// that only one node is open at a time. Only affects non-IE 5.5
// browsers, which handle updates to the display server-side
// routine runs whenever a node is expanded to show its contents
void NodeExpanded(object oSender, TreeViewClickEventArgs oArgs) {
// iterate through all the top-level child nodes of the control
TreeView oCtrl = (TreeView)oSender;
foreach (TreeNode oTopNode in oCtrl.Nodes) {
// if it is not the node that was just expanded, collapse it
if (oTopNode != oCtrl.GetNodeFromIndex(oArgs.Node)) {
oTopNode.Expanded = false;
}
}
}
// -------------------------------------------------------------------
// function to get selected car details as a DataSet
// DataSet is cached in user's Session after first access
DataSet GetCarDetailsDS(String sCarID) {
// to hold DataSet for the results
DataSet dsCarDetail = null;
// to hold Session key for DataSet
String sSessionKey = "XroxCar" + sCarID + "Details";
// try and get DataSet from user's Session
dsCarDetail = (DataSet)Session[sSessionKey];
if (dsCarDetail == null) { // not in Session - get from database
// to hold stored procedure names
String sCarProcName = "GetAllCarDetails";
String sColorProcName = "GetAllCarColors";
String sExtrasProcName = "GetAllCarExtras";
String sFinancePMTProcName = "GetFinancePMTRates";
String sConnect = ConfigurationSettings.AppSettings["XroxCarsConnectString"];
SqlConnection sqlConn = new SqlConnection(sConnect);
SqlCommand sqlComm = new SqlCommand(sCarProcName, sqlConn);
sqlComm.CommandType = CommandType.StoredProcedure;
try {
// create a new empty DataSet for the results
dsCarDetail = new DataSet();
// and fill it from the database
sqlComm.Parameters.Add("@CarID", sCarID);
SqlDataAdapter daCarDetail = new SqlDataAdapter(sqlComm);
sqlConn.Open();
daCarDetail.Fill(dsCarDetail, "CarDetails");
sqlComm.CommandText = sColorProcName;
daCarDetail.Fill(dsCarDetail, "CarColors");
sqlComm.CommandText = sExtrasProcName;
daCarDetail.Fill(dsCarDetail, "CarExtras");
sqlComm.CommandText = sFinancePMTProcName;
sqlComm.Parameters.Clear();
daCarDetail.Fill(dsCarDetail, "FinancePMTRates");
// save DataSet in Session for next postback
Session[sSessionKey] = dsCarDetail;
}
catch (Exception e) {
return null;
}
finally {
sqlConn.Close();
}
}
return dsCarDetail;
}
// -------------------------------------------------------------------
// routine to save current configuration of vehicle in database
void SaveCarDetails(object oSender, ImageClickEventArgs oArgs ) {
// collect the values we'll need for the tblQuoteOrder table
String sAnonUserID = (String)Session["WccUserID"];
int iCarID = Int32.Parse(sCarID);
String sCarName = (String)oCarsDS.Tables["CarDetails"].Rows[0]["Model"];
Decimal dBasePrice = Decimal.Parse(aCurrentPrice[0]);
Decimal dTotalPrice = dBasePrice;
Decimal dInterest = 0;
Decimal dPaymentAmount = 0;
int iPaymentMonths = 0;
if (aCurrentPrice[2] != "") {
dInterest = Decimal.Parse(aCurrentPrice[2]);
dTotalPrice = Decimal.Parse(aCurrentPrice[3]);
iPaymentMonths = Int32.Parse(aCurrentPrice[4]);
dPaymentAmount = Decimal.Parse(aCurrentPrice[5]);
}
int iEngineID = int.Parse(optEngine.SelectedItem.Value);
String sEngineName = optEngine.SelectedItem.Text;
// extract color from DataSet as in CalculateCarPrice routine
String sColorName = tclColor.BgColor;
String sFilter = "Color = '" + sColorName + "'";
DataRow[] aRows = oCarsDS.Tables["CarColors"].Select(sFilter, "");
int iColorID = (int)aRows[0]["ColorID"];
// specify the stored procedure names
String sSaveQuote = "InsertNewQuote";
String sSaveExtraLine = "InsertNewQuoteExtraLine";
// create connection and command objects
String sConnect = ConfigurationSettings.AppSettings["XroxCarsConnectString"];
SqlConnection sqlConn = new SqlConnection(sConnect);
SqlCommand sqlComm = new SqlCommand(sSaveQuote, sqlConn);
sqlComm.CommandType = CommandType.StoredProcedure;
// declare a variable to hold a Transaction object
SqlTransaction oTransaction = null;
try {
// open connection and start a transaction
sqlConn.Open();
oTransaction = sqlConn.BeginTransaction();
// attach transaction to Command object
sqlComm.Transaction = oTransaction;
// add parameters to Command
sqlComm.Parameters.Add("@UserID", sAnonUserID);
sqlComm.Parameters.Add("@CarID", iCarID);
sqlComm.Parameters.Add("@CarName", sCarName);
sqlComm.Parameters.Add("@EngineID", iEngineID);
sqlComm.Parameters.Add("@EngineName", sEngineName);
sqlComm.Parameters.Add("@ColorID", iColorID);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -