0

Hi I am having an issue with displaying data on a listview element inside an updatepanel. Without the update panel the information displays perfectly fine. I am using the update panel to sort the data based on the selected value of a DropDown element.

Code:

<asp:DropDownList ID="sortdropdown" AutoPostBack="true" runat="server" Enabled="true" OnSelectedIndexChanged="SelectedIndexChanged">
  <asp:ListItem>Most Recent</asp:ListItem>
  <asp:ListItem>Price: Rs Low to High</asp:ListItem>
  <asp:ListItem>Price: Rs High to Low</asp:ListItem>
</asp:DropDownList>

<asp:ScriptManager ID="MyScriptManager" runat="server" />

<asp:SqlDataSource ID="posts" runat="server" ConnectionString="<%$ ConnectionStrings:ElmTreeConnect %>">
</asp:SqlDataSource>



<asp:UpdatePanel ID="updatepanel" runat="server" UpdateMode="Conditional">
  <asp:ContentTemplate>


    <asp:ListView ID="userpostslistview" runat="server" DataKeyNames="ID" DataSourceID="posts">


      <EmptyDataTemplate>
        <span>No data was returned.</span>
      </EmptyDataTemplate>

      <ItemTemplate>



        <div class="col-md-11 pad2 lead">

          <ul class="list">
            <a>                      
                                  <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "single.aspx?postID="+Eval("ID") %>'>
                                  <li>
             <asp:Image ID="adimage" runat="server" ImageUrl='<%#"../files/" + Eval("adphoto") %>' />
             <section class="list-left">
             <h5 class="title"><asp:Label Text='<%# Eval("adtitle") %>' runat="server" ID="adtitlelabel" /></h5>
             <span class="adprice"><asp:Label Text='<%# "Price: £" + Eval("adprice", "{0:n2}") %>' runat="server" ID="adpricelabel" /></span>
             <p class="catpath"><asp:Label Text='<%# "Category: " + Eval("category") %>' runat="server" ID="adcategorylabel" /></p>
             </section>
             <section class="list-right">
             <span class="date"><asp:Label Text='<%# "Date uploaded: " + Eval("uploadeddate", "{0:d}") %>' runat="server" ID="uploadeddate" /></span>
             <span class="cityname"></span>
             </section>
             <div class="clearfix"></div>
                                 </li>
                                  </asp:HyperLink></a>
          </ul>
        </div>
      </ItemTemplate>
      <LayoutTemplate>

        <div runat="server" id="itemPlaceholderContainer" class="container">
          <span runat="server" id="itemPlaceholder" />
        </div>
        <div class="pad">
          <asp:DataPager runat="server" ID="DataPager1" PageSize="3">
            <Fields>
              <asp:NextPreviousPagerField ButtonType="Button" ButtonCssClass="btn btn-default" ShowFirstPageButton="True" ShowLastPageButton="True"></asp:NextPreviousPagerField>
            </Fields>
          </asp:DataPager>

        </div>
      </LayoutTemplate>

    </asp:ListView>
  </asp:ContentTemplate>
  <Triggers>
    <asp:AsyncPostBackTrigger ControlID="sortdropdown" EventName="SelectedIndexChanged" />
  </Triggers>
</asp:UpdatePanel>

