problems in creating dynamic controls
Sir,i have created two dynamic controls and bind it to the grid .but the problem is i can't add these controls to the grid and after adding how could i get these controls in the next row of the grid and also i couldn't tke values from the control. hre i s code thsat ihve written.
CreateTable()
Dim ucxct As New ControlDyna
If Not IsPostBack Then
FillGrid()
bindTable()
Else
dtTemp = CType(Session("DYNA"), DataTable)
End If
'Put user code to initialize the page here
End Sub
Private Sub CreateTable()
If Session("DYNA") Is Nothing Then
dtTemp = New DataTable
dtTemp.Columns.Add("sct", GetType(Control))
dtTemp.Columns.Add("scr", GetType(Control))
' dtTemp.Columns.Add("abc", GetType(Control))
Session("DYNA") = dtTemp
Else
dtTemp = CType(Session("DYNA"), DataTable)
End If
End Sub
Private Sub bindTable()
DataGrid2.DataSource = dtTemp
DataGrid2.DataBind()
End Sub
Private Sub FillGrid()
Dim sql As String
Dim DT1 As New DataTable
Dim obj As New clsValidation
sql = "SELECT Firstcol,Secondcol from Dynamic where code='1' "
DT1 = obj.ReturnDataTable(sql, strconnect, erpath)
Dim i As Integer
For i = 0 To DT1.Rows.Count
If Trim(DT1.Rows(0).Item(i)) = "TextBox" Then
createTextBox()
ElseIf Trim(DT1.Rows(0).Item(i)) = "CheckBox" Then
createchkbox()
ElseIf Trim(DT1.Rows(0).Item(i)) = "ListBox" Then
createListBox()
ElseIf Trim(DT1.Rows(0).Item(i)) = "RadioButton" Then
createRadBox()
ElseIf Trim(DT1.Rows(0).Item(i)) = "DropDown" Then
createDropDown()
ElseIf Trim(DT1.Rows(0).Item(i)) = "LinkButton" Then
createLinkButton()
End If
Next
bindTable()
End Sub
Private Sub createLinkButton()
Dim lnk As New System.Web.UI.WebControls.LinkButton
lnk.ID = "Lnkadd"
lnk.Text = "Add"
lnk.Visible = True
display(lnk)
End Sub
Private Sub createDropDown()
Dim comp As New System.Web.UI.WebControls.DropDownList
comp.Visible = True
display(comp)
End Sub
Private Sub createRadBox()
Dim rb As New System.Web.UI.WebControls.RadioButton
rb.Visible = True
display(rb)
End Sub
Private Sub createListBox()
Dim lst As New System.Web.UI.WebControls.ListBox
lst.Visible = True
display(lst)
End Sub
Private Sub createchkbox()
Dim chk As New System.Web.UI.WebControls.CheckBox
chk.Visible = True
display(chk)
End Sub
Private Sub createTextBox()
Dim ctrl As Control
Dim phold As PlaceHolder
Dim i As Integer = 0
Dim z As Object
Dim x As New ColorConverter
Dim y As Color
z = x.ConvertFromString("#FFFFFF")
y = CType(z, Color)
Dim tb As New System.Web.UI.WebControls.TextBox
Dim TextBoxesHere As System.Web.UI.WebControls.PlaceHolder
' z = x.ConvertFromString("#dcdcdc")
'y = CType(z, Color)
tb.Visible = True
tb.BorderStyle = BorderStyle.Double
tb.BackColor = y
tb.ID = "TextBoxID" + i.ToString()
i = i + 1
' PlaceHolder1.Controls.Add(tb)
display(tb)
End Sub
Private Sub display(ByVal ctrl As Control)
Dim dr As DataRow
dr = dtTemp.NewRow
dr(0) = CType(ctrl, Control)
' dr(1) = CType(cTyp, String)
dtTemp.Rows.Add(dr)
End Sub
'
Private Sub DataGrid2_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles DataGrid2.ItemCommand
If TypeOf e.CommandSource Is LinkButton Then
' bindTable()
Dim lnkTemp As New LinkButton
Dim btn As Button
Dim txtTemp As LiteralControl
Dim ctrl As Control
lnkTemp = CType(e.CommandSource, LinkButton)
Select Case Trim(lnkTemp.Text)
Case Is = "Add"
Dim drs As DataRow
drs = dtTemp.NewRow
' txtTemp = CType(e.Item.Cells(2).Controls(0), LiteralControl)
ctrl = CType(e.Item.Cells(2).Controls(0), Control)
'ctrl = DataGrid2.Controls(0).Controls(0)
' txtTemp = CType(ctrl.FindControl("TextBoxID0"), TextBox)
''drs(0) = Trim(txtTemp.Text)
'txtTemp = CType(e.Item.Cells(2).Controls(1), TextBox)
' drs(1) = Trim(txtTemp.Text)
drs(0) = ctrl
' ctrl = CType(e.Item.Cells(1).Controls(2), Control)
' drs(1) = ctrl
dtTemp.Rows.Add(drs)
bindTable()
End Select
End If
End Sub
Private Sub DataGrid2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid2.ItemDataBound
Dim i As Integer
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.Footer Then
If dtTemp.Rows.Count > 0 Then
For i = 0 To dtTemp.Rows.Count - 1
e.Item.Cells(i).Controls.Add(CType(dtTemp.Rows(i). Item(0), Control))
Next
End If
End If
End Sub
|