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;">
<thead>
<tr>
<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>
</tr>
</thead>
<tbody>
<tr>
<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>
</tr>
<tr>
<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>
</tr>
<tr>
<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>
</tr>
<tr>
<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>
</tr>
<tr>
<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>
</tr>
</tbody>
</table>
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.
Question
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?
1 Answer 1
You want to use an <asp:GridView />
http://msdn.microsoft.com/en-us/library/vstudio/2s019wc0(v=vs.100).aspx
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
fooGrid.DataBind();
There are many more tags and attributes you need to set on the grid view element. See the link above for examples.
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\$<tr>/*stuff between*/</tr>
. This could drastically shorten your code to simply providing the correct collection to the repeater and do nothing more. \$\endgroup\$