繁体   English   中英

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

[英]Stop DropDownList SelectedIndexChanged Event firing on FormView command

我在editItemTemplate中有一个下拉列表。 Dropdownlist onSelectedIndexChanged事件会按我的意愿触发更改。 但是,当我提交表格时也会触发。

********更新************根据Saechel在下面的评论,我开始进一步调查。 在这里描述它可以完成。 http://blog.programmingsolution.net/net-windows-application/selectionchangecommitted-and-selectedindexchanged-events-system-nullreferenceexception-while-closing-windows-form/但是,我尝试了它甚至没有触发该事件。 我检查了http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.dropdownlist(v=vs.110).aspx ,但他们没有列出该事件。

我的目标是让用户输入以合计百分比表示的值或以实际吨数作为实际值,然后来回切换。 将插入数据库的最终值将以吨为单位。

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

我尝试通过ViewState验证Dropdown的当前索引,如果相同则跳过代码,但找不到该值。 也许可以帮助我弄清楚如何获得该价值? 到处搜寻,找不到该怎么做。 我试过:`var vs = ViewState [“ ddl_units”]。ToString(); 和我需要通过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";
    }
}

尝试使用SelectionChangeCommitted Event

这不会在表单加载时触发,而是在selectionchange时触发

暂无
暂无

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

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