4
\$\begingroup\$

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?

asked Aug 14, 2014 at 13:11
\$\endgroup\$
4
  • \$\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\$ Commented Aug 14, 2014 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\$ Commented Aug 14, 2014 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\$ Commented Aug 14, 2014 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\$ Commented Aug 14, 2014 at 14:07

1 Answer 1

3
\$\begingroup\$

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.

answered Aug 15, 2014 at 0:05
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.