I have a list of 10 articles which gets displayed with the following Linq to SQL query in the page load event:
if (!Page.IsPostBack)
{
MyDataClassesDataContext db = new MyDataClassesDataContext();
var pressRelease = from pr in db.PressReleases
orderby pr.DatePublished ascending
where pr.Published == true
select pr;
int i = 0;
foreach (var release in pressRelease)
{
Literal literal = new Literal();
literal.Text = "<div class='row no-gutters pb-5'><div class='col-12'><h2>" +
release.Title + "</h2>" + Server.HtmlDecode(release.Content) + "</h2>" +
"<a href='Press-Release.aspx?id=" + release.Id +
"'><b>Read article...</b></a></div></div>";
pnlPressReleaases.Controls.Add(literal);
}
}
On my web page, I have a dropdownlist with the following values and the autopostback set to true.
<asp:DropDownList ID="ddlPressSortBy" AutoPostBack="True" runat="server" OnSelectedIndexChanged="ddlPressSortBy_SelectedIndexChanged">
<asp:ListItem Value="pr.DatePublished descending">Date Published Desc</asp:ListItem>
<asp:ListItem Selected="True" Value="pr.DatePublished ascending">Date Published Asc</asp:ListItem>
<asp:ListItem Value="pr.Title ascending">Alphabetical</asp:ListItem>
</asp:DropDownList>
Once I change the value of the dropdownlist, the data returned has no orderby on the corresponding select statement. Here is my code for the OnSelectIndexChanged event:
protected void ddlPressSortBy_SelectedIndexChanged(object sender, EventArgs e)
{
MyDataClassesDataContext db = new MyDataClassesDataContext();
var pressRelease = from pr in db.PressReleases
orderby ddlPressSortBy.SelectedValue
where pr.Published == true
select pr;
foreach (var release in pressRelease)
{
Literal literal = new Literal();
literal.Text = "<div class='row no-gutters pb-5'><div class='col-12'><h2>" +
release.Title + "</h2>" + Server.HtmlDecode(release.Content) + "</h2>" + "<a href='Press-Release.aspx?id=" + release.Id + "'><b>Read article...</b></a></div></div>";
pnlPressReleaases.Controls.Add(literal);
}
}
I'm a little unsure why it is not firing with the orderby.
Any help would be greatly appreciated.
Thanks Rob