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!