asp.net - ASP web forms ASCX User control for loop in repeater -
i have repeater model has status each month.
so want use loop inside repeater, problem 1 variable not accessible
code:
<itemtemplate> <% (int month = 1; month <= 12; month++) { %> <div class="ds_monthcol <%= getstatusclassname(item, month) %>"> <asp:hyperlink id="hyperlink1" runat="server"></asp:hyperlink> </div> <% } %> </itemtemplate>
the method getstatusclassname
accessible - no problems. case: the name item not exits in current context
, error visual studio.
if use like:
<%# getstatusclassname(item, month) %>
the name month not exits in current context
how both accessible ?
the idea avoid that:
<div class='ds_monthcol <%# getstatusclassname(item, 1) %>'> <asp:hyperlink id="hyperlink1" runat="server"></asp:hyperlink> </div> <div class='ds_monthcol <%# getstatusclassname(item, 2) %>'> <asp:hyperlink id="hyperlink2" runat="server"></asp:hyperlink> </div> <div class='ds_monthcol <%# getstatusclassname(item, 3) %>'> <asp:hyperlink id="hyperlink3" runat="server"></asp:hyperlink> </div> <div class='ds_monthcol <%# getstatusclassname(item, 4) %>'> <asp:hyperlink id="hyperlink4" runat="server"></asp:hyperlink> </div> <div class='ds_monthcol <%# getstatusclassname(item, 5) %>'> <asp:hyperlink id="hyperlink5" runat="server"></asp:hyperlink> </div> <div class='ds_monthcol <%# getstatusclassname(item, 6) %>'> <asp:hyperlink id="hyperlink6" runat="server"></asp:hyperlink> </div> <div class='ds_monthcol <%# getstatusclassname(item, 7) %>'> <asp:hyperlink id="hyperlink7" runat="server"></asp:hyperlink> </div> <div class='ds_monthcol <%# getstatusclassname(item, 8) %>'> <asp:hyperlink id="hyperlink8" runat="server"></asp:hyperlink> </div> <div class='ds_monthcol <%# getstatusclassname(item, 9) %>'> <asp:hyperlink id="hyperlink9" runat="server"></asp:hyperlink> </div> <div class='ds_monthcol <%# getstatusclassname(item, 10) %>'> <asp:hyperlink id="hyperlink10" runat="server"></asp:hyperlink> </div> <div class='ds_monthcol <%# getstatusclassname(item, 11) %>'> <asp:hyperlink id="hyperlink11" runat="server"></asp:hyperlink> </div> <div class='ds_monthcol <%# getstatusclassname(item, 12) %>'> <asp:hyperlink id="hyperlink12" runat="server"></asp:hyperlink> </div>
you can try using itemdatabound event
<itemtemplate> <asp:placeholder id="ph" runat="server" /> </itemtemplate> string getstatusclassname(object obj, int month) { //your existing function }
use itemdatabound event as:
protected void rpt_itemdatabound(object sender, repeateritemeventargs e) { if (e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem) { //find place holder , add links in place holder placeholder ph = (placeholder)e.item.findcontrol("ph"); //get object of datasource assigned repeater object dataitem = e.item.dataitem;//need add items source (int month = 1; month <= 12; month++) { //create div per example else can directly add links panel , handled display css htmlgenericcontrol div = new htmlgenericcontrol("div"); // set id, class whatever want div.attributes.add("class", "ds_monthcol " + getstatusclassname(dataitem, month)); // declare textbox hyperlink lnk = new hyperlink(); //add link in placeholder div.controls.add(lnk); ph.controls.add(div); } } }
note: code not tested data.
Comments
Post a Comment