Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem in Dynamically created Controls

    Hi all

    I am new to VB.Net.


    I am creating controls dynamically.

    While running the project, controls are displayed correct.

    Button clicks are working properly. But when entering to textbox automatically changes the button values.


    Please help in this regard.


    my codings for Page_Load

    Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    	If sqlConn.State = ConnectionState.Closed Then
    		sqlConn.Open()
    	End If
    
    	If Not IsPostBack Then
    		Dim sqlComm1 As New SqlCommand("select DESCR FROM MAIN ORDER BY SID", sqlConn)
    		Dim r1 As SqlDataReader = sqlComm1.ExecuteReader()
    		radempfb.Items.Clear()
    		While r1.Read()
    			radempfb.Items.Add(r1(0).ToString)
    		End While
    		r1.Close()
    
    		Calendar1.Visible = False
    	End If
    
    	TAB()
    End Sub
    TAB() for creating controls dynamically

    Code:
    Private Sub TAB()
    	Panel3.Controls.Add(TABLE1) 'place the table in panel3 
    
    	Dim str As String = radempfb.Text 'stores value of radiobuttonlist
    	If sqlConn.State = ConnectionState.Closed Then
    		sqlConn.Open()
    	End If
    	'tel(Me)
    	'select sid from main table depending on radiobutton selected value
    
    	scm = New SqlCommand("select SID  FROM MAIN where DESCR='" + Trim(str.ToString) + "'  ", sqlConn)
    	sr = scm.ExecuteReader()
    
    	If sr.Read Then
    		Dim st As String = sr(0).ToString 'stores sid of selected radio button
    		sr.Close()
    
    		'if sid found, then check whether it is in submain table
    
    		scm = New SqlCommand("select DESCRI,SUBID,colms FROM SUBMAIN where SID='" + Trim(st.ToString) + "' order by right(sid,2),right(subid,2)", sqlConn)
    		sr = scm.ExecuteReader()
    
    		While (sr.Read())
    			count = 0 'set to 0
    
    			'Dim k As String = sr(1).ToString 'stores subid from submain corresponding sid
    			k = sr(1).ToString
    
    			Dim tr As New TableRow() 'defining tablerow
    			Dim td As New TableCell() 'defining table cell
    			Dim dd As New TableCell()  'table cell
    
    			lblsub(subid) = New Label() 'defining label 
    			lblsub(subid).ID = "lblsub-" + subid.ToString() 'giving ID to Label
    			lblsub(subid).Text = Convert.ToString(sr.GetString(0)) 'text value for label 
    			lblsub(subid).Font.Bold = True
    			lblsub(subid).Font.Size = 12
    			lblsub(subid).Font.Name = "Times New Roman"
    			lblsub(subid).ForeColor = Drawing.Color.Maroon
    			lblsub(subid).Width = 170
    			td.BorderColor = Drawing.Color.DarkCyan
    			td.BorderWidth = 2
    			'td.BackColor = Drawing.Color.Blue
    
    			td.VerticalAlign = VerticalAlign.Middle
    			td.Controls.Add(lblsub(subid)) 'place label in first row of tablecell
    			tr.Cells.Add(td) 'add td in table row
    
    			'If Trim(k.ToString) = "s1-1" Then
    
    			dd.BorderColor = Drawing.Color.DarkCyan
    			dd.BorderWidth = 2
    			dd.VerticalAlign = VerticalAlign.Middle
    			tr.Cells.Add(dd)
    
    			n = sr(2) 'saves no of colums as in submain table
    
    			kq = 0
    
    			If sqlConn2.State = ConnectionState.Closed Then
    				sqlConn2.Open()
    			End If
    
    			'display for strongly agree,agree,disagree columns
    
    			'If Trim(k.ToString) <> "S3-2" Then
    			scm1 = New SqlCommand("SELECT * FROM ANS where subid='" + Trim(k.ToString) + "' ", sqlConn2)
    
    			dr = scm1.ExecuteReader()
    			While dr.Read
    				Dim col1 As New TableCell 'table cell
    				Dim col2 As New TableCell 'table cell
    				col1.BorderColor = Drawing.Color.DarkCyan
    				col2.BorderColor = Drawing.Color.DarkCyan
    
    				col1.BorderWidth = 2
    				col2.BorderWidth = 2
    
    				col2.Text = dr(1).ToString
    				col2.Font.Bold = True
    				col2.ForeColor = Drawing.Color.Maroon
    
    				str1(kq) = dr(1).ToString
    				kq = kq + 1
    
    				tr.Controls.Add(col2) 'add col to table row
    			End While
    			kq = 0
    			sqlConn2.Close()
    
    			TABLE1.Rows.Add(tr)  'table row added to table1
    
    			If sqlconn1.State = ConnectionState.Closed Then
    				sqlconn1.Open()
    			End If
    
    			scm1 = New SqlCommand("select QUEDESCR,questid,TYPE FROM QUESTION WHERE SUBID='" + Trim(k.ToString) + "' order by questid", sqlconn1)
    			Dim dr1 As SqlDataReader
    			dr1 = scm1.ExecuteReader
    			While (dr1.Read())
    				count = count + 1
    
    				Dim tr1 As New TableRow
    				Dim td1 As New TableCell
    				Dim td2 As New TableCell
    				Dim tl As New TableCell
    				td1.BorderColor = Drawing.Color.DarkCyan
    				td2.BorderColor = Drawing.Color.DarkCyan
    
    				td1.BorderWidth = 2
    				td2.BorderWidth = 2
    
    				quest(que) = New Label()
    				quest(que).ID = "quest-" + que.ToString()
    				quest(que).Text = Convert.ToString(dr1.GetString(0))
    				quest(que).Font.Name = "Times New Roman"
    				quest(que).Font.Bold = True
    				quest(que).Font.Size = 12
    				quest(que).ForeColor = Drawing.Color.White
    
    				lq(que) = New Label()
    				lq(que).ID = "lq" + que.ToString()
    				lq(que).Text = Convert.ToString(dr1.GetString(1))
    
    				td1.Text = count
    				td2.Controls.Add(quest(que))
    				tr1.Controls.Add(td1)
    				td2.HorizontalAlign = HorizontalAlign.Left
    				tr1.Controls.Add(td2)
    
    				If dr1(2) = "Entry Type" Then
    					Dim td3 As New TableCell
    					td3.BorderWidth = 2
    					td3.BorderColor = Drawing.Color.DarkCyan
    					txtb(que) = New TextBox()
    
    					txtb(que).ID = "txtb-" + que.ToString() 'textbox creation
    					txtb(que).BorderColor = Drawing.Color.Blue
    					txtb(que).BorderStyle = BorderStyle.Solid
    					txtb(que).BorderWidth = 1
    
    					txtb(que).ForeColor = Drawing.Color.BlueViolet
    					txtb(que).Font.Bold = True
    					txtb(que).AutoPostBack = True
    					'txtb(que).AutoPostBack = False
    
    					td3.ColumnSpan = n
    					'txtb(que).Width = Unit.Percentage(100)
    					txtb(que).Width = 600
    					td3.Controls.Add(txtb(que))
    					tr1.Controls.Add(td3)
    
    					AddHandler txtb(que).TextChanged, AddressOf txtb_textchanged
    					mt = que
    				Else
    					For y = 0 To n - 1
    						Dim td3 As New TableCell
    						td3.BorderColor = Drawing.Color.DarkCyan
    						td3.BorderWidth = 2
    
    						btn(que, y) = New Button()
    						btn(que, y).ID = "btn-" + que.ToString() + "-" + y.ToString
    						btn(que, y).BorderColor = Drawing.Color.Blue
    						btn(que, y).BorderStyle = BorderStyle.Solid
    						btn(que, y).Width = Unit.Percentage(100)
    						btn(que, y).ForeColor = Drawing.Color.White
    						btn(que, y).Width = 130
    						btn(que, y).Height = 40
    						'btn(que, y).Text = str1(y).ToString
    						btn(que, y).ForeColor = Drawing.Color.BlueViolet
    						btn(que, y).Font.Bold = True
    						td3.Controls.Add(btn(que, y))
    						tr1.Controls.Add(td3)
    
    						TABLE1.Rows.Add(tr1)
    						TABLE1.DataBind()
    
    						AddHandler btn(que, y).Click, AddressOf Button_click
    
    						mnk = que
    						mnj = y
    
    						hj = str1(y).ToString
    					Next
    				End If
    				txt = txt + 1
    
    				tl.Controls.Add(lq(que))
    				tl.Visible = False
    				tr1.Controls.Add(tl)
    
    				TABLE1.Rows.Add(tr1)
    				que = que + 1
    				tr1.HorizontalAlign = HorizontalAlign.Center
    			End While
    			sqlconn1.Close()
    
    			tr.Width = Unit.Percentage(100)
    			tr.HorizontalAlign = HorizontalAlign.Center
    
    			subid = subid + 1
    
    			Dim br As New TableRow
    			Dim cell As New TableCell
    
    			cell.Height = 25
    			br.Controls.Add(cell)
    			TABLE1.Rows.Add(br)
    		End While
    		TABLE1.CellSpacing = 0
    	End If
    	sqlConn.Close()
    End Sub
    
    Private Sub txtb_textchanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    	Dim objb As [Object]
    
    	Dim cbt As TextBox
    	cbt = CType(sender, TextBox)
    	Dim xt As Integer
    	For xt = 0 To mt
    		If sender.Equals(txtb(xt)) Then
    			For Each objb In blist
    				If objb.ToString.Split(":")(0) = lq(xt).Text Then
    					blist.Remove(objb)
    					Exit For
    				End If
    			Next
    			Dim sv1 As String = lq(xt).Text
    			blist.Add(sv1 + ":" + xt.ToString + ":" + txtb(xt).Text.ToString)
    		End If
    	Next
    
    	For Each objb In blist
    		Dim i As Integer = objb.ToString.Split(":")(1)
    		For kk As Integer = 0 To mt
    			If kk = i Then
    				txtb(kk).Text = objb.ToString.Split(":")(2)
    			'Else
    			'	txtb(mt).Text = ""
    			End If
    		Next
    	Next
    End Sub
    
    Private Sub Button_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    	Dim cb As Button
    	cb = CType(sender, Button)
    	Dim obja As [Object]
    
    	Dim x As Integer = 0
    	Dim t As Integer = 0
    
    	For x = 0 To mnk  '20 questns
    		For t = 0 To mnj '4 buttons
    			's1-1-1:0:0:Strongly Agree
    			If sender.Equals(btn(x, t)) Then
    				For Each obja In alist
    					If obja.ToString.Split(":")(0) = lq(x).Text Then
    						alist.Remove(obja)
    						Exit For
    					End If
    				Next
    				Dim sv As String = str1(t).ToString
    				'MsgBox(sv)
    
    				Dim sv1 As String = lq(x).Text
    				'MsgBox(sv1)
    				alist.Add(sv1 + ":" + x.ToString + ":" + t.ToString + ":" + sv.ToString)
    				'MsgBox(alist.Add(sv1 + ":" + x.ToString + ":" + t.ToString + ":" + sv.ToString))
    			End If
    		Next
    	Next
    
    	For Each obja In alist
    		'MsgBox(obja)
    		Dim i As Integer = obja.ToString.Split(":")(2)
    		'MsgBox(i)
    
    		For kk As Integer = 0 To mnj
    			If kk = i Then
    				btn(obja.ToString.Split(":")(1), kk).Text = obja.ToString.Split(":")(3) 'displays 3 value in button text
    				'MsgBox(btn(obja.ToString.Split(":")(1), kk).Text)
    			Else
    				btn(obja.ToString.Split(":")(1), kk).Text = ""
    			'ElseIf IsDBNull(obja.ToString.Split(":")(1)) Then
    			'	btn(obja.ToString.Split(":")(1), kk).Text = ""
    			'	MsgBox(btn(obja.ToString.Split(":")(1), kk).Text)
    			End If
    		Next
    	Next
    End Sub
    please help me in this regards


    Thank you in advance


    shubha
    Last edited by sage45; 06-25-2014 at 11:40 PM. Reason: Please use code tags

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,928
    Thanks
    46
    Thanked 203 Times in 202 Posts
    Please narrow down your problem. This is just a giant code dump with little explanation. Are the buttons' text really changing as you type in the text box? If that's so it is being done client side via javascript or jquery or ajax or "something". The server cannot change the buttons' text until it posts back. Check and make sure that none of your controls are causing auto-postback.

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •