Reputation: 1
I am trying to create multiple tables based on SurveyCount(ViewState variable) and populate them on ascx page itself rather than using code behind to populate the data. The number of tables being displayed are correct but I am not able to populate the table cells using foreach loop. The table cell doesn't identify the c variable.
<% if(SurveyCount > 0)
{%>
<% foreach(SurveyAvailabilities c in this.lstSurveyAvailabilities){%>
<asp:Table id="Table2" runat ="server" GridLines="Both" HorizontalAlign="Center">
<asp:TableHeaderRow ForeColor="Snow" BackColor ="#8f001a" Font-Bold="true" >
<asp:TableHeaderCell>Year</asp:TableHeaderCell>
<asp:TableHeaderCell>Language</asp:TableHeaderCell>
<asp:TableHeaderCell>Grade</asp:TableHeaderCell>
<asp:TableHeaderCell>Subject</asp:TableHeaderCell>
</asp:TableHeaderRow>
<asp:TableRow>
<asp:TableCell Text="0" />
<asp:TableCell Text="1" />
<asp:TableCell><%= c.Grade%></asp:TableCell>
<asp:TableCell><%= c.Subject%></asp:TableCell>
</asp:TableRow>
</asp:Table>
<textarea><%= c.Grade%></textarea>
<br>
<% } %>
<% } %>
The text area identifies the element correctly but not the table cells. I do not want to use Gridview or Repeaters. How can I loop through the list and populate table cells?
Upvotes: 0
Views: 524
Reputation: 49039
rather than using code behind to populate the data.
Well, the instant you start placing "<% and some c# code? You ARE in effect using code behind and server side code - that code becomes part of the code behind page. So, little consolation to say that you "trying" to avoid code behind, but then turn around and start writing some code behind for the web page anyway, right???
Now, I do kind of wish we had a lighter weight version of gridview. And often listview is better (since it follows a standard html table quite a bit closer).
So, at the end of the day? Seems to me, to save code, time, efforts, and quite much save world poverty here?
I think if you want some repeating tables? Well, then there is this thing called a REPEATer - (right in the name!!!).
given you have a repeater control, and you can then drop in a GV inside?
It becomes REALLY but REALLY hard to make a case to attempt a different road unless you have a FANTASTIC reason and case for doing so. (and you not done so).
Unless some huge compelling case exists, then it seems to be a simple approach is the best approach, and will also save all that time as a bonus.
I mean, this simple markup - we can have 2 or 15 tables.
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<asp:Label id="MyHeader" runat="server" Text='<%# Eval("TableGroup") %>' Font-Size="X-Large" >
</asp:Label>
<asp:GridView ID="GTable" runat="server"
class="table table-responsive-sm table-bordered table-hover" Width="20%">
</asp:GridView>
</ItemTemplate>
</asp:Repeater>
I mean, golly, already less markup then you posted.
And code behind to load the repeater - say a source of tables - or even a list ?
Say this:
DataTable TblList = new DataTable();
TblList.Columns.Add("TableGroup");
for (int i = 1;i<=3;i++)
{
DataRow OneRow = TblList.NewRow();
OneRow["TableGroup"] = "Table " + i;
TblList.Rows.Add(OneRow);
}
Repeater1.DataSource = TblList;
Repeater1.DataBind();
The above will give us 3 tables!!! - not a lot of code.
For each set, we use Item data bound. Say this:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// each new group/thing, lets fill the grid/table
GridView gV = (GridView)e.Item.FindControl("GTable");
DataTable TestTable = new DataTable();
TestTable.Columns.Add("Name");
TestTable.Columns.Add("Age");
int TableGroup = e.Item.ItemIndex + 1;
// add 3 rows
for (int i = 1; i <= 3; i++)
{
DataRow OneRow = TestTable.NewRow();
OneRow["Name"] = "Group " + TableGroup + " Name " + i;
OneRow["Age"] = i * 15;
TestTable.Rows.Add(OneRow);
}
gV.DataSource = TestTable;
gV.DataBind();
}
And now we get this:
I mean, the above was done quite much in LESS time then it took me to write this post.
You have to ask how long you live, how many days you have, and what each day is worth to you? The above is now done, and in fact I am going to get a pizza. You? Your still here trying to write and cobble up some code.
So, try the above simple idea - it oh so easy, and then you have spare time to go out for pizza.
Upvotes: 0