TableRows and Postback


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
 
Ad

Advertisements

J

Joseph Byrns

I think you need to create a session variable to contain the selected rows
(i.e. Session("AddedItemsTable")=AddedItemsTable, that way previous
selections will be remembered between postbacks.
 
Ad

Advertisements

W

William F. Robertson, Jr.

The changes you are making on the client on not known by the server. Since
<tr> and <td> tags are not postable, their new values never make it back to
the server for processing.

One workaround is to use a <input type="hidden"> element on the page as this
is postable. When you are client side adding rows to your table, you should
alter the value of your hidden field. On postback, you parse the hidden
field and on the server, create the rows and columns you need. You will
need two delimiters, one for each column, and one for a new row:

"col1.1^col1.2^col1.3~col1.2^col2.2^col2.3"

<asp:Table id="tblAddons" runat="server" />

Page_Load
{
string tableValue = String.Empty;
if ( Page.IsPostBack )
{
tableValue = Request.Forms["tblAddons_Value"];
RebuildHtmlTable( tableValue );
}

//always register this hidden field with the previous value.
Page.RegisterHiddenField( "tblAddons_Text", tableValue );
}

//in this method you will create the TableRow and TableCell items based on
the string passed to it.
RebuildHtmlTable( string instructions )

HTH,

bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top