I am populating some data in a table with 6 columns using a repeater control in ASP.NET (VB) using the enclosed code.
I want a piece of code to achieve the following functionality by attaching an if/elseif/else condition to the repeater controll:
For the record with<%#Eval("SOLID")%> = "40103" then the data to be displayed in one cell, merging all 6 columns in to one (ideally using colspan = 6).
For the record having <%#Eval("SOLID")%> = "40105" then the data to be displayed in two cell, merging the second and third column.
For all the other records the data to be displayed in the actual format.
I have gone through various sites for a solution, but in vain. Please help me
<asp:Repeater ID="Repeater3" runat="server">
<headertemplate>
<tr style="height:25px;vertical-align:middle">
<th class ="dg_rpt_center" align="left" width = "10%" >
<asp:Label ID="Label7" runat="server" class ="lbl105" >SOLID</asp:Label></th>
<th class ="dg_rpt_center" align="left" width = "30%">
<asp:Label ID="Label10" runat="server" class ="lbl105" >SOL Name</asp:Label></th>
<th class ="dg_rpt_center" align="right" width = "10%">
<asp:Label ID="Label11" runat="server" class ="lbl105" >Target</asp:Label></th>
<th class ="dg_rpt_center" align="right" width = "10%">
<asp:Label ID="Label12" runat="server" class ="lbl105" >Achievement</asp:Label></th>
<th class ="dg_rpt_center" align="right" width = "10%">
<asp:Label ID="Label13" runat="server" class ="lbl105" >Margin</asp:Label></th>
<th class ="dg_rpt_center" align="right" width = "10%" >
<asp:Label ID="Label14" runat="server" class ="lbl105" >Growth</asp:Label></th>
</tr>
</headertemplate>
<ItemTemplate>
<tr>
<td align="left" width = "10%"><asp:LinkButton ID="lblsolid" runat="server" class ="lbl9N" Text='<%#Eval("SOLID")%>' OnClick='subDtClick' CommandArgument='<%#Eval("SLNO")%>'></asp:LinkButton></td>
<td align="left" width = "30%"><asp:Label ID="lblsolname" runat="server" class ="lbl9N" Text='<%#Eval("NAME")%>'></asp:Label></td>
<td align="right" width = "10%"><asp:Label ID="lbltgt" runat="server" class ="lbl9N" Text='<%#Eval("TGT")%>'></asp:Label></td>
<td align="right" width = "10%"><asp:Label ID="lblach" runat="server" class ="lbl9N" Text='<%#Eval("ACH")%>'></asp:Label></td>
<td align="right" width = "10%"><asp:Label ID="lblmrgn" runat="server" class ="lbl9N" Text='<%#Eval("MGR")%>'></asp:Label></td>
<td align="right" width = "10%"><asp:Label ID="lblgr" runat="server" class ="lbl9N" Text='<%#Eval("GRW")%>'></asp:Label></td>
</tr>
</ItemTemplate>
</asp:Repeater>
To achieve this you can write your logic in the ItemDataBound
event of the Repeater control. You will need to get references of your table cells and labels, then set your colspan
, merge your label values, and hide any extra cells.
ASPX page
<asp:Repeater ID="Repeater3" runat="server" OnItemDataBound="Repeater3_ItemDataBound">
<HeaderTemplate>
<tr style="height: 25px; vertical-align: middle">
<th class="dg_rpt_center" align="left" width="10%">
<asp:Label ID="Label7" runat="server" class="lbl105">SOLID</asp:Label></th>
<th class="dg_rpt_center" align="left" width="30%">
<asp:Label ID="Label10" runat="server" class="lbl105">SOL Name</asp:Label></th>
<th class="dg_rpt_center" align="right" width="10%">
<asp:Label ID="Label11" runat="server" class="lbl105">Target</asp:Label></th>
<th class="dg_rpt_center" align="right" width="10%">
<asp:Label ID="Label12" runat="server" class="lbl105">Achievement</asp:Label></th>
<th class="dg_rpt_center" align="right" width="10%">
<asp:Label ID="Label13" runat="server" class="lbl105">Margin</asp:Label></th>
<th class="dg_rpt_center" align="right" width="10%">
<asp:Label ID="Label14" runat="server" class="lbl105">Growth</asp:Label></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td id="tdSOLID" runat="server" align="left" width="10%">
<asp:LinkButton ID="lblsolid" runat="server" class="lbl9N" Text='<%#Eval("SOLID")%>' CommandArgument='<%#Eval("SLNO")%>'></asp:LinkButton></td>
<td id="tdNAME" runat="server" align="left" width="30%">
<asp:Label ID="lblsolname" runat="server" class="lbl9N" Text='<%#Eval("NAME")%>'></asp:Label></td>
<td id="tdTGT" runat="server" align="right" width="10%">
<asp:Label ID="lbltgt" runat="server" class="lbl9N" Text='<%#Eval("TGT")%>'></asp:Label></td>
<td id="tdACH" runat="server" align="right" width="10%">
<asp:Label ID="lblach" runat="server" class="lbl9N" Text='<%#Eval("ACH")%>'></asp:Label></td>
<td id="tdMGR" runat="server" align="right" width="10%">
<asp:Label ID="lblmrgn" runat="server" class="lbl9N" Text='<%#Eval("MGR")%>'></asp:Label></td>
<td id="tdGRW" runat="server" align="right" width="10%">
<asp:Label ID="lblgr" runat="server" class="lbl9N" Text='<%#Eval("GRW")%>'></asp:Label></td>
</tr>
</ItemTemplate>
</asp:Repeater>
Code Behind
public void Repeater3_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
var lblsolid = (LinkButton)e.Item.FindControl("lblsolid");
if (lblsolid.CommandArgument == "40103" || lblsolid.CommandArgument == "40105") {
//Labels
var lblsolname = (Label) e.Item.FindControl("lblsolname");
var lbltgt = (Label) e.Item.FindControl("lbltgt");
var lblach = (Label) e.Item.FindControl("lblach");
var lblmrgn = (Label) e.Item.FindControl("lblmrgn");
var lblgr = (Label) e.Item.FindControl("lblgr");
//Table Cells
var tdSOLID = (HtmlTableCell)e.Item.FindControl("tdSOLID");
var tdNAME = (HtmlTableCell)e.Item.FindControl("tdNAME");
var tdTGT = (HtmlTableCell)e.Item.FindControl("tdTGT");
var tdACH = (HtmlTableCell)e.Item.FindControl("tdACH");
var tdMGR = (HtmlTableCell)e.Item.FindControl("tdMGR");
var tdGRW = (HtmlTableCell)e.Item.FindControl("tdGRW");
if (lblsolid.CommandArgument == "40103") {
//merge all label values into a single string
var mergedCellValues = string.Format("{0} {1} {2} {3} {4}", lblsolname.Text, lbltgt.Text, lblach.Text, lblmrgn.Text, lblgr.Text);
//set the text value of the first table cell and span it 6 columns
tdSOLID.InnerText = mergedCellValues;
tdSOLID.Attributes.Add("colspan", "6");
//remove extra cells
tdNAME.Visible = false;
tdTGT.Visible = false;
tdACH.Visible = false;
tdMGR.Visible = false;
tdGRW.Visible = false;
} else if (lblsolid.CommandArgument == "40105") {
//same concept as above, but with different colspan and cell hiding
}
}
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments