简体   繁体   English

停止DropDownList SelectedIndexChanged事件在FormView命令上触发

[英]Stop DropDownList SelectedIndexChanged Event firing on FormView command

I have a dropdown list inside an editItemTemplate. 我在editItemTemplate中有一个下拉列表。 The Dropdownlist onSelectedIndexChanged event fires on change like I want. Dropdownlist onSelectedIndexChanged事件会按我的意愿触发更改。 However it also will fire when I submit the form. 但是,当我提交表格时也会触发。

********UPDATE ************ Per Saechel comment below, I started to investigate further. ********更新************根据Saechel在下面的评论,我开始进一步调查。 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. http://blog.programmingsolution.net/net-windows-application/selectionchangecommitted-and-selectedindexchanged-events-system-nullreferenceexception-while-closing-windows-form/但是,我尝试了它甚至没有触发该事件。 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. 我检查了http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.dropdownlist(v=vs.110).aspx ,但他们没有列出该事件。

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. 我想:a)不触发“ onSelectedIndexChanged”事件b)一些如何确定我是否在同一个索引上并使用if语句跳过所有内容的方法c)一种我不知道的更好的解决方法。

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. 我尝试通过ViewState验证Dropdown的当前索引,如果相同则跳过代码,但找不到该值。 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(); 我试过:`var vs = ViewState [“ ddl_units”]。ToString(); and some other variations as I needed to find the control via FindControl 和我需要通过FindControl找到控件的其他一些变体

.aspx: .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: .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";
    }
}

Try using the SelectionChangeCommitted Event . 尝试使用SelectionChangeCommitted Event

This will not trigger on form load but triggers on selectionchange 这不会在表单加载时触发,而是在selectionchange时触发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM