c# - asp:ListView create an Case condition -
hello find below code.
i trying create webpart sharepoint can bind multiple type of items in slider.
i have 2 types image or video
and each type has different tag , formatting. wont able bind value tags. question how can create "swich case" inside listview.
if type = image --> bind tag else bind second tag.
<asp:listview runat="server" id="lva"> <layouttemplate> <div class="slider"> <ul class="bxslider"> <asp:placeholder runat="server" id="itemplaceholder" /> </ul> </layouttemplate> <itemtemplate> <li> <!-- "tag a" --> <video width="320" height="260" controls> <source src="xyz" type="video/mp4"> browser not support video tag. </video> </li> <li> <!-- "tag b" --> <img src="abc" /> </li> </itemtemplate> </asp:listview>
you can following:
- have
<asp:placeholder>
video , image type. - put condition in
visible
attribute each<asp:placeholder>
. change condition suitable project.
code here:
<itemtemplate> <asp:placeholder runat="server" visible='<%# eval("type") == "video" %>'> <li> <video width="320" height="260" controls> <source src="xyz" type="video/mp4"> browser not support video tag. </video> </li> </asp:placeholder> <asp:placeholder runat="server" visible='<%# eval("type") == "image" %>'> <li> <img src="abc" /> </li> </asp:placeholder> </itemtemplate>
update: correctly pointed out possible errors might occur during runtime. if not able avoid these errors adding additional checks on data, there second alternative, has limitations.
the solution render each item in code behind. each scenario create function returns html:
protected string rendervideo(system.data.datarow dr) { // values current datarow string path = dr["path"].tostring(); // create appropriate markup video return "<video>" + "</video>"; } protected string renderphoto(system.data.datarow dr) { // values current datarow string path = dr["path"].tostring(); // create appropriate markup video return "<img src='' alt='' />"; }
each of functions has datarow
parameter. need call each function when needed current datarow
. current datarow
can accessed ((system.data.datarowview)getdataitem()).row
.
in following example use inline if call correct function.
<itemtemplate> <%#(eval("type") == "video" ? rendervideo(((system.data.datarowview)getdataitem()).row) : renderphoto(((system.data.datarowview)getdataitem()).row)) %> </itemtemplate>
alternative: if have more 2 scenarios above use of inline if inefficient. can following practically same more practical maintain.
<itemtemplate> <%#(eval("type") == "video" ? rendervideo(((system.data.datarowview)getdataitem()).row) : "") %> <%#(eval("type") == "photo" ? renderphoto(((system.data.datarowview)getdataitem()).row) : "") %> </itemtemplate>
the limitation on approach cannot have asp controls on each item.
Comments
Post a Comment