Stop DropDownList SelectedIndexChanged在FormView命令上触发事件

时间:2014-08-19 23:15:42

标签: c# asp.net .net drop-down-menu viewstate

我在editItemTemplate中有一个下拉列表。 DropSelist onSelectedIndexChanged事件会像我想要的那样触发更改。但是,当我提交表格时它也会触发。

********更新************ 根据Saechel的评论,我开始进一步调查。在这里它描述了它可以做到。 http://blog.programmingsolution.net/net-windows-application/selectionchangecommitted-and-selectedindexchanged-events-system-nullreferenceexception-while-closing-windows-form/ 但是,我尝试了它,它甚至没有发射事件。 我检查了http://msdn.microsoft.com/en-us/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";
    }
}

1 个答案:

答案 0 :(得分:0)

尝试使用SelectionChangeCommitted Event

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

上触发