G
Guest
I've been at this all day and it's really starting to annoy me now. I have a
form, this has a textbox which the user enters a search parameter into. This
is passed to a Stored Procedure as a parameter, the results returned are then
bound to a GridView control. This GridView control has an extra column with a
checkbox in it for every row. The idea for this being that the user selects
which items he/she would like to use by checking the checkboxes.
Once they have made their selection, they then click a button and this
copies their selections over to an ASP:Table control. The table control is
declaratively created with each table row being dynamically created.
All of the above works fine using the code included in this post. The
problems begin if they then start another search, they want to keep the
values in the table but the table is cleared and populated with the new
values when they add the results of their new search to it.
STEP: User performs a search.
RESULT: GridView populated with results of search
STEP: User selects items they want to use from the GridView and then click
on the Add To List button.
RESULT: Table control has x amount of rows added containing the details as
they appear in the GridView.
STEP: User then selects more items from the same search.
RESULT: The items selected are appended to the table.
STEP: User performs another search and selects the items from those search
results.
RESULT: Table only shows the items that the user has just selected, all the
previous items are lost
I've taken a screenshot of the test form to help you visualize it.
(http://mparter.pwp.blueyonder.co.uk/dyntable.png)
Please save me before I go mad
Thanks.
------------------------------------------CODE------------------------------------------------
Protected Sub AddToList()
Dim gridRow As GridViewRow
For Each gridRow In GridView1.Rows
Dim gridCell0 As TableCell = gridRow.Cells(0)
Dim gridCell1 As TableCell = gridRow.Cells(1)
Dim gridCell2 As TableCell = gridRow.Cells(2)
Dim gridCell3 As TableCell = gridRow.Cells(3)
Dim gridCell4 As TableCell = gridRow.Cells(4)
Dim chkStudent As HtmlInputCheckBox =
CType(gridCell0.Controls(1), HtmlInputCheckBox)
If Not (chkStudent Is Nothing) And chkStudent.Checked Then
Dim tblRow As New TableRow
Dim tblCell0 As New TableCell
Dim tblCell1 As New TableCell
Dim tblCell2 As New TableCell
Dim tblCell3 As New TableCell
Dim tblCell4 As New TableCell
Dim chkSelectedStudent As New HtmlInputCheckBox
tblCell0.Controls.Add(chkSelectedStudent)
chkSelectedStudent.ID = "chkSelectedStudent_" & gridCell1.Text
chkSelectedStudent.Value = gridCell1.Text
tblRow.Cells.Add(tblCell0)
tblCell1.Controls.Add(New LiteralControl(gridCell1.Text))
tblRow.Cells.Add(tblCell1)
tblCell2.Controls.Add(New LiteralControl(gridCell2.Text))
tblRow.Cells.Add(tblCell2)
tblCell3.Controls.Add(New LiteralControl(gridCell3.Text))
tblRow.Cells.Add(tblCell3)
tblCell4.Controls.Add(New LiteralControl(gridCell4.Text))
tblRow.Cells.Add(tblCell4)
Table1.Rows.Add(tblRow)
End If
Next
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Label1.Text = DateTime.Now.ToString
If Page.IsPostBack Then
AddToList()
End If
End Sub
form, this has a textbox which the user enters a search parameter into. This
is passed to a Stored Procedure as a parameter, the results returned are then
bound to a GridView control. This GridView control has an extra column with a
checkbox in it for every row. The idea for this being that the user selects
which items he/she would like to use by checking the checkboxes.
Once they have made their selection, they then click a button and this
copies their selections over to an ASP:Table control. The table control is
declaratively created with each table row being dynamically created.
All of the above works fine using the code included in this post. The
problems begin if they then start another search, they want to keep the
values in the table but the table is cleared and populated with the new
values when they add the results of their new search to it.
STEP: User performs a search.
RESULT: GridView populated with results of search
STEP: User selects items they want to use from the GridView and then click
on the Add To List button.
RESULT: Table control has x amount of rows added containing the details as
they appear in the GridView.
STEP: User then selects more items from the same search.
RESULT: The items selected are appended to the table.
STEP: User performs another search and selects the items from those search
results.
RESULT: Table only shows the items that the user has just selected, all the
previous items are lost
I've taken a screenshot of the test form to help you visualize it.
(http://mparter.pwp.blueyonder.co.uk/dyntable.png)
Please save me before I go mad
Thanks.
------------------------------------------CODE------------------------------------------------
Protected Sub AddToList()
Dim gridRow As GridViewRow
For Each gridRow In GridView1.Rows
Dim gridCell0 As TableCell = gridRow.Cells(0)
Dim gridCell1 As TableCell = gridRow.Cells(1)
Dim gridCell2 As TableCell = gridRow.Cells(2)
Dim gridCell3 As TableCell = gridRow.Cells(3)
Dim gridCell4 As TableCell = gridRow.Cells(4)
Dim chkStudent As HtmlInputCheckBox =
CType(gridCell0.Controls(1), HtmlInputCheckBox)
If Not (chkStudent Is Nothing) And chkStudent.Checked Then
Dim tblRow As New TableRow
Dim tblCell0 As New TableCell
Dim tblCell1 As New TableCell
Dim tblCell2 As New TableCell
Dim tblCell3 As New TableCell
Dim tblCell4 As New TableCell
Dim chkSelectedStudent As New HtmlInputCheckBox
tblCell0.Controls.Add(chkSelectedStudent)
chkSelectedStudent.ID = "chkSelectedStudent_" & gridCell1.Text
chkSelectedStudent.Value = gridCell1.Text
tblRow.Cells.Add(tblCell0)
tblCell1.Controls.Add(New LiteralControl(gridCell1.Text))
tblRow.Cells.Add(tblCell1)
tblCell2.Controls.Add(New LiteralControl(gridCell2.Text))
tblRow.Cells.Add(tblCell2)
tblCell3.Controls.Add(New LiteralControl(gridCell3.Text))
tblRow.Cells.Add(tblCell3)
tblCell4.Controls.Add(New LiteralControl(gridCell4.Text))
tblRow.Cells.Add(tblCell4)
Table1.Rows.Add(tblRow)
End If
Next
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Label1.Text = DateTime.Now.ToString
If Page.IsPostBack Then
AddToList()
End If
End Sub