If you want to make dynamic row in asp.net, you must remember that every page post back you must draw it again and put the existing value for each control.Assume that you already have a dropdown, two textbox ,a button to insert the new row.
Private Sub InsertExistRow()
Dim row As HtmlTableRow
Dim cell As HtmlTableCell
Dim i As Integer
Dim btn As Button
Dim x() As String
x = Session("x")
For i = 1 To x.Length - 1
row = New HtmlTableRow
row.ID = "row" & x(i)
cell = New HtmlTableCell
Dim dropdown1 As New DropDownList
dropdown1.ID = "dropdown" & x(i)
dropdown1.Items.Add("0001")
dropdown1.Items.Add("0002")
dropdown1.Items.Add("0003")
dropdown1.Items.Add("0004")
dropdown1.Items.Add("0005")
dropdown1.Items.Add("0006")
dropdown1.Items.FindByText(Request.Params.Get("dropdown" & x(i)))
cell.Controls.Add(dropdown1)
row.Controls.Add(cell)
cell = New HtmlTableCell
Dim txt As New TextBox
txt.ID = "txt" & x(i)
txt.Text = Request.Params.Get("txt" & x(i))
cell.Controls.Add(txt)
row.Controls.Add(cell)
cell = New HtmlTableCell
Dim dropdown2 As New DropDownList
dropdown2.ID = "dropdown2" & x(i)
dropdown2.Items.Add("A100")
dropdown2.Items.Add("A200")
dropdown2.Items.Add("A300")
dropdown2.Items.Add("A400")
dropdown2.Items.Add("A500")
dropdown2.Items.Add("A600")
dropdown2.Items.FindByText(Request.Params.Get("dropdown2" & x(i)))
cell.Controls.Add(dropdown2)
row.Controls.Add(cell)
cell = New HtmlTableCell
btn = New Button
btn.Text = "Insert"
btn.ID = "Insert" & x(i)
btn.CommandName = "group"
btn.CommandArgument = "Insert" & x(i)
AddHandler btn.Command, AddressOf btn_click
cell.Controls.Add(btn)
row.Controls.Add(cell)
cell = New HtmlTableCell
btn = New Button
btn.Text = "Copy"
btn.ID = "Copy" & x(i)
btn.CommandName = "group"
btn.CommandArgument = "Copy" & x(i)
AddHandler btn.Command, AddressOf btn_click
cell.Controls.Add(btn)
row.Controls.Add(cell)
cell = New HtmlTableCell
btn = New Button
btn.Text = "Paste"
btn.ID = "Paste" & x(i)
btn.CommandName = "group"
btn.CommandArgument = "Paste" & x(i)
AddHandler btn.Command, AddressOf btn_click
cell.Controls.Add(btn)
row.Controls.Add(cell)
cell = New HtmlTableCell
btn = New Button
btn.Text = "Delete"
btn.ID = "Delete" & x(i)
btn.CommandName = "group"
btn.CommandArgument = "Delete" & x(i)
AddHandler btn.Command, AddressOf btn_click
cell.Controls.Add(btn)
row.Controls.Add(cell)
tblBasic.Rows.Add(row)
Next
End Sub
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
If Page.IsPostBack = True Then
If Session("i") > 0 Then
InsertExistRow()
End If
Else
Session("i") = 0
End If
End Sub
tblBasic is ID of table control that run at server. Code for button to insert new row :
Private Sub InsertRow(ByVal index As Integer)
Dim row As HtmlTableRow
Dim cell As HtmlTableCell
Dim btn As Button
Session("i") = Session("i") + 1
row = New HtmlTableRow
row.ID = "row" & Session("i")
cell = New HtmlTableCell
Dim dropdown1 As New DropDownList
dropdown1.ID = "dropdown" & Session("i")
dropdown1.Items.Add("0001")
dropdown1.Items.Add("0002")
dropdown1.Items.Add("0003")
dropdown1.Items.Add("0004")
dropdown1.Items.Add("0005")
dropdown1.Items.Add("0006")
cell.Controls.Add(dropdown1)
row.Controls.Add(cell)
cell = New HtmlTableCell
Dim txt As New TextBox
txt.ID = "txt" & Session("i")
cell.Controls.Add(txt)
row.Controls.Add(cell)
cell = New HtmlTableCell
Dim dropdown2 As New DropDownList
dropdown2.ID = "dropdown2" & Session("i")
dropdown2.Items.Add("A100")
dropdown2.Items.Add("A200")
dropdown2.Items.Add("A300")
dropdown2.Items.Add("A400")
dropdown2.Items.Add("A500")
dropdown2.Items.Add("A600")
cell.Controls.Add(dropdown2)
row.Controls.Add(cell)
cell = New HtmlTableCell
btn = New Button
btn.ID = "Insert" & Session("i")
btn.Text = "Insert"
btn.CommandName = "group"
btn.CommandArgument = "Insert" & Session("i")
AddHandler btn.Command, AddressOf btn_click
cell.Controls.Add(btn)
row.Controls.Add(cell)
cell = New HtmlTableCell
btn = New Button
btn.ID = "Copy" & Session("i")
btn.Text = "Copy"
btn.CommandName = "group"
btn.CommandArgument = "Copy" & Session("i")
AddHandler btn.Command, AddressOf btn_click
cell.Controls.Add(btn)
row.Controls.Add(cell)
cell = New HtmlTableCell
btn = New Button
btn.ID = "Paste" & Session("i")
btn.Text = "Paste"
btn.CommandName = "group"
btn.CommandArgument = "Paste" & Session("i")
AddHandler btn.Command, AddressOf btn_click
cell.Controls.Add(btn)
row.Controls.Add(cell)
cell = New HtmlTableCell
btn = New Button
btn.ID = "Delete" & Session("i")
btn.Text = "Delete"
btn.CommandName = "group"
btn.CommandArgument = "Delete" & Session("i")
AddHandler btn.Command, AddressOf btn_click
cell.Controls.Add(btn)
row.Controls.Add(cell)
tblBasic.Rows.Insert(index, row)
Dim s() As String
ReDim s(tblBasic.Rows.Count - 1)
Dim a As HtmlTableRow
Dim x As Integer
x = 0
For Each a In tblBasic.Rows
s(x) = Mid(a.ID, 4, Len(a.ID) - 3)
x = x + 1
Next
Session("x") = s
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Insert0.Click
InsertRow(1)
End Sub
Any question, please send email to : wokee_cs@yahoo.com
0 Comments:
Post a Comment