in my code behind I am using if's to check the value of the dropdown and then changing my Select statement as necessary (the reason I have an else is that I have set it to display all categorys if the query string id is equal to 0):

    protected void Page_Load(object sender, EventArgs e)
    {

        string connectionString = WebConfigurationManager.ConnectionStrings["ElmTreeConnect"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(connectionString);

        //myConnection.ConnectionString is now set to connectionString.
        myConnection.Open();

        string urlquery = Request.QueryString["categorytype"];
        int queryid = Convert.ToInt32(urlquery);

        string query = "SELECT userpost.ID, userpost.adtitle, userpost.adphoto, userpost.uploadeddate, userpost.adprice, category.category FROM userpost INNER JOIN category ON userpost.categoryID = category.ID WHERE userpost.categoryID =" + queryid;

        string query1 = "SELECT userpost.ID, userpost.adtitle, userpost.adphoto, userpost.uploadeddate, userpost.adprice, category.category FROM userpost INNER JOIN category ON userpost.categoryID = category.ID";

        string mostrecent = " ORDER BY userpost.uploadeddate DESC";

        if (queryid != 0)
        {

                posts.SelectCommand = query += mostrecent;
                posts.DataBind();
                updatepanel.Update();

        }
        else
        {

            posts.SelectCommand = query1;
                posts.DataBind();
                updatepanel.Update();

        }

        myConnection.Close();
    }
    protected void SelectedIndexChanged(object sender, EventArgs e)
    {

        string urlquery = Request.QueryString["categorytype"];
        int queryid = Convert.ToInt32(urlquery);

        string query = "SELECT userpost.ID, userpost.adtitle, userpost.adphoto, userpost.uploadeddate, userpost.adprice, category.category FROM userpost INNER JOIN category ON userpost.categoryID = category.ID WHERE userpost.categoryID =" + queryid;

        string query1 = "SELECT userpost.ID, userpost.adtitle, userpost.adphoto, userpost.uploadeddate, userpost.adprice, category.category FROM userpost INNER JOIN category ON userpost.categoryID = category.ID";

        string mostrecent = " ORDER BY userpost.uploadeddate DESC";
        string lowtohigh = " ORDER BY userpost.adprice DESC";
        string hightolow = " ORDER BY userpost.adprice ASC";


        if (queryid != 0)
        {
            if (sortdropdown.SelectedValue == "0")
            {
                posts.SelectCommand = query += mostrecent;
                posts.DataBind();
                updatepanel.Update();
            }
            if (sortdropdown.SelectedValue == "1")
            {
                posts.SelectCommand = query += lowtohigh;
                posts.DataBind();
                updatepanel.Update();
            }
            if (sortdropdown.SelectedValue == "2")
            {
                posts.SelectCommand = query += hightolow;
                posts.DataBind();
                updatepanel.Update();
            }
        }
        else
        {
            if (sortdropdown.SelectedValue == "0")
            {
                posts.SelectCommand = query1 += mostrecent;
                posts.DataBind();
                updatepanel.Update();
            }
            if (sortdropdown.SelectedValue == "1")
            {
                posts.SelectCommand = query1 += lowtohigh;
                posts.DataBind();
                updatepanel.Update();
            }
            if (sortdropdown.SelectedValue == "2")
            {
                posts.SelectCommand = query1 += hightolow;
                posts.DataBind();
                updatepanel.Update();
            }
        }


    }

}

The select statement is being bound to the sqldatasource from the code behind.

Can anyone see where I am going wrong with this? I can't work out why the listview will not display information when contained within the update panel.

Any help is much appreciated!

  • Is your list injected into the DOM but just not showing up on the screen? – haltersweb May 09 '16 at 16:56
  • pretty new to all of this, how would I check if it's in the DOM? @haltersweb – Hip Hip Array May 09 '16 at 17:03
  • Open your website in Chrome and turn on Web Development Tools under Other Tools (other browsers have something similar) You will see your page's rendered HTML there. Drill down into your HTML in this tool and see if you can find the elements with the content you are looking for. – haltersweb May 09 '16 at 17:07
  • okay so I have checked this and it only shows me the update panel, it doesn't show the elements that would be contained within the panel. This is what shows:
    ==$0 @haltersweb
    – Hip Hip Array May 09 '16 at 17:10
  • Hmmm. Was hoping it was just a CSS issue which I could have helped you with. I am unable to help you with the ASP issue. Sorry. – haltersweb May 09 '16 at 17:15
  • that's no problem, I appreciate your attempt to help! @haltersweb – Hip Hip Array May 09 '16 at 17:19
  • Any help would be much appreciated! – Hip Hip Array May 09 '16 at 18:10
  • Can we see the markup for sortdropdown? By the way, the double scroll bars make it somewhat complicated to see the code sample... :-) – ConnorsFan May 09 '16 at 18:51
  • Updated the post there. Fixed the double scroll and also added the markup for the dropdown at the top there! :D @ConnorsFan – Hip Hip Array May 09 '16 at 18:58
  • I have also tried putting the update panel inside the list view panel, but again the data is not displaying. :S – Hip Hip Array May 09 '16 at 19:00
  • You can try putting the ScriptManager as the first control in the form, before the DropDownList (http://stackoverflow.com/questions/13473808/the-scriptmanager-must-appear-before-any-controls-that-need-it). – ConnorsFan May 09 '16 at 19:02
  • unfortunately still nothing! :( @ConnorsFan – Hip Hip Array May 09 '16 at 19:08
  • There is definitely an error with the update panel as I am not able to display anything within it. Any ideas why this might be? I'm very confused by this. @ConnorsFan – Hip Hip Array May 09 '16 at 20:43
  • The ID of the DropDownList may be mangled in the HTML output, so that the `ControlID` of the trigger does not match. You can try putting the DropDownList inside the UpdatePanel and remove the explicit trigger. Since `ChildrenAsTriggers` is true by default, the trigger should work. If it does work, and if you prefer to have the DropDownList outside of the UpdatePanel, it could be defined as a postback trigger in code-behind. – ConnorsFan May 10 '16 at 00:52

0 Answers0