I'm surprised at how difficult this question has been for me. I have a OnSelectedIndexChanged listener for a asp:DropDownList that fires correctly. I'm trying to assign the NEW selected value to a variable. However, inside my listener method, the "SelectedItem.Text" returns the DropDownList's value BEFORE the event was fired. Any insight on how to get the new value would be appreciated. Thanks.
---- EDIT ------ Below is the full code source
public partial class About : Page
{
protected void Page_Load(object sender, EventArgs e)
{
BuildTable();
updateFilterPanel();
}
private void BuildTable()
{
ArrayList players = FilterHandler.getInstance().getFilteredPlayerList();
List<Filter> filters = FilterHandler.getInstance().allFilters;
StringBuilder html = new StringBuilder();
html.Append("<table id=\"tbl_listThisSeason\" class=\"table table-hover\">");
html.Append("<thead class=\"thead-dark\"><tr><th scope=\"col\" > Name </th><th scope = \"col\" > Position </th><th scope=\"col\">Team</th>");
foreach (Filter f in filters)
{
if(!f.isPrimary())
html.Append("<th scope=\"col\" >"+f.columnTitle+"</th>");
}
html.Append("</tr></thead><tbody>");
foreach (JObject p in players)
{
html.Append("<tr>");
html.Append("<td>" + (string)p["firstName"] + " " + (string)p["lastName"] + "</td>");
html.Append("<td>" + (string)p["position"] + "</td>");
html.Append("<td>" + CloudDataReceiver.getTeamName((string)p["teamId"]) + "</td>");
foreach (Filter f in filters)
html.Append("<td>" + (string)p[f.field] + "</td>");
html.Append("</tr>");
}
html.Append("</tbody></table>");
if (DataTable.Controls.Count > 0)
DataTable.Controls.RemoveAt(0);
DataTable.Controls.Add(new Literal { Text = html.ToString() });
}
private void updateFilterPanel()
{
List<Filter> filters = FilterHandler.getInstance().allFilters;
int count = 0;
foreach (Filter f in filters) {
TableRow row = (TableRow)Table_Filters.FindControl("row_" + count.ToString());
row.Visible = true;
DropDownList d = (DropDownList) row.FindControl("combo_" + count.ToString() + "_a");
String[] dItems = FilterHandler.getInstance().getAllTitles();
d.Items.Clear();
foreach(String s in dItems)
d.Items.Add(s);
d.SelectedValue = f.field;
DropDownList d2 = (DropDownList)row.FindControl("combo_" + count.ToString() + "_b");
String[] dItems2 = ConvertHandler.getInstance().getModifierChoices(f.field);
d2.Items.Clear();
foreach (String s in dItems2)
d2.Items.Add(s);
d2.SelectedIndex = 0;
count++;
}
for (int i = count; i < 10; i++) {
TableRow row = (TableRow)Table_Filters.FindControl("row_" + i.ToString());
row.Visible = false;
}
}
protected void btn_add_Click(object sender, EventArgs e)
{
if (FilterHandler.getInstance().allFilters.Count >= 10)
return;
Filter f = new Filter("playerBestOvr", Filter.TYPE_INT);
f.valueFloat = 93;
FilterHandler.getInstance().addFilter(f);
updateFilterPanel();
}
protected void btn_get_Click(object sender, EventArgs e)
{
BuildTable();
}
protected void Remove_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
FilterHandler.getInstance().allFilters.RemoveAt(Int32.Parse(btn.CommandArgument));
updateFilterPanel();
}
protected void FieldCombo_Change(object sender, EventArgs e)
{
DropDownList d = (DropDownList)sender;
int id = Int32.Parse(d.ID.Split('_')[1]);
FilterHandler.getInstance().allFilters[id].field = d.SelectedItem.Text; // the selected value is before change
}
}
And the HTML
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h2>Player Database</h2>
<div class="row">
<div class="col-12 col-md-8">
<p>
<asp:PlaceHolder ID = "DataTable" runat="server" />
</p>
</div>
<div class="col-6 col-md-4">
<div class="col-sm-6"><asp:Button ID="btn_add" runat="server" Text="Add Filter" OnClick="btn_add_Click" class="btn-block btn-primary"/></div>
<div class="col-sm-6"><asp:Button ID="btn_get" runat="server" Text="Get" OnClick="btn_get_Click" class="btn-block btn-info"/></div>
<br>
<asp:Table id="Table_Filters" runat="server" class="table table-sm table-dark">
<asp:TableRow ID="row_0" runat="server" visible="false">
<asp:TableCell><asp:DropDownList runat="server" id="combo_0_a" class="mdb-select lg-form" OnTextChanged="FieldCombo_Change" AutoPostBack="true"/></asp:TableCell>
<asp:TableCell><input id="text_0" type="number" class="textfield" min="0" max="99"/></asp:TableCell>
<asp:TableCell><asp:DropDownList runat="server" id="combo_0_b" class="mdb-select lg-form"></asp:DropDownList></asp:TableCell>
<asp:TableCell><asp:Button runat="server" CommandArgument="0" OnClick="Remove_Click" Text="Remove"/></asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="row_1" runat="server" visible="false">
<asp:TableCell><asp:DropDownList runat="server" id="combo_1_a" class="mdb-select lg-form" OnSelectedIndexChanged="FieldCombo_Change" AutoPostBack="true"/></asp:TableCell>
<asp:TableCell><input id="text_1" type="number" class="textfield" min="0" max="99"/></asp:TableCell>
<asp:TableCell><asp:DropDownList runat="server" id="combo_1_b" class="mdb-select lg-form"></asp:DropDownList></asp:TableCell>
<asp:TableCell><asp:Button runat="server" CommandArgument="1" OnClick="Remove_Click" Text="Remove"/></asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="row_2" runat="server" visible="false">
<asp:TableCell><asp:DropDownList runat="server" id="combo_2_a" class="mdb-select lg-form" OnSelectedIndexChanged="FieldCombo_Change" AutoPostBack="true"/></asp:TableCell>
<asp:TableCell><input id="text_2" type="number" class="textfield" min="0" max="99"/></asp:TableCell>
<asp:TableCell><asp:DropDownList runat="server" id="combo_2_b" class="mdb-select lg-form"></asp:DropDownList></asp:TableCell>
<asp:TableCell><asp:Button runat="server" CommandArgument="2" OnClick="Remove_Click" Text="Remove"/></asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="row_3" runat="server" visible="false">
<asp:TableCell><asp:DropDownList runat="server" id="combo_3_a" class="mdb-select lg-form" OnSelectedIndexChanged="FieldCombo_Change" AutoPostBack="true"/></asp:TableCell>
<asp:TableCell><input id="text_3" type="number" class="textfield" min="0" max="99"/></asp:TableCell>
<asp:TableCell><asp:DropDownList runat="server" id="combo_3_b" class="mdb-select lg-form"></asp:DropDownList></asp:TableCell>
<asp:TableCell><asp:Button runat="server" CommandArgument="3" OnClick="Remove_Click" Text="Remove"/></asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="row_4" runat="server" visible="false">
<asp:TableCell><asp:DropDownList runat="server" id="combo_4_a" class="mdb-select lg-form" OnSelectedIndexChanged="FieldCombo_Change" AutoPostBack="true"/></asp:TableCell>
<asp:TableCell><input id="text_4" type="number" class="textfield" min="0" max="99"/></asp:TableCell>
<asp:TableCell><asp:DropDownList runat="server" id="combo_4_b" class="mdb-select lg-form"></asp:DropDownList></asp:TableCell>
<asp:TableCell><asp:Button runat="server" CommandArgument="4" OnClick="Remove_Click" Text="Remove"/></asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="row_5" runat="server" visible="false">
<asp:TableCell><asp:DropDownList runat="server" id="combo_5_a" class="mdb-select lg-form" OnSelectedIndexChanged="FieldCombo_Change" AutoPostBack="true"/></asp:TableCell>
<asp:TableCell><input id="text_5" type="number" class="textfield" min="0" max="99"/></asp:TableCell>
<asp:TableCell><asp:DropDownList runat="server" id="combo_5_b" class="mdb-select lg-form"></asp:DropDownList></asp:TableCell>
<asp:TableCell><asp:Button runat="server" CommandArgument="5" OnClick="Remove_Click" Text="Remove"/></asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="row_6" runat="server" visible="false">
<asp:TableCell><asp:DropDownList runat="server" id="combo_6_a" class="mdb-select lg-form" OnSelectedIndexChanged="FieldCombo_Change" AutoPostBack="true"/></asp:TableCell>
<asp:TableCell><input id="text_6" type="number" class="textfield" min="0" max="99"/></asp:TableCell>
<asp:TableCell><asp:DropDownList runat="server" id="combo_6_b" class="mdb-select lg-form"></asp:DropDownList></asp:TableCell>
<asp:TableCell><asp:Button runat="server" CommandArgument="6" OnClick="Remove_Click" Text="Remove"/></asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="row_7" runat="server" visible="false">
<asp:TableCell><asp:DropDownList runat="server" id="combo_7_a" class="mdb-select lg-form" OnSelectedIndexChanged="FieldCombo_Change" AutoPostBack="true"/></asp:TableCell>
<asp:TableCell><input id="text_7" type="number" class="textfield" min="0" max="99"/></asp:TableCell>
<asp:TableCell><asp:DropDownList runat="server" id="combo_7_b" class="mdb-select lg-form"></asp:DropDownList></asp:TableCell>
<asp:TableCell><asp:Button runat="server" CommandArgument="7" OnClick="Remove_Click" Text="Remove"/></asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="row_8" runat="server" visible="false">
<asp:TableCell><asp:DropDownList runat="server" id="combo_8_a" class="mdb-select lg-form" OnSelectedIndexChanged="FieldCombo_Change" AutoPostBack="true"/></asp:TableCell>
<asp:TableCell><input id="text_8" type="number" class="textfield" min="0" max="99"/></asp:TableCell>
<asp:TableCell><asp:DropDownList runat="server" id="combo_8_b" class="mdb-select lg-form"></asp:DropDownList></asp:TableCell>
<asp:TableCell><asp:Button runat="server" CommandArgument="8" OnClick="Remove_Click" Text="Remove"/></asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="row_9" runat="server" visible="false">
<asp:TableCell><asp:DropDownList runat="server" id="combo_9_a" class="mdb-select lg-form" OnSelectedIndexChanged="FieldCombo_Change" AutoPostBack="true"/></asp:TableCell>
<asp:TableCell><input id="text_9" type="number" class="textfield" min="0" max="99"/></asp:TableCell>
<asp:TableCell><asp:DropDownList runat="server" id="combo_9_b" class="mdb-select lg-form"></asp:DropDownList></asp:TableCell>
<asp:TableCell><asp:Button runat="server" CommandArgument="9" OnClick="Remove_Click" Text="Remove"/></asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
</div>
</asp:Content>