Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Dynamic Fields in ASP.NET (VB)

I have a table that I need to populate based off queries from a table on page load.

the ASPX page has the following table layout
<table>
	<tr>
		<td><td>
		<td colspan="4">Year 1</td>
		<td colspan="4">Year 2</td>
		<td colspan="4">Year 3</td>
	</tr>
	<tr>
		<td><td>
		<td>Q1</td>
		<td>Q2</td>
		<td>Q3</td>
		<td>Q4</td>
		<td>Q1</td>
		<td>Q2</td>
		<td>Q3</td>
		<td>Q4</td>
		<td>Q1</td>
		<td>Q2</td>
		<td>Q3</td>
		<td>Q4</td>
	</tr>
	<tr>
		<td>SALES</td>
		<td><asp:Literal ID="Y1Q1" Text="" runat="server" /></td>
		<td><asp:Literal ID="Y1Q2" Text="" runat="server" /></td>
		<td><asp:Literal ID="Y1Q3" Text="" runat="server" /></td>
		<td><asp:Literal ID="Y1Q4" Text="" runat="server" /></td>
		<td><asp:Literal ID="Y2Q1" Text="" runat="server" /></td>
		<td><asp:Literal ID="Y2Q2" Text="" runat="server" /></td>
		<td><asp:Literal ID="Y2Q3" Text="" runat="server" /></td>
		<td><asp:Literal ID="Y2Q4" Text="" runat="server" /></td>
		<td><asp:Literal ID="Y3Q1" Text="" runat="server" /></td>
		<td><asp:Literal ID="Y3Q2" Text="" runat="server" /></td>
		<td><asp:Literal ID="Y3Q3" Text="" runat="server" /></td>
		<td><asp:Literal ID="Y3Q4" Text="" runat="server" /></td>
	</tr>
.................................................

Open in new window

The data is being pulled in the backend for each year/quarter.

In the code behind I have the following :
Do While xYear < 4
	Select Case xYear
		Case Is = 1
			thisYear = FirstYear.ToString
		Case Is = 2
			thisYear = SecondYear.ToString
		Case Is = 3
			thisYear = ThirdYear.ToString
	End Select

	Do While xQuarter < 5
		Select Case xQuarter
			Case Is = 1
				thisQuarter = 1
			Case Is = 2
				thisQuarter = 2
			Case Is = 3
				thisQuarter = 3
			Case Is = 4
				thisQuarter = 4
		End Select
		
		'This does the query to get the data I will be working with.
		myRecords = Library.GetLFNSTATS(thisYear + thisQuarter)

		For Each row As DataRow In myRecords.Rows

			thisField = "Y" & xYear & "Q" & xQuarter
			thisControl = Me.FindControl(thisField)
			thisControl.text = row("ColumnName").ToString.Trim
			
		Next
		xQuarter += 1
	Loop
	xYear += 1
Loop

Open in new window


When the code runs, the control referenced by thisControl comes back with nothing.
0
ChaseWoofer
Asked:
ChaseWoofer
  • 2
  • 2
1 Solution
 
Seven priceFull StackCommented:
How are you passing the do while loop to which ID. you have to pass it to the literal ID or to a table ID

<asp:table id="tableID"

how does it know to return?
0
 
Seven priceFull StackCommented:
another example on the server side
 tableID.Rows.Add(r)
0
 
ChaseWooferAuthor Commented:
The solution is as follows:

in the code behind make a reference to the Content Placeholder
Dim MainContent As ContentPlaceHolder = CType(Page.Master.FindControl("MainContent"), ContentPlaceHolder)

Open in new window


Then change :
thisField = "Y" & xYear.ToString & "Q" & xQuarter.ToString
thisControl = Me.FindControl(thisField)
thisControl.text = row("ColumnName").ToString.Trim

Open in new window


thisField = "Y" & xYear.ToString & "Q" & xQuarter.ToString
thisControl = CType(MainContent.FindControl(thisField), Literal)
thisControl.Text = row("ColumnName").ToString.Trim

Open in new window

0
 
ChaseWooferAuthor Commented:
After much playing arround then going out and getting some fresh air, it came to me where I was doing the references for finding the control.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now