6 Replies - 3236 Views - Last Post: 25 May 2009 - 09:11 PM Rate Topic: -----

#1 gymratz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 18-October 07

Accessing Dynamically Created Labels

Posted 28 April 2009 - 03:07 PM

My current project involves displaying questions/answers from two different databases to be used to compare values and make judgements.
The number of questions, choices, and answers is all variable. Thus I am attempting to create labels dynamically to show the information on the screen and create textboxes to leave comments.
My problem is not knowing how to then access these lables, since I can't code in to use "label_comments" & (i) as it doesn't exist (at least, I can't get it to work).

First, my code for creating and displaying the information:

		db_connection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionStringMerrill3"))
		db_connection.Open()

		db_connection2 = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionStringMerrill2"))
		db_connection2.Open()

		db_command = New SqlCommand("Select Q.Question_Text as [Question], Case When C.Answer_Text = '[NULL]' then null else C.Answer_Text end as [Choice], Case When A.Choice_Value = ' ' then null else A.Choice_Value end [Value], Case When A.Choice_Value2 = ' ' then null else A.Choice_Value2 end [Value2], Case When A.Choice_Value3 = ' ' then null else A.Choice_Value3 end [Value3], A.Min_Percent, A.Target_Percent, A.Max_Percent, A.Answer_Type, Q.Old_Question_Table, Q.Old_Question_Field, A.MLS_ID, A.Answer_ID, A.Materially_Diff, A.Comments From Answers as A INNER JOIN Questions as Q on Q.MLQ_ID = A.MLQ_ID INNER JOIN Choices as C on C.MLC_ID = A.MLC_ID AND C.MLQ_ID = A.MLQ_ID Where A.MLS_ID = '81282' Order By Q.Question_ID", db_connection)
		datareader = db_command.ExecuteReader()

		Dim Previous_Question As String = ""
		Dim question_count As Int16 = 1

		While datareader.Read   'Any questions for this Strategy?

			If Previous_Question <> datareader("Question").ToString Then 'Only write the question if it is a new question.
				question_count = question_count + 1
				MainTable.Rows.Insert(question_count, New HtmlTableRow())
				MainTable.Rows(question_count).Cells.Add(New HtmlTableCell())
				'MainTable.Rows(question_count).Style.Add("page-break-before", "always")

				Dim l2 As New Label
				l2.ID = "lbl_q" & (question_count).ToString
				l2.Font.Size = FontUnit.Point(12)
				l2.Font.Name = "Times New Roman"
				l2.Font.Bold = True
				l2.Attributes.Add("runat", "server")
				l2.Width = Unit.Pixel(1100)
				l2.ForeColor = Drawing.Color.Black
				l2.Text = datareader("Question").ToString ' & "<br>New Answer:"

				With MainTable.Rows(question_count).Cells(0)
					'.BgColor = "#ebf8ff"
					.Width = "15px"
					.Align = "left"
					.VAlign = "top"
					.Controls.Add(l2)
				End With


				If datareader("Old_Question_Field").ToString <> "" Then
					'Old Answer...
					Dim l7 As New Label
					l7.ID = "lbl_o" & (question_count).ToString
					l7.Font.Size = FontUnit.Point(11)
					l7.Font.Name = "Times New Roman"
					l7.Font.Bold = True
					l7.Attributes.Add("runat", "server")
					l7.Width = Unit.Pixel(1100)
					l7.ForeColor = Drawing.Color.Black
					l7.Text = "Old Answer:"

					With MainTable.Rows(question_count).Cells(0)
						'.BgColor = "#ebf8ff"
						.Width = "15px"
						.Align = "left"
						.VAlign = "top"
						.Controls.Add(l7)
					End With
					'End

					db_command2 = New SqlCommand("Select " & datareader("Old_Question_field").ToString & " as [temp] from " & datareader("Old_Question_Table").ToString & " Where MLS_ID = '" & datareader("MLS_ID").ToString & "'", db_connection2)
					datareader2 = db_command2.ExecuteReader()
					While datareader2.Read
						Dim l8 As New Label
						l8.ID = "lbl_o2" & (question_count).ToString
						l8.Font.Size = FontUnit.Point(11)
						l8.Font.Name = "Times New Roman"
						l8.Font.Bold = False
						l8.Attributes.Add("runat", "server")
						l8.Width = Unit.Pixel(1100)
						l8.ForeColor = Drawing.Color.Black
						l8.Text = datareader2("temp").ToString

						With MainTable.Rows(question_count).Cells(0)
							'.BgColor = "#ebf8ff"
							.Width = "15px"
							.Align = "left"
							.VAlign = "top"
							.Controls.Add(l8)
						End With
					End While
					datareader2.Close()


					'Old Answer...
					Dim t1 As New TextBox
					t1.ID = "txt_" & (question_count).ToString
					t1.Font.Size = FontUnit.Point(11)
					t1.Font.Name = "Times New Roman"
					t1.Font.Bold = False
					t1.Attributes.Add("runat", "server")
					t1.Width = Unit.Pixel(1100)
					t1.ForeColor = Drawing.Color.Black
					t1.Text = datareader("Comments").ToString


					With MainTable.Rows(question_count).Cells(0)
						'.BgColor = "#ebf8ff"
						.Width = "150px"
						.Align = "left"
						.VAlign = "top"
						.Controls.Add(t1)
					End With
					'End

					Dim lcom As New Label
					lcom.ID = "lbl_comm" & (question_count).ToString
					lcom.Font.Size = FontUnit.Point(11)
					lcom.Font.Name = "Times New Roman"
					lcom.Font.Bold = False
					lcom.Attributes.Add("runat", "server")
					lcom.Width = Unit.Pixel(100)
					lcom.ForeColor = Drawing.Color.Black
					lcom.Text = datareader("Comments").ToString
					lcom.Visible = False

					With MainTable.Rows(question_count).Cells(0)
						'.BgColor = "#ebf8ff"
						.Width = "15px"
						.Align = "left"
						.VAlign = "top"
						.Controls.Add(lcom)
					End With

					Dim l9 As New Label
					l9.ID = "lbl_ID" & (question_count).ToString
					l9.Font.Size = FontUnit.Point(11)
					l9.Font.Name = "Times New Roman"
					l9.Font.Bold = False
					l9.Attributes.Add("runat", "server")
					l9.Width = Unit.Pixel(100)
					l9.ForeColor = Drawing.Color.Black
					l9.Text = datareader("Answer_ID").ToString
					l9.Visible = False

					With MainTable.Rows(question_count).Cells(0)
						'.BgColor = "#ebf8ff"
						.Width = "15px"
						.Align = "left"
						.VAlign = "top"
						.Controls.Add(l9)
					End With

					Dim d1 As New DropDownList
					d1.ID = "ddl_" & (question_count).ToString
					d1.Font.Size = FontUnit.Point(11)
					d1.Font.Name = "Times New Roman"
					d1.Font.Bold = False
					d1.Attributes.Add("runat", "server")
					d1.Width = Unit.Pixel(150)
					d1.ForeColor = Drawing.Color.Black
					d1.Items.Insert(0, "Fail")
					d1.Items.Insert(0, "Needs Improvement")
					d1.Items.Insert(0, "Pass")
					d1.Items.Insert(0, "Select...")

					With MainTable.Rows(question_count).Cells(0)
						'.BgColor = "#ebf8ff"
						.Width = "150px"
						.Align = "left"
						.VAlign = "top"
						.Controls.Add(d1)
					End With
					'End
				End If

				'New Answer...
				Dim l6 As New Label
				l6.ID = "lbl_n" & (question_count).ToString
				l6.Font.Size = FontUnit.Point(11)
				l6.Font.Name = "Times New Roman"
				l6.Font.Bold = True
				l6.Attributes.Add("runat", "server")
				l6.Width = Unit.Pixel(1100)
				l6.ForeColor = Drawing.Color.Black
				l6.Text = "New Answer:"

				With MainTable.Rows(question_count).Cells(0)
					'.BgColor = "#ebf8ff"
					.Width = "15px"
					.Align = "left"
					.VAlign = "top"
					.Controls.Add(l6)
				End With
				'End

				Previous_Question = datareader("Question").ToString 'Used to know if I am on a new question or not.
			End If


			'This section displays the choices chosen, and any corresponding values.
			Dim l4 As New Label
			l4.ID = "lbl_c" & (question_count).ToString
			l4.Font.Size = FontUnit.Point(11)
			l4.Font.Name = "Times New Roman"
			l4.Font.Bold = False
			l4.Attributes.Add("runat", "server")
			l4.Width = Unit.Pixel(1100)
			l4.ForeColor = Drawing.Color.Black
			If datareader("Choice").ToString <> "" Then
				l4.Text = datareader("Choice").ToString
			End If

			If datareader("Answer_Type").ToString = "NUM" Then
				l4.Text = l4.Text & " <br>Min: " & datareader("Min_Percent").ToString & "  Target: " & datareader("Target_Percent") & "  Max: " & datareader("Max_Percent") & "<br><br>"
			Else
				If datareader("Value").ToString <> "on" Then
					If l4.Text <> "" Then
						If datareader("Value2").ToString <> "" Then
							l4.Text = l4.Text & ": " & datareader("Value").ToString & " // " & datareader("Value2") & " // " & datareader("Value3") & "<br><br>"
						Else
							l4.Text = l4.Text & ": " & datareader("Value").ToString & "<br><br>"
						End If
					Else
						If datareader("Value2").ToString <> "" Then
							l4.Text = datareader("Value").ToString & " // " & datareader("Value2") & " // " & datareader("Value3") & "<br><br>"
						Else
							l4.Text = datareader("Value").ToString & "<br><br>"
						End If
					End If
				End If
			End If

			With MainTable.Rows(question_count).Cells(0)
				'.BgColor = "#ebf8ff"
				.Width = "15px"
				.Align = "left"
				.VAlign = "top"
				.Controls.Add(l4)
			End With
			'End

		End While
		datareader.Close()
		db_connection.Close()
		db_connection2.Close()
		lbl_question_count.Text = question_count


My attempt at cycling through the page to save the data back:
 db_connection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionStringMerrill3"))
		db_connection.Open()

		Dim i As Int16

		For i = 1 To lbl_question_count.Text
			Dim comm, l9 As Label
			comm.ID = "lbl_comm" & (i).ToString
			l9.ID = "lbl_ID" & (i).ToString
			db_command = New SqlCommand("Update Answers SET Comments = '" & Clean_Text(comm.Text) & "' where Answer_ID = '" & l9.Text & "'", db_connection)
			db_command.ExecuteNonQuery()
		Next
		db_connection.Close()


Displaying the data works great (may not be the best method - but it's what I know how to do... hah). Saving it isn't even close to working.

Is This A Good Question/Topic? 0
  • +

Replies To: Accessing Dynamically Created Labels

#2 specialfreckles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 06-March 09

Re: Accessing Dynamically Created Labels

Posted 29 April 2009 - 06:12 PM

To get you closer-

Would wrapping your labels in a class to get arrays help you?

Friend Class clsLabel
	inherits Label

	dim mInstance as integer

	friend Sub New( ByVal zInstance as integer, ByVal zText as string)
		mInstance= zInstance
		Text = zText
	end sub

	friend readonly property Instance() as integer
		get
			return mInstance
		end get
	 end property

end class

dim SomeLabel(10)  as clsLabel


Was This Post Helpful? 0
  • +
  • -

#3 gymratz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 18-October 07

Re: Accessing Dynamically Created Labels

Posted 01 May 2009 - 08:47 AM

That didn't appear to get me anywhere; however, in stepping through all of the code I think I found the problem.
For some reason my table MainTable goes away on any postback.
Example:
If I put all of my code in a Page_Load : If Not Page.IsPostBack sub, it populates the table on page load. If I do anything that makes the page post back, the table is gone...

I can not see anything in my code that is erasing my table!

Below is the entire code for the page if it helps - I could really use some help here!
Thanks,

Imports System.Data.SqlClient
Imports System.DirectoryServices
Imports System.Security.Principal
Partial Class Merrill_MerrillQuestionnaire_Compare
	Inherits System.Web.UI.Page
	Public db_connection As SqlConnection
	Public db_command As SqlCommand
	Public datareader As SqlDataReader

	Public db_connection2 As SqlConnection
	Public db_command2 As SqlCommand
	Public datareader2 As SqlDataReader
	Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
		'Populate_Page()
	End Sub
	Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
		'Do not cache page
		Response.CacheControl = "no-cache"
		Response.AddHeader("Pragma", "no-cache")
		Response.Expires = -1

		If Not Page.IsPostBack Then
			MainTable.Border = 1
			Dim today As DateTime
			today = Date.Now

			lbl_date.Text = Now()
			populate_page()
		End If
	End Sub
	Sub populate_page()
		db_connection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionStringMerrill3"))
		db_connection.Open()

		db_connection2 = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionStringMerrill2"))
		db_connection2.Open()

		db_command = New SqlCommand("Select Q.Question_Text as [Question], Case When C.Answer_Text = '[NULL]' then null else C.Answer_Text end as [Choice], Case When A.Choice_Value = '&nbsp;' then null else A.Choice_Value end [Value], Case When A.Choice_Value2 = '&nbsp;' then null else A.Choice_Value2 end [Value2], Case When A.Choice_Value3 = '&nbsp;' then null else A.Choice_Value3 end [Value3], A.Min_Percent, A.Target_Percent, A.Max_Percent, A.Answer_Type, Q.Old_Question_Table, Q.Old_Question_Field, A.MLS_ID, A.Answer_ID From Answers as A INNER JOIN Questions as Q on Q.MLQ_ID = A.MLQ_ID INNER JOIN Choices as C on C.MLC_ID = A.MLC_ID AND C.MLQ_ID = A.MLQ_ID Where A.MLS_ID = '81282' Order By Q.Question_ID", db_connection)
		datareader = db_command.ExecuteReader()

		Dim Previous_Question As String = ""
		Dim question_count As Int16 = 1

		While datareader.Read   'Any questions for this Strategy?

			If Previous_Question <> datareader("Question").ToString Then 'Only write the question if it is a new question.
				question_count = question_count + 1
				MainTable.Rows.Insert(question_count, New HtmlTableRow())
				MainTable.Rows(question_count).Cells.Add(New HtmlTableCell())
				'MainTable.Rows(question_count).Style.Add("page-break-before", "always")

				Dim l2 As New Label
				l2.ID = "lbl_q" & (question_count).ToString
				l2.Font.Size = FontUnit.Point(12)
				l2.Font.Name = "Times New Roman"
				l2.Font.Bold = True
				l2.Attributes.Add("runat", "server")
				l2.Width = Unit.Pixel(1100)
				l2.ForeColor = Drawing.Color.Black
				l2.Text = datareader("Question").ToString ' & "<br>New Answer:"

				With MainTable.Rows(question_count).Cells(0)
					'.BgColor = "#ebf8ff"
					.Width = "15px"
					.Align = "left"
					.VAlign = "top"
					.Controls.Add(l2)
				End With


				If datareader("Old_Question_Field").ToString <> "" Then
					'Old Answer...
					Dim l7 As New Label
					l7.ID = "lbl_n" & (question_count).ToString
					l7.Font.Size = FontUnit.Point(11)
					l7.Font.Name = "Times New Roman"
					l7.Font.Bold = True
					l7.Attributes.Add("runat", "server")
					l7.Width = Unit.Pixel(1100)
					l7.ForeColor = Drawing.Color.Black
					l7.Text = "Old Answer:"

					With MainTable.Rows(question_count).Cells(0)
						'.BgColor = "#ebf8ff"
						.Width = "15px"
						.Align = "left"
						.VAlign = "top"
						.Controls.Add(l7)
					End With
					'End

					db_command2 = New SqlCommand("Select " & datareader("Old_Question_field").ToString & " as [temp] from " & datareader("Old_Question_Table").ToString & " Where MLS_ID = '" & datareader("MLS_ID").ToString & "'", db_connection2)
					datareader2 = db_command2.ExecuteReader()
					While datareader2.Read
						Dim l8 As New Label
						l8.ID = "lbl_n" & (question_count).ToString
						l8.Font.Size = FontUnit.Point(11)
						l8.Font.Name = "Times New Roman"
						l8.Font.Bold = False
						l8.Attributes.Add("runat", "server")
						l8.Width = Unit.Pixel(1100)
						l8.ForeColor = Drawing.Color.Black
						l8.Text = datareader2("temp").ToString

						With MainTable.Rows(question_count).Cells(0)
							'.BgColor = "#ebf8ff"
							.Width = "15px"
							.Align = "left"
							.VAlign = "top"
							.Controls.Add(l8)
						End With
					End While
					datareader2.Close()


					'Old Answer...
					Dim t1 As New TextBox
					t1.ID = "txt_" & (question_count).ToString
					t1.Font.Size = FontUnit.Point(11)
					t1.Font.Name = "Times New Roman"
					t1.Font.Bold = False
					t1.Attributes.Add("runat", "server")
					t1.Width = Unit.Pixel(1100)
					t1.ForeColor = Drawing.Color.Black


					With MainTable.Rows(question_count).Cells(0)
						'.BgColor = "#ebf8ff"
						.Width = "150px"
						.Align = "left"
						.VAlign = "top"
						.Controls.Add(t1)
					End With
					'End

					Dim l9 As New Label
					l9.ID = "lbl_ID" & (question_count).ToString
					l9.Font.Size = FontUnit.Point(11)
					l9.Font.Name = "Times New Roman"
					l9.Font.Bold = False
					l9.Attributes.Add("runat", "server")
					l9.Width = Unit.Pixel(100)
					l9.ForeColor = Drawing.Color.Black
					l9.Text = datareader("Answer_ID").ToString
					l9.Visible = False

					With MainTable.Rows(question_count).Cells(0)
						'.BgColor = "#ebf8ff"
						.Width = "15px"
						.Align = "left"
						.VAlign = "top"
						.Controls.Add(l9)
					End With

					Dim d1 As New DropDownList
					d1.ID = "txt_" & (question_count).ToString
					d1.Font.Size = FontUnit.Point(11)
					d1.Font.Name = "Times New Roman"
					d1.Font.Bold = False
					d1.Attributes.Add("runat", "server")
					d1.Width = Unit.Pixel(150)
					d1.ForeColor = Drawing.Color.Black
					d1.Items.Insert(0, "Fail")
					d1.Items.Insert(0, "Needs Improvement")
					d1.Items.Insert(0, "Pass")
					d1.Items.Insert(0, "Select...")

					With MainTable.Rows(question_count).Cells(0)
						'.BgColor = "#ebf8ff"
						.Width = "150px"
						.Align = "left"
						.VAlign = "top"
						.Controls.Add(d1)
					End With
					'End
				End If

				'New Answer...
				Dim l6 As New Label
				l6.ID = "lbl_n" & (question_count).ToString
				l6.Font.Size = FontUnit.Point(11)
				l6.Font.Name = "Times New Roman"
				l6.Font.Bold = True
				l6.Attributes.Add("runat", "server")
				l6.Width = Unit.Pixel(1100)
				l6.ForeColor = Drawing.Color.Black
				l6.Text = "New Answer:"

				With MainTable.Rows(question_count).Cells(0)
					'.BgColor = "#ebf8ff"
					.Width = "15px"
					.Align = "left"
					.VAlign = "top"
					.Controls.Add(l6)
				End With
				'End

				Previous_Question = datareader("Question").ToString 'Used to know if I am on a new question or not.
			End If


			'This section displays the choices chosen, and any corresponding values.
			Dim l4 As New Label
			l4.ID = "lbl_c" & (question_count).ToString
			l4.Font.Size = FontUnit.Point(11)
			l4.Font.Name = "Times New Roman"
			l4.Font.Bold = False
			l4.Attributes.Add("runat", "server")
			l4.Width = Unit.Pixel(1100)
			l4.ForeColor = Drawing.Color.Black
			If datareader("Choice").ToString <> "" Then
				l4.Text = datareader("Choice").ToString
			End If

			If datareader("Answer_Type").ToString = "NUM" Then
				l4.Text = l4.Text & " <br>Min: " & datareader("Min_Percent").ToString & "  Target: " & datareader("Target_Percent") & "  Max: " & datareader("Max_Percent") & "<br><br>"
			Else
				If datareader("Value").ToString <> "on" Then
					If l4.Text <> "" Then
						If datareader("Value2").ToString <> "" Then
							l4.Text = l4.Text & ": " & datareader("Value").ToString & " // " & datareader("Value2") & " // " & datareader("Value3") & "<br><br>"
						Else
							l4.Text = l4.Text & ": " & datareader("Value").ToString & "<br><br>"
						End If
					Else
						If datareader("Value2").ToString <> "" Then
							l4.Text = datareader("Value").ToString & " // " & datareader("Value2") & " // " & datareader("Value3") & "<br><br>"
						Else
							l4.Text = datareader("Value").ToString & "<br><br>"
						End If
					End If
				End If
			End If

			With MainTable.Rows(question_count).Cells(0)
				'.BgColor = "#ebf8ff"
				.Width = "15px"
				.Align = "left"
				.VAlign = "top"
				.Controls.Add(l4)
			End With
			'End
		End While
		datareader.Close()
	End Sub
	Protected Sub btn_submit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_submit.Click
		
	End Sub

	Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
		db_connection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionStringWF"))
		db_connection.Open()
		For i As Integer = 3 To MainTable.Rows.Count
			Try
				Dim Comments As Label = MainTable.Rows(i).Cells(0).Controls(3)
				Dim Answer_ID As Label = MainTable.Rows(i).Cells(0).Controls(4)
			Catch ex As Exception
				Exit For
			End Try
		Next
		db_connection.Close()
	End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Accessing Dynamically Created Labels

Posted 01 May 2009 - 08:49 AM

Moved to ASP.NET :)
Was This Post Helpful? 0
  • +
  • -

#5 gymratz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 18-October 07

Re: Accessing Dynamically Created Labels

Posted 01 May 2009 - 09:58 AM

To follow up with more information:
I looked at old pages that I have done something similar in. In those pages I always populated the MainTable using page_init.

Question 1.
If I create a textbox, type in it, then "submit" the page. In the background I see that it is hitting the page_init again and recreating the table. When I create the table, I potentially put data in the textbox - what I want to do is take the new data in the textbox and update it back to the server.......
Gah!

Question 2.
(This was user error, fixed this).

This post has been edited by gymratz: 01 May 2009 - 10:38 AM

Was This Post Helpful? 0
  • +
  • -

#6 gymratz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 18-October 07

Re: Accessing Dynamically Created Labels

Posted 01 May 2009 - 01:24 PM

Got it working...
Still don't understand how it works (My "Question 1" confuses the snot out of me).

Thanks,
Was This Post Helpful? 0
  • +
  • -

#7 Rathika  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-April 09

Re: Accessing Dynamically Created Labels

Posted 25 May 2009 - 09:11 PM

Hope this might solve your questions.


http://forums.asp.net/t/1298850.aspx



Thanks,
Rathika.K
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1