Stop DropDownList SelectedIndexChanged Event firing on FormView command

為{幸葍}努か 提交于 2019-12-25 03:25:00

问题


I have a dropdown list inside an editItemTemplate. The Dropdownlist onSelectedIndexChanged event fires on change like I want. However it also will fire when I submit the form.

********UPDATE ************ Per Saechel comment below, I started to investigate further. Here it describes it can be done. http://blog.programmingsolution.net/net-windows-application/selectionchangecommitted-and-selectedindexchanged-events-system-nullreferenceexception-while-closing-windows-form/ BUT, I tried it and it didn't even fire the event. I checked http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist(v=vs.110).aspx and they don't list the event.

My goal is to let the user enter the values as a % of the Total or actual as actual tons and then toggle back and forth. The end value that will be inserted into the DB will be in tons.

I want to either: a) not fire the "onSelectedIndexChanged" event b) Some how determine if I am on the same index and have an if statement to skip everything c) A better way of going about this that I don't know of.

I tried verifying via the ViewState the Dropdown's current index and skip the code if it was the same but couldn't find the value. Maybe help me figure out how to get that value? Searched around and could not find how do do it. I tried: `var vs = ViewState["ddl_units"].ToString(); and some other variations as I needed to find the control via FindControl

.aspx:

<asp:Panel ID="pnlRecycledMaterialsReceivedForm" runat="server" Visible="false">
    <asp:FormView ID="fvAddRecycledMaterialsReceived" runat="server" SkinID="annualReportFormview" EnableViewState="false"
        HeaderText="Selected Recycled Materials Received Detail" DataKeyNames="RecycleDetailId" DefaultMode="Insert"
        DataSourceID="odsRecycledMaterialsReceivedDetail" OnDataBound="fvAddRecycledMaterialsReceived_DataBound" 
        OnItemCommand="fvAddRecycledMaterialsReceived_ItemCommand" OnItemInserted="fvAddRecycledMaterialsReceived_ItemInserted"
        OnItemUpdated="fvAddRecycledMaterialsReceived_ItemUpdated" OnItemDeleted="fvAddRecycledMaterialsReceived_ItemDeleted">
        <EditItemTemplate>
            <asp:TextBox ID="tbxRecycledTotalWasteQuantity" runat="server" Text='<%# Bind("TotalWasteQuantity") %>' Width="64px"></asp:TextBox>
            <asp:TextBox ID="tbxRecycledWasteCommercialQuantity" runat="server" Text='<%# Bind("CommercialQuantity") %>' Width="64px"></asp:TextBox>
            <asp:TextBox ID="tbxRecycledWasteResidentialQuantity" runat="server" Text='<%# Bind("ResidentialQuantity") %>' Width="64px"></asp:TextBox>
            <asp:DropDownList ID="ddl_Units" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_Units_SelectedIndexChanged">
                <asp:ListItem Value="" Text="" Enabled="false" />
                <asp:ListItem Text="Tons" Value="1" />
                <asp:ListItem Text="Percent" Value="9" />
            </asp:DropDownList>
            <asp:LinkButton ID="lbtnWasteReceivedUpdate" runat="server" Text="Update" CommandName="Update"
                ValidationGroup="RecycledWasteReceivedDetail" Font-Bold="True" />&nbsp;
            <asp:LinkButton ID="lbtnWasteReceivedInsertCancel" runat="server" Text="Cancel" CausesValidation="False" CommandName="Cancel" />
        </EditItemTemplate>
    </asp:FormView>
</asp:Panel>

.cs:

protected void ddl_Units_SelectedIndexChanged(object sender, EventArgs e)
{
    TextBox tbxRecycledTotalWasteQuantity = (TextBox)fvAddRecycledMaterialsReceived.FindControl("tbxRecycledTotalWasteQuantity");
    TextBox tbxRecycledWasteResidentialQuantity = (TextBox)fvAddRecycledMaterialsReceived.FindControl("tbxRecycledWasteResidentialQuantity");
    var d_TotalWasteQuantity = Convert.ToDecimal(tbxRecycledTotalWasteQuantity.Text);
    ResidentialQuantity = Convert.ToDecimal(tbxRecycledWasteResidentialQuantity.Text);

    DropDownList ddl_units = (DropDownList)fvAddRecycledMaterialsReceived.FindControl("ddl_units");

    if (ddl_units.SelectedIndex.ToString() == "2")
    {
        //2 = percent
        //Take tb value and convert to percent
        //300/700 * 100
        tbxRecycledWasteResidentialQuantity.Text = ((ResidentialQuantity / d_TotalWasteQuantity) * 100).ToString();
        //ResidentialQuantity = ResidentialQuantity * (d_TotalWasteQuantity / 100);
    }
    else
    {
        //Else 'tons' was chosen. Convert value(%) to tons. 
        //700 * (43/100) = 301
        ResidentialQuantity = d_TotalWasteQuantity * (ResidentialQuantity / 100);
        tbxRecycledWasteResidentialQuantity.Text = ResidentialQuantity.ToString();
        //tbxRecycledWasteResidentialQuantity.Text = "Tons";
    }
}

回答1:


Try using the SelectionChangeCommitted Event.

This will not trigger on form load but triggers on selectionchange



来源:https://stackoverflow.com/questions/25394452/stop-dropdownlist-selectedindexchanged-event-firing-on-formview-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!