停止DropDownList的SelectedIndexChanged事件射击FormView的命令 [英] Stop DropDownList SelectedIndexChanged Event firing on FormView command

查看:186
本文介绍了停止DropDownList的SelectedIndexChanged事件射击FormView的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EditItemTemplate模板内的下拉列表。上更改DROPDOWNLIST onSelectedIndexChanged事件触发像我想要的。然而,当我提交表单也将触发。

********更新************
每低于Saechel评论,我开始进一步调查。这说明是可以做到的。 <一href=\"http://blog.programmingsolution.net/net-windows-application/selectionchangecommitted-and-selectedindexchanged-events-system-nullreferenceexception-while-closing-windows-form/\" rel=\"nofollow\">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他们不列出该事件。

我的目标是让用户输入的值作为总的百分比或实际为实际吨,然后来回切换。将被插入到DB中的端值将是在吨。

我想要么:
一)不火的onSelectedIndexChanged事件
二)一些如何确定,如果我在同一个指标,如果语句跳过一切有
三)要对此,我不知道一个更好的办法。

我试图通过验证的ViewState的下拉当前指数和跳过code。如果这是同样的,但无法找到该值。也许帮我找出如何获取的价值?四处搜寻,但没有找到如何做到这一点。
。我想:'无功VS =的ViewState [ddl_units]的ToString();还有一些其它的变化,因为我需要找到通过的FindControl控制

的.aspx:

 &LT; ASP:面板ID =pnlRecycledMaterialsReceivedForm=服务器可见=假&GT;
    &LT; ASP:FormView控件ID =fvAddRecycledMaterialsReceived=服务器SkinID =annualReportFormview的EnableViewState =假
        的HeaderText =选择回收材料收到详细信息的DataKeyNames =RecycleDetailIdDefaultMode =插入
        的DataSourceID =odsRecycledMaterialsReceivedDetailOnDataBound =fvAddRecycledMaterialsReceived_DataBound
        OnItemCommand =fvAddRecycledMaterialsReceived_ItemCommandOnItemInserted =fvAddRecycledMaterialsReceived_ItemInserted
        OnItemUpdated =fvAddRecycledMaterialsReceived_ItemUpdatedOnItemDeleted =fvAddRecycledMaterialsReceived_ItemDeleted&GT;
        &LT;&EditItemTemplate的GT;
            &LT; ASP:文本框ID =tbxRecycledTotalWasteQuantity=服务器文本='&LT;%#绑定(TotalWasteQuantity)%&GT;' WIDTH =64PX&GT;&LT; / ASP:文本框&GT;
            &LT; ASP:文本框ID =tbxRecycledWasteCommercialQuantity=服务器文本='&LT;%#绑定(CommercialQuantity)%&GT;' WIDTH =64PX&GT;&LT; / ASP:文本框&GT;
            &LT; ASP:文本框ID =tbxRecycledWasteResidentialQuantity=服务器文本='&LT;%#绑定(ResidentialQuantity)%&GT;' WIDTH =64PX&GT;&LT; / ASP:文本框&GT;
            &LT; ASP:DropDownList的ID =ddl_Units=服务器的AutoPostBack =真OnSelectedIndexChanged =ddl_Units_SelectedIndexChanged&GT;
                &LT; ASP:ListItem的值=文本=启用=FALSE/&GT;
                &LT; ASP:ListItem的文本=吨VALUE =1/&GT;
                &LT; ASP:ListItem的文本=百分比VALUE =9/&GT;
            &LT; / ASP:DropDownList的&GT;
            &LT; ASP:LinkBut​​ton的ID =lbtnWasteReceivedUpdate=服务器文本=更新的CommandName =更新
                的ValidationGroup =RecycledWasteReceivedDetail字体粗体=真/&GT;&安培; NBSP;
            &LT; ASP:LinkBut​​ton的ID =lbtnWasteReceivedInsertCancel=服务器文本=取消的CausesValidation =FALSE的CommandName =取消/&GT;
        &LT; / EditItemTemplate中&GT;
    &LT; / ASP:FormView控件&GT;
&LT; / ASP:面板&gt;

的.cs:

 保护无效ddl_Units_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
    文本框tbxRecycledTotalWasteQuantity =(文本框)fvAddRecycledMaterialsReceived.FindControl(tbxRecycledTotalWasteQuantity);
    文本框tbxRecycledWasteResidentialQuantity =(文本框)fvAddRecycledMaterialsReceived.FindControl(tbxRecycledWasteResidentialQuantity);
    VAR d_TotalWasteQuantity = Convert.ToDecimal(tbxRecycledTotalWasteQuantity.Text);
    ResidentialQuantity = Convert.ToDecimal(tbxRecycledWasteResidentialQuantity.Text);    DropDownList的ddl_units =(DropDownList的)fvAddRecycledMaterialsReceived.FindControl(ddl_units);    如果(ddl_units.SelectedIndex.ToString()==2)
    {
        // 2 =%的
        //以TB值,并转换为百分比
        //七百分之三百* 100
        tbxRecycledWasteResidentialQuantity.Text =((ResidentialQuantity / d_TotalWasteQuantity)* 100)的ToString();
        // ResidentialQuantity = ResidentialQuantity *(d_TotalWasteQuantity / 100);
    }
    其他
    {
        //否则'吨'被选中。转换值(%),以吨。
        // 700 *(43/100)= 301
        ResidentialQuantity = d_TotalWasteQuantity *(ResidentialQuantity / 100);
        tbxRecycledWasteResidentialQuantity.Text = ResidentialQuantity.ToString();
        //tbxRecycledWasteResidentialQuantity.Text =吨;
    }
}


解决方案

尝试使用 SelectionChangeCommitted事件

这不会触发窗体加载但触发selectionchange

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";
    }
}

解决方案

Try using the SelectionChangeCommitted Event.

This will not trigger on form load but triggers on selectionchange

这篇关于停止DropDownList的SelectedIndexChanged事件射击FormView的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