I was wondering if there could be an easier or simpler way of filling a table with code behind.

So far I have my html/asp.net table :

<table style="width:275px;">
            <th><asp:Label ID="LBL_StatsH0" runat="server" Text="Section"></asp:Label></th>
            <th><asp:Label ID="LBL_StatsH1" runat="server" Text="Capacity Push (lbf)"></asp:Label></th>
            <th><asp:Label ID="LBL_StatsH2" runat="server" Text="Capacity Pull (lbf)"></asp:Label></th>
            <th><asp:Label ID="LBL_StatsH3" runat="server" Text="Stroke (in)"></asp:Label></th>
            <th><asp:Label ID="LBL_StatsH4" runat="server" Text="Outside diameter (in)"></asp:Label></th>
            <td><asp:Label ID="LBL_StatsStage0Text" runat="server" Text="Master"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage0Value0" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage0Value1" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage0Value2" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage0Value3" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage1Text" runat="server" Text="1st stage"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage1Value0" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage1Value1" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage1Value2" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage1Value3" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage2Text" runat="server" Text="2nd stage"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage2Value0" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage2Value1" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage2Value2" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage2Value3" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage3Text" runat="server" Text="3rd stage"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage3Value0" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage3Value1" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage3Value2" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage3Value3" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage4Text" runat="server" Text="4th stage"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage4Value0" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage4Value1" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage4Value2" runat="server" Text="--"></asp:Label></td>
            <td><asp:Label ID="LBL_StatsStage4Value3" runat="server" Text="--"></asp:Label></td>

And my CodeBehind :

LBL_StatsStage0Value0.Text = "--"
LBL_StatsStage1Value0.Text = Math.Round(cylinderLoadForcePush(1))
LBL_StatsStage2Value0.Text = Math.Round(cylinderLoadForcePush(2))
LBL_StatsStage3Value0.Text = "--"
LBL_StatsStage4Value0.Text = "--"

LBL_StatsStage0Value1.Text = "--"
LBL_StatsStage1Value1.Text = Math.Round(cylinderLoadForcePull(2))
LBL_StatsStage2Value1.Text = Math.Round(cylinderLoadForcePull(2))
LBL_StatsStage3Value1.Text = "--"
LBL_StatsStage4Value1.Text = "--"

LBL_StatsStage0Value2.Text = "--"
LBL_StatsStage1Value2.Text = Math.Round(cylinderLoadLength(1) - cylinderLoadLength(0))
LBL_StatsStage2Value2.Text = Math.Round(cylinderLoadLength(2) - cylinderLoadLength(1))
LBL_StatsStage3Value2.Text = "--"
LBL_StatsStage4Value2.Text = "--"

LBL_StatsStage0Value3.Text = cylinderOutsideDiameter(0)

If UBound(cylinderLoadForcePush) > 2 AndAlso cylinderSectionStroke(3) <> 0 Then
    LBL_StatsStage3Value0.Text = Math.Round(cylinderLoadForcePush(3))
    LBL_StatsStage1Value1.Text = Math.Round(cylinderLoadForcePull(3))
    LBL_StatsStage2Value1.Text = Math.Round(cylinderLoadForcePull(3))
    LBL_StatsStage3Value1.Text = Math.Round(cylinderLoadForcePull(3))
    LBL_StatsStage3Value2.Text = Math.Round(cylinderLoadLength(3) - cylinderLoadLength(2))
    If UBound(cylinderLoadForcePush) > 3 AndAlso cylinderSectionStroke(4) <> 0 Then
        LBL_StatsStage4Value0.Text = Math.Round(cylinderLoadForcePush(4))
        LBL_StatsStage1Value1.Text = Math.Round(cylinderLoadForcePull(4))
        LBL_StatsStage2Value1.Text = Math.Round(cylinderLoadForcePull(4))
        LBL_StatsStage3Value1.Text = Math.Round(cylinderLoadForcePull(4))
        LBL_StatsStage4Value1.Text = Math.Round(cylinderLoadForcePull(4))
        LBL_StatsStage4Value2.Text = Math.Round(cylinderLoadLength(4) - cylinderLoadLength(3))
    End If
End If

As you notice, I have hardcoded line by line every cell. I have also made the name of my cells incremented to make them easier to find or use.


How can I make this code simpler or how can I shorten it? Is there a way that I could implement a For Loop that loops through every cell of the table and adds the proper value to it?

  • \$\begingroup\$ It looks a lot like you could use a Repeater control, but I have no VS handy right now so I can't put code together... \$\endgroup\$ – Vogel612 Aug 14 '14 at 13:53
  • \$\begingroup\$ @Vogel612 Interesting, I read through the documentation of ASP:Repeater. It says that it needs a template.. As this is the only table I have in my website, wouldn't that take even more lines? \$\endgroup\$ – Sifu Aug 14 '14 at 13:57
  • \$\begingroup\$ How that? Your template is one <tr>/*stuff between*/</tr>. This could drastically shorten your code to simply providing the correct collection to the repeater and do nothing more. \$\endgroup\$ – Vogel612 Aug 14 '14 at 13:58
  • \$\begingroup\$ @Vogel612 Oh okay! Nevermind, I misunderstood the documentation, that will indeed drastically shorten my asp.net/html code! Thanks for that. Any thoughts on how I could shorten my CodeBehind(VB)? \$\endgroup\$ – Sifu Aug 14 '14 at 14:07

You want to use an <asp:GridView />


In your code behind you can set the DataSource property and the call DataBind

<asp:GridView runat="server" id="fooGrid" />

And in C#:

fooGrid.DataSource = // some array or IEnumerable or DataTable

There are many more tags and attributes you need to set on the grid view element. See the link above for examples.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.