What is the cleanest way to make a dynamic list of buttons in asp.net? -


i want change list dynamically populated:

<ul>  <li id="tab1" class="selected" runat="server">      <asp:linkbutton id="linkbutton1" onclick="linkbutton1_click"runat="server">          tab1 text      </asp:linkbutton>  </li>  <li id="tab2" runat="server">      <asp:linkbutton id="linkbutton2" onclick="linkbutton2_click" runat="server">          tab2 text      </asp:linkbutton>  </li>  <li id="tab3" runat="server">      <asp:linkbutton id="linkbutton3" onclick="linkbutton3_click" runat="server">          tab3 text      </asp:linkbutton>  </li> </ul> 

i tried using code:

<asp:listview id="listview_tabs" onitemcommand="listview_changetab" runat="server">     <layouttemplate>         <div class="tabs">             <ul>                 <li id="itemplaceholder" runat="server" />             </ul>         </div>     </layouttemplate>     <itemtemplate>         <li>             <asp:linkbutton text='<%# eval("displayname") %>' commandname='<%# eval("parametername") %>' runat="server"/>         </li>     </itemtemplate> </asp:listview> 

and populate in code behind:

  listview_tabs.datasource = comeclass.gettabs();   listview_tabs.databind(); 

but don't see clear way set class='selected' last link clicked ( or rather list item contains link )

so question

what cleanest way make dynamic list of buttons in asp.net, or in .net?

it doesn't have approach. i'm not sure using listview best approach solve this.

you can leverage selecteditemtemplate , selectedindex attributes of listview accomplish this.

change commandname property "select" in listitemtemplate. way when the postback occurs, listview have row index of linkbutton selected. can set custom command commandargument paramter custom processing when itemselected event raised clicking linkbutton.

then in selecteditemtemplate, can apply class right there. listview this:

<asp:listview id="listview_tabs" onitemcommand="listview_changetab" runat="server">     <layouttemplate>         <div class="tabs">             <ul>                 <li id="itemplaceholder" runat="server" />             </ul>         </div>     </layouttemplate>     <itemtemplate>         <li>             <asp:linkbutton text='<%# eval("displayname") %>' commandname="select"  commandargument='<%# eval("parametername") %>' runat="server"/>         </li>     </itemtemplate>     <selecteditemtemplate>         <li>             <asp:linkbutton text='<%# eval("displayname") %>' commandname="select" commandargument='<%# eval("parametername") %>' cssclass="selected" runat="server"/>         </li>     </selecteditemtemplate> </asp:listview> 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -