Answered by:
Listview not updating base on selected dropdownlist item

Question
-
User766369706 posted
Hi, I have a listview that is unable to update despite having a method a dropdownlist selectedindexchange method. Below is my code. Please take a look to see what wrong
<asp:ListView runat="server" ID="ListView1" GroupItemCount="5" OnPagePropertiesChanging="ListView1_PagePropertiesChanging"> <LayoutTemplate> <table cellpadding="4" runat="server" id="tblProducts" style="height: 320px"> <tr runat="server" id="groupPlaceholder"> </tr> </table> <div class="pagerContent"> <asp:DataPager runat="server" ID="DataPager" PagedControlID="ListView1" PageSize="10" OnPreRender="DataPagerProducts_PreRender"> <Fields> <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="true" ShowPreviousPageButton="true" ShowNextPageButton="false" /> <asp:NumericPagerField ButtonType="Link" NumericButtonCssClass="btn" CurrentPageLabelCssClass="btn3" ButtonCount="3" /> <asp:NextPreviousPagerField ButtonType="Link" ShowLastPageButton="true" ShowNextPageButton="true" ShowPreviousPageButton="false" /> </Fields> </asp:DataPager> </div> </LayoutTemplate> <GroupTemplate> <tr runat="server" id="productRow" style="height: 80px"> <td runat="server" id="itemPlaceholder"></td> </tr> </GroupTemplate> <ItemTemplate> <td valign="top" align="center" style="width: 250px" runat="server"> <asp:ImageButton ID="ProductImage" runat="server" ImageUrl='<%# Eval("ProductImage") %>' Height="250px" NavigateUrl='<%#"~/ShoppingProduct.aspx?ProductID="+ Eval("ProductID")%>' /> <br /> <asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Eval("ProductName")%>' NavigateUrl='<%#"~/ShoppingProduct.aspx?ProductID="+ Eval("ProductID")%>' /> </td> </ItemTemplate> </asp:ListView>
Here is the method
protected void ddlCategory _SelectedIndexChanged(object sender, EventArgs e) { if (ddlCategory.SelectedIndex == 0) { ListView1.DataSource = sBLL.retrieveAllProudctAvailable(); ListView1.DataBind(); tbSearchBar.Text = "Default"; } else if (ddlCategory.SelectedIndex != 0) { tbSearchBar.Text = ddlCategory.SelectedItem.ToString(); ListView1.DataSource = sDAL.retrieveProductSelectedCategoryAvailable(ddlCategory.SelectedItem.ToString()); ListView1.DataBind(); } } public DataSet retrieveProductSelectedCategoryAvailable(String category) { SqlConnection myConnection = new SqlConnection(strConnectionString); string strCommandText = "Select * from Product where ProductCategory = @ProductCategory AND ProductStatus = 'Available'"; SqlDataAdapter myAdapter = new SqlDataAdapter(strCommandText, myConnection); myAdapter.SelectCommand.Parameters.AddWithValue("@ProductCategory", category); DataSet myDS = new DataSet(); myAdapter.Fill(myDS); return myDS; }
Monday, July 4, 2016 3:54 AM
Answers
-
User36583972 posted
Hi Alvin096,
From your description, I found your posted code is not very clear. May be you can try the following suggestions.
1: Please debug your code step by step. Check your SelectedIndexChanged event will be executed (if not you can add AutoPostBack="true" in DropDownList).
2: The following tutorial for your reference.
ListView Control in ASP.NET 4.5:
https://chsakell.com/2013/08/30/listview-control-in-asp-net-4-5/
Best Regards,
Yohann Lu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, July 5, 2016 2:43 AM
All replies
-
User1577371250 posted
Hi,
Your Page_Load should have the code for binding the ListView inside the postback.
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ListView1.DataSource = sBLL.retrieveAllProudctAvailable(); ListView1.DataBind(); } }
Monday, July 4, 2016 6:21 AM -
User766369706 posted
I did that but it still did not work
. Is there any other solution?
Monday, July 4, 2016 1:08 PM -
User36583972 posted
Hi Alvin096,
From your description, I found your posted code is not very clear. May be you can try the following suggestions.
1: Please debug your code step by step. Check your SelectedIndexChanged event will be executed (if not you can add AutoPostBack="true" in DropDownList).
2: The following tutorial for your reference.
ListView Control in ASP.NET 4.5:
https://chsakell.com/2013/08/30/listview-control-in-asp-net-4-5/
Best Regards,
Yohann Lu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, July 5, 2016 2:43 AM