During my btnSave_Click event, I get the old text and value of my ddlConfirmPrice drop-down list as opposed to my desired new text and value that I selected prior to clicking Save. Am I doing something wrong? I'll describe the markup and code I'm working with.
First, this is the simple block of code inside my btnSave_Click event where I observed that the old instead of the new text and value is being picked up:
if (ddlConfirmPrice.SelectedItem != null)
{
sPrice = ddlConfirmPrice.SelectedItem.Text.Trim();
iPriceID = int.Parse(ddlConfirmPrice.SelectedItem.Value);
}
In the above, I expected sPrice and iPriceID to have come back with the new text and value I chose in the drop-down list, but instead I'm getting the previous values. All the values and text pairs in the drop-down are unique, so there's no way to mistake one for the other. I'm unfortunately getting the old text and value pair that was previously loaded for that DataGrid row prior to clicking Save. But I just selected a new item in the list! That's the problem I'm trying to solve.
Next, here's the simple DataGrid inside my *.ASCX user control file:
<div class="col-lg-12 ">
<div class="table-responsive">
<asp:datagrid id="dgShoppingListDetails" UseAccessibleHeader="True" AutoGenerateColumns="False" CssClass="table table-striped table-bordered table-hover" ShowHeader="True" ShowFooter="True" runat="server" DataKeyField="MerchandiseID" CellPadding="0" GridLines="None" AllowSorting="True" OnSortCommand="dgShoppingListDetails_SortCommand" OnItemCreated="dgShoppingListDetails_ItemCreated">
<FooterStyle CssClass="">
</FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="Merchandise ID" SortExpression="MerchandiseID" HeaderStyle-CssClass=" " ItemStyle-CssClass=" ">
<ItemTemplate>
<asp:Label ID="lblMerchandiseID" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "MerchandiseID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Product Price" SortExpression="LMSPrice" HeaderStyle-CssClass=" " ItemStyle-CssClass=" ">
<ItemTemplate>
<asp:Label ID="lblPrice" Runat="server" />
<asp:Label ID="lblPriceErrorMsg" Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Confirm Price" SortExpression="Price" HeaderStyle-CssClass=" " ItemStyle-CssClass=" ">
<ItemTemplate>
<asp:dropdownlist id="ddlConfirmPrice" Runat="server"></asp:dropdownlist>
<asp:Label ID="lblConfirmPrice" Runat="server" />
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
</div>
</div>
<asp:button id="btnSave" runat="server" Width="64px" Text="Save" onclick="btnSave_Click"></asp:button>
Finally, the *.ASCX user control file above is referenced inside my *.ASPX markup page like this:
<%@ Register tagprefix="fac1" tagname="shoppinglist" src="shoppingList.ascx" %>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<form id="frmShoppingList" method="post" runat="server">
<h1>Post Grades</h1>
<fac1:shoppinglist id="shoppingList1" runat="server"></fac1:shoppinglist>
</form>
When binding data to a DropDownList (or any other Control) you should always check for IsPostBack
. If not the data is re-added and the selected/checked state will be lost.
protected void Page_Load(object sender, EventArgs e)
{
//check for a postback
if (!Page.IsPostBack)
{
ddlConfirmPrice.DataSource = mySource;
ddlConfirmPrice.DataBind();
}
}