c# - ListView DataBind Sorting Values -
c# - ListView DataBind Sorting Values -
i want have dropdownmenu
can take how want sort listview
. current code :
<asp:dropdownlist id="dropdownselect" runat="server" autopostback="true" onselectedindexchanged="getproducts"> <asp:listitem selected="true" value="desdate"> descending date </asp:listitem> <asp:listitem value="asdate"> ascending date </asp:listitem> <asp:listitem value="asalp"> ascending alphabetical </asp:listitem> <asp:listitem value="desalp"> decentind alphabetical </asp:listitem> </asp:dropdownlist>
and have listview
display data:
<asp:listview id="productlist" runat="server" datakeynames="newsid" groupitemcount="1" itemtype="sitestiri.models.news" selectmethod="getproducts"> <emptydatatemplate> <table> <tr> <td>no info returned.</td> </tr> </table> </emptydatatemplate> <emptyitemtemplate> <td/> </emptyitemtemplate> <grouptemplate> <tr id="itemplaceholdercontainer" runat="server"> <td id="itemplaceholder" runat="server"></td> </tr> </grouptemplate> <itemtemplate> <td runat="server"> <table> <tr> <td> <a href="newsdetails.aspx?newsid=<%#:item.newsid%>"> <img src="/catalog/images/thumbs/<%#:item.imagepath%>" width="100" height="75" style="border: solid" /></a> </td> </tr> <tr> <td> <a href="newsdetails.aspx?newsid=<%#:item.newsid%>"> <p style="color: black;"> <%#:item.newstitle%> </p> </a> </td> </tr> <tr> <td> </td> </tr> </table> </p> </td> </itemtemplate> <layouttemplate> <table style="width:100%;"> <tbody> <tr> <td> <table id="groupplaceholdercontainer" runat="server" style="width:100%"> <tr id="groupplaceholder"></tr> </table> </td> </tr> <tr> <td></td> </tr> <tr></tr> </tbody> </table> </layouttemplate> </asp:listview>
the thing have no thought how is: after selecting sorting rule dropdown menu, can't figure out how write(or write) method update listview should. attemp :
public iqueryable<news> getproducts() { var _db = new sitestiri.models.newscontext(); iqueryable<news> query = _db.news; if (("desdate").equals(dropdownselect.selecteditem.value)) { query.orderbydescending(u => u.releasedate); } if (("asdate").equals(dropdownselect.selecteditem.value)) { query.orderby(u => u.releasedate); } if (("asalp").equals(dropdownselect.selecteditem.value)) { query.orderby(u => u.newstitle); } if (("desapl").equals(dropdownselect.selecteditem.value)) { query.orderbydescending(u => u.newstitle); } homecoming query; }
which gives me bunch of errors , doesn't work .... little bit of help please? new (2 days).
let's prepare code step step.
event handlers need have signature. in case of every asp.net command can remember of, need receive 2 parameters, event arguments , event source object, , homecoming void. note calling getproduct
not going update listview
, need trigger databinding command itself. we'll later. let's introduce proper handler:
public void dropdownselect_selectedindexchanged(object sender, eventargs e)
don't forget update markup well:
onselectedindexchanged="dropdownselect_selectedindexchanged"
the conditions on how show info in listview
have changed. means need rebind calling databind
, should phone call getproducts
(as 1 specified in selectmethod
):
public void dropdownselect_selectedindexchanged(object sender, eventargs e) { productlist.databind(); }
finally in getproducts
note linq calls not update current object, rather produce new 1 every time. should have this:
if ("desdate".equals(dropdownselect.selecteditem.value)) { query = query.orderbydescending(u => u.releasedate); }
c# asp.net listview
Comments
Post a Comment