Haris Ahmed
Haris Ahmed

Reputation: 7

asp DropDownList inside ListView with UpdatePanel SelectedIndexChanged Not fire

asp DropDownList inside ListView with UpdatePanel. SelectedIndexChanged Not fire when select item from dropdownlist. Already Tryied Every Solution.

OnPageLoad() !isPost AutoPostBack="True" ViewStateMode="Enabled" EnableViewState="true"

    <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# Code:

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();
        }
    }

Upvotes: 0

Views: 472

Answers (1)

asfandahmed1
asfandahmed1

Reputation: 464

The problem here is you are binding data on page load, so whenever DDL is posting page loads new values causing not to fire OnSelectdIndedChanged event. Here is my solution hope it works.

this should be replaced

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();
        }

with this

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();    
            }

Upvotes: 0

Related Questions