Greetings dear experts,
I was dragged into this project because the original developer is no longer with the company.
The issue that I have here is a situation where the developer hardcoded the values of water values in the dropdown.
These are Water1, Water2 and Water3.
Here is what the current code looks like
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ToiletRebate2
{
public partial class BatchEntry : System.Web.UI.Page
{
WaterDataContext waterLinq = new WaterDataContext();
int installID = 0, mailID = 0;
string ownerID = "";
string[] missingValues = new string[8];
static int numWater = 0;
static byte num20 = 0, num30 = 0;
int valueCount = 0;
static bool resubmit = false, rereturn = false;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if ((string)Session["Type"] == "Batch")
{
// check to see if there is a return available
if ((bool)Session["ReturnAvailable"] == true) { CheckReturn(); }
else { CheckValues(); }
}
else
{
// disable the updateWaterAcctNo LinkButton
//updateWaterAcctNo.Enabled = false;
// clear the session, except for type
Master.Clear_Session(false);
// set the type to Batch
Session["Type"] = "Batch";
// set the entrydate to today
Session["DateEntered"] = entryDate.Text = DateTime.Now.ToString();
dateReceived.Text = (string)Session["DateReceived"];
if (dateReceived.Text != "") dateReceived_CalendarExtender.SelectedDate = DateTime.Parse(dateReceived.Text);
// get the current open batch nubmer
GetBatchInfo();
}
}
}
}
protected void addWater_Click(object sender, EventArgs e)
{
// get session information on rebatable waters
string[] waters = new string[3], waters = new string[3];
if ((string)Session["GPFPerWater"] != "")
{
waters = Session["GPFPerWater"].ToString().Split(',');
waters = Session["GPFPerWater"].ToString().Split(',');
}
// set the available fields based on the number of rebatable waters
switch (numWater)
{
case 0:
firstToilet.Visible = Water2.Visible = Water3.Visible = false;
break;
case 1:
firstToilet.Visible = true;
if (Water1.SelectedIndex == 0)
{
if (waters[0] != null) Water1.SelectedValue = waters[0].ToString();
if (waters[0] != null) WaterValue1.Text = waters[0].ToString();
Water2.Visible = Water3.Visible = false;
}
break;
case 2:
firstToilet.Visible = Water2.Visible = true;
if (Water1.SelectedIndex == 0)
{
if (waters[0] != null) Water1.SelectedValue = waters[0].ToString();
if (waters[0] != null) waterValue1.Text = waters[0].ToString();
if (waters[1] != null) Water2.SelectedValue = waters[1].ToString();
if (waters[1] != null) waterValue2.Text = waters[1].ToString();
Water3.Visible = false;
}
break;
case 3:
firstToilet.Visible = Water2.Visible = Water3.Visible = true;
if (Water1.SelectedIndex == 0)
{
if (waters[0] != null) Water1.SelectedValue = waters[0].ToString();
if (waters[0] != null) waterValue1.Text = waters[0].ToString();
if (waters[1] != null) Water2.SelectedValue = waters[1].ToString();
if (waters[1] != null) waterValue2.Text = waters[1].ToString();
if (waters[2] != null) Water2.SelectedValue = waters[2].ToString();
if (waters[2] != null) waterValue2.Text = waters[2].ToString();
}
break;
}
addRebateWater_ModalPopupExtender.Show();
}
protected void addWater_Click(object sender, EventArgs e)
{
string gpf = "", rpt = "", tinfo = "";
double ttl = 0;
bool okToHide = true;
if (Water1.SelectedIndex != 0)
{
if (waterValue1.Text.Contains(".") == false) waterValue1.Text = waterValue1.Text + ".00";
if (CheckToiletValue(Water1.SelectedValue, decimal.Parse(waterValue1.Text)) == true)
{
// gallons per flush
gpf += Water1.SelectedValue;
Session["GPFPerWater"] = Water1.SelectedItem.Value;
// water per water
rpt += waterValue1.Text;
Session["WaterPerToilet"] = waterValue1.Text;
// add to the total
ttl += double.Parse(waterValue1.Text);
// format the water info string
tinfo += Water1.SelectedItem + " @ " + waterValue1.Text;
if (Water1.SelectedItem.Value == "1.60") num30 += 1;
else num20 += 1;
}
else
{
Master.ShowMessage("The first water water value is higher than rules allow, please edit the value and try again");
okToHide = false;
}
}
if (Water2.Visible == true)
{
if (Water2.SelectedIndex != 0)
{
if (waterValue2.Text.Contains(".") == false) waterValue2.Text = waterValue2.Text + ".00";
if (CheckToiletValue(Water2.SelectedValue, decimal.Parse(waterValue2.Text)) == true)
{
// gallons per flush
gpf += "," + Water2.SelectedValue;
Session["GPFPerWater"] += "," + Water2.SelectedItem.Value;
// water per water
rpt += waterValue2.Text;
Session["WaterPerToilet"] += "," + waterValue2.Text;
// add to the total
ttl += double.Parse(waterValue2.Text);
// format the water info string
tinfo += "<br />" + Water2.SelectedItem + " @ " + waterValue2.Text;
if (Water2.SelectedItem.Value == "1.60") num30 += 1;
else num20 += 1;
}
else
{
Master.ShowMessage("The second water water value is higher than rules allow, please edit the value and try again");
okToHide = false;
}
}
}
if (Water3.Visible==true)
{
if (water3.SelectedIndex != 0)
{
if (waterValue3.Text.Contains(".") == false) waterValue3.Text = waterValue3.Text + ".00";
if (CheckToiletValue(water3.SelectedValue, decimal.Parse(waterValue3.Text)) == true)
{
// gallons per flush
gpf += "," + water3.SelectedValue;
Session["GPFPerWater"] += "," + water3.SelectedItem.Value;
// water per water
rpt += waterValue3.Text;
Session["WaterPerToilet"] += "," + waterValue3.Text;
// add to the total
ttl += double.Parse(waterValue3.Text);
// format the water info string
tinfo += "<br />" + water3.SelectedItem + " @ " + waterValue3.Text;
if (water3.SelectedItem.Value == "1.60") num30 += 1;
else num20 += 1;
}
else
{
Master.ShowMessage("The third water water value is higher than rules allow, please edit the value and try again");
okToHide = false;
}
}
}
if (okToHide == true)
{
waterInformation.Text = tinfo;
Session["ToiletInformation"] = tinfo;
totalWater.Text = ttl.ToString("##00.00");
Session["TotalWater"] = ttl.ToString("##00.00");
addRebateWater_ModalPopupExtender.Hide();
}
}
protected bool CheckToiletValue(string gpf, decimal amt)
{
bool valueOK = false;
switch (gpf)
{
case "1.28":
if (amt <= 100)
valueOK = true;
break;
case "1.60":
if (amt <= 50)
valueOK = true;
break;
}
return valueOK;
}
protected void addAdditional_Click(object sender, EventArgs e)
{
string gpf = "", tinfo = "";
// gallons per flush
gpf += additional1.SelectedItem;
Session["AdditionalWater"] = additional1.SelectedItem.Value;
// format the water info string
tinfo += additional1.SelectedItem;
if (additional1.SelectedItem.Value == "1.60") num30 += 1;
else num20 += 1;
if (additional2.SelectedIndex > 0)
{
// gallons per flush
gpf += "," + additional2.SelectedItem;
Session["AdditionalWater"] = "," + additional2.SelectedItem.Value;
// format the water info string
tinfo += "<br />" + additional2.SelectedItem;
if (additional2.SelectedItem.Value == "1.60") num30 += 1;
else num20 += 1;
}
if (additional3.SelectedIndex > 0)
{
// gallons per flush
gpf += "," + additional3.SelectedValue;
Session["AdditionalWater"] = "," + additional3.SelectedItem.Value;
// format the water info string
tinfo += "<br />" + additional3.SelectedItem;
if (additional3.SelectedItem.Value == "1.60") num30 += 1;
else num20 += 1;
}
additionalToiletInformation.Text = tinfo;
Session["AdditionalInformation"] = tinfo;
addAdditionalWater_ModalPopupExtender.Hide();
}
}
}
As you can see from the above code, the dropdown values of Water2, Water3,Water3. Additional1,Additional2 and Additional3 are hardcoded. Those hardcoded values are 0.20 and 0.30.
The users want to add additional values but cannot.
As a result, this was dropped on my lap.
I was able to creat a dynamically populated dropdownList with the following code:
The huge challenge I have now is how to convert the original C# code work with my version of dynamically populated DropdownList.
<center>
<ajax:ModalPopupExtender ID="addMore_ModalPopupExtender" runat="server" TargetControlID="hiddenLabel1" PopupControlID="divWater" BackgroundCssClass="popup" />
<div id="divRebatable" class="popupdiv">
<asp:Table ID="waterTable" Width="100%" runat="server">
<asp:TableRow CssClass="divHeader"><asp:TableHeaderCell ColumnSpan="4">Add RWater Size</asp:TableHeaderCell></asp:TableRow>
<asp:TableRow ID="Water1" runat="server">
<asp:TableHeaderCell>Water #1:</asp:TableHeaderCell>
<asp:TableCell>
<asp:DropDownList ID="Water1" runat="server">
<asp:ListItem Selected="True" Text="Select" Value="0" />
<asp:ListItem Text="0.20 GPF" Value="0.20" />
<asp:ListItem Text="0.30 GPF" Value="0.30" />
</asp:DropDownList>
</asp:TableCell>
<asp:TableHeaderCell>Water Value:</asp:TableHeaderCell>
<asp:TableCell>$<asp:TextBox ID="WaterValue1" runat="server" /></asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="Water2" runat="server">
<asp:TableHeaderCell>Water #2:</asp:TableHeaderCell>
<asp:TableCell>
<asp:DropDownList ID="Water2" runat="server">
<asp:ListItem Selected="True" Text="Select" Value="0" />
<asp:ListItem Text="0.20 GPF" Value="0.20" />
<asp:ListItem Text="0.30 GPF" Value="0.30" />
</asp:DropDownList>
</asp:TableCell>
<asp:TableHeaderCell>Water Value:</asp:TableHeaderCell>
<asp:TableCell>$<asp:TextBox ID="waterValue2" runat="server" /></asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="Water3" runat="server">
<asp:TableHeaderCell>Water #3:</asp:TableHeaderCell>
<asp:TableCell>
<asp:DropDownList ID="water3" runat="server">
<asp:ListItem Selected="True" Text="Select" Value="0" />
<asp:ListItem Text="0.20 GPF" Value="0.20" />
<asp:ListItem Text="0.30 GPF" Value="0.30" />
</asp:DropDownList>
</asp:TableCell>
<asp:TableHeaderCell>Water Value:</asp:TableHeaderCell>
<asp:TableCell>$<asp:TextBox ID="waterValue3" runat="server" /></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell ColumnSpan="2">
<asp:Button ID="addWater" Enabled="true" Text="Add Water Volume" runat="server" OnClick="addWater_Click" />
</asp:TableCell>
<asp:TableCell ColumnSpan="2">
<asp:Button ID="cancelWater" Text="Cancel" runat="server" OnClick="cancelWater_Click" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
<asp:Label ID="hiddenLabel1" runat="server" Text="Hidden" CssClass="hideLbl" /><br />
</center>
<center>
<ajax:ModalPopupExtender ID="addAdditionalWater_ModalPopupExtender" runat="server" TargetControlID="hiddenLabel2" PopupControlID="divAdditional" BackgroundCssClass="popup" />
<div id="divAdditional" class="popupdiv">
<asp:Table ID="Table1" Width="100%" runat="server">
<asp:TableRow CssClass="divHeader"><asp:TableHeaderCell ColumnSpan="4">Add Additional Water</asp:TableHeaderCell></asp:TableRow>
<asp:TableRow>
<asp:TableHeaderCell>Water #1:</asp:TableHeaderCell>
<asp:TableCell>
<asp:DropDownList ID="additional1" runat="server">
<asp:ListItem Selected="True" Text="Select" Value="0" />
<asp:ListItem Text="0.20 GPF" Value="0.20" />
<asp:ListItem Text="0.30 GPF" Value="0.30" />
</asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableHeaderCell>Water #2:</asp:TableHeaderCell>
<asp:TableCell>
<asp:DropDownList ID="additional2" runat="server">
<asp:ListItem Selected="True" Text="Select" Value="0" />
<asp:ListItem Text="0.20 GPF" Value="0.20" />
<asp:ListItem Text="0.30 GPF" Value="0.30" />
</asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableHeaderCell>Water #3:</asp:TableHeaderCell>
<asp:TableCell>
<asp:DropDownList ID="additional3" runat="server">
<asp:ListItem Selected="True" Text="Select" Value="0" />
<asp:ListItem Text="0.20 GPF" Value="0.20" />
<asp:ListItem Text="0.30 GPF" Value="0.30" />
</asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
<asp:Button ID="addAdditional" Text="Add Water" runat="server" OnClick="addAdditional_Click" />
</asp:TableCell>
<asp:TableCell>
<asp:Button ID="cancelAdditional" Text="Cancel" runat="server" OnClick="cancelAdditional_Click" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
<asp:Label ID="hiddenLabel2" runat="server" Text="Hidden" CssClass="hideLbl" /><br />
</center>
Thank you as always for any assistance you can give.