Conditional statement for Repeater Controll in ASP.NET

Franklin.K.F

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:

  1. 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).

  2. For the record having <%#Eval("SOLID")%> = "40105" then the data to be displayed in two cell, merging the second and third column.

  3. 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>
zgood

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ASP.Net repeater control - using conditional statements

From Dev

NavigateUrl on Repeater ASP.net

From Dev

NavigateUrl on Repeater ASP.net

From Dev

Using Conditional Statement in ASP.NET Control Attribute

From Dev

Using Conditional Statement in ASP.NET Control Attribute

From Dev

ASP.NET Repeater - Eval() for bool?

From Dev

Convert Bootstrap Carousel to ASP.Net Repeater

From Dev

ASP .NET Using a Repeater inside of UpdatePanel with UpdateProgress

From Dev

asp.net repeater for each row

From Dev

repeater item command in asp.net

From Dev

to disable a label of repeater on some condition in asp .net

From Dev

Using Bootstrap tabs with ASP NET repeater

From Dev

asp.net repeater use template in javascript

From Dev

To bind images in repeater using asp.net

From Dev

asp.net repeater merge columns dynamically

From Dev

Fix header row in repeater in ASP.NET

From Dev

ASP.NET Converting repeater item to the textbox

From Dev

Repeater asp.net tag not working

From Dev

asp.net repeater merge columns dynamically

From Dev

Convert Bootstrap Carousel to ASP.Net Repeater

From Dev

Fix header row in repeater in ASP.NET

From Dev

trying Image Button in repeater Asp.net

From Dev

asp net dropdown selectedvalue/selectedindex not updating in repeater

From Dev

ASP.Net - Issues with Repeater / DataBinding

From Dev

Repeater in asp.net from DB with DropDownList

From Dev

Adding Header to the repeater in asp.net

From Dev

ASP.NET Repeater Control - Getting Hiddenfield value inside the repeater control

From Dev

ASP.NET Repeater Control - Getting Hiddenfield value inside the repeater control

From Dev

button click event in repeater control to show data in same repeater in asp.net c#

Related Related

HotTag

Archive