使用UpdatePanel的ListView内的asp DropDownList SelectedIndexChanged不会触发

时间:2016-02-24 14:15:39

标签: asp.net drop-down-menu webforms

使用UpdatePanel在ListView中使用asp DropDownList。 SelectedIndexChanged从下拉列表中选择项目时不激活。 已经尝试过每一种解决方案。

  

的onPageLoad()   !isPost   的AutoPostBack ="真"   ViewStateMode ="启用"   的EnableViewState ="真"

    <asp:ListView ID="productListView" runat="server" OnItemDataBound="productListView_ItemDataBound">
    <ItemTemplate>
        <div class="item col-lg-3 col-md-4 col-sm-6 col-xs-12">
            <div class="hovereffect">
               <div class="overlay">

                     <div class="selection">
                                <asp:Label runat="server" CssClass="lblShowTag">BRAND:</asp:Label>
                                <asp:DropDownList ID="ddlBrand" runat="server" CssClass="branddrop" AutoPostBack="True" OnSelectedIndexChanged="ddlBrand_SelectedIndexChanged" ViewStateMode="Enabled" EnableViewState="true">
                                        </asp:DropDownList>
                                <asp:UpdatePanel ID="updatePanel" runat="server" >
                                    <ContentTemplate>
                                        <asp:Label runat="server" ID="lblShowTag" text="PRICE: " CssClass="lblShowTag"/><asp:Label runat="server" ID="lblEachPrice" CssClass="lblEachPrice"/>
                                        <div class="clearfix"></div>
                                        <asp:Label runat="server" class="lblShowTag">QUANTITY:&nbsp;&nbsp;</asp:Label>
                                        <asp:TextBox ID="txtQuantity" runat="server" Text="1" CssClass='txtquantity' />
                                        <div class="clearfix"></div>
                                        <asp:Label runat="server" ID="Label1" text="TOTAL: " CssClass="lblShowTag"/><asp:Label runat="server" ID="lblTotalPrice" CssClass="lblEachPrice" ForeColor="Green" Text="5000"/>
                                        <div class="clearfix"></div>
                                        <asp:Label id="lblError" runat="server" Text="Label 2" ForeColor="Red"/>
                                    </ContentTemplate>
                                    <Triggers>
                                        <asp:AsyncPostBackTrigger ControlID="ddlBrand" EventName="SelectedIndexChanged" />
                                    </Triggers>
                                </asp:UpdatePanel>
                            </div>


                </div>
            </div>
        </div>

    </ItemTemplate>
    <EmptyDataTemplate>
        <h3>Sorry...No Product Availabel</h3>
    </EmptyDataTemplate>
</asp:ListView>

C#代码:

protected void Page_Load(object sender, EventArgs e)
    {
        //showProdutListView();
        String CateId = Request.QueryString["CateId"];
        if (!String.IsNullOrEmpty(CateId))
            {
                showProdutListView(CateId);
            }
            else
            {
                Response.Redirect("/default.aspx");
            }
         if (!IsPostBack)
        {

            if (Session["myCart"] != null)
            {
                DataTable dt = (DataTable)Session["myCart"];
                cartQuantity = dt.Rows.Count.ToString();
            }
            else
            {
                cartQuantity = "0";
            }
            ((ProdutcPageNested)Master).OnProductMasterPage.Text = "Cart Items: " + cartQuantity;
            ShowBrand();
        }

    }public void ddlBrand_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (ListViewItem item in productListView.Items)
        {
            DropDownList ddl = (DropDownList)sender;

            lblEachPrice.Text = ddl.SelectedValue.ToString();
        }
    }

1 个答案:

答案 0 :(得分:0)

这里的问题是你是关于页面加载的绑定数据,所以每当DDL发布页面加载新值时都会导致不发送OnSelectdIndedChanged事件。这是我的解决方案希望它有效。

应该替换

if (!String.IsNullOrEmpty(CateId))
            {
                showProdutListView(CateId);
            }
            else
            {
                Response.Redirect("/default.aspx");
            }
         if (!IsPostBack)
        {

            if (Session["myCart"] != null)
            {
                DataTable dt = (DataTable)Session["myCart"];
                cartQuantity = dt.Rows.Count.ToString();
            }
            else
            {
                cartQuantity = "0";
            }
            ((ProdutcPageNested)Master).OnProductMasterPage.Text = "Cart Items: " + cartQuantity;
            ShowBrand();
        }

使用此

if (!IsPostBack)
            {
                if (!String.IsNullOrEmpty(CateId))
                {
                        showProdutListView(CateId);
                }
                else
                {
                    Response.Redirect("/default.aspx");
                }
                if (Session["myCart"] != null)
                {
                    DataTable dt = (DataTable)Session["myCart"];
                    cartQuantity = dt.Rows.Count.ToString();
                }
                else
                {
                    cartQuantity = "0";
                }
                ((ProdutcPageNested)Master).OnProductMasterPage.Text = "Cart Items: " + cartQuantity;
                ShowBrand();    
            }