Searching in a multipage gridview in an updatepanel is extremelydifficult (for me) !

R

Radu

Hi. I have tried since yesterday morning to make this search work. I
have some javascript code which works great. Problem is, it only works
on the current page. So then I decided to write code to search in the
whole grid. This is painful !

1. To minimize flashing, the "search, filter" table (outside the
gridview, of course) is inside an updatepanel. The grid is inside
another updatepanel.

Since the grid might have been sorted and resorted, I cannot search in
the database - I do NOT know where in my grid the record belonging to
PIN 666 might be. I HAVE to search in the grid directly.

PinLabel is the control where I'm holding the PIN#
<ItemTemplate>
<asp:Label ID="PINLabel" Runat="Server"><%#Eval("PIN")
%></asp:Label>
</ItemTemplate>

So then... I have tried to write code in the RowDataBound event, like
if ctype(e.row.FindControl("PinLabel"),label).text=....
but ctype(e.row.FindControl("PinLabel"),label).text always evaluates
to ""

I have tried to do something similar in a standalone function on the
cmdSearch_Click event. Again, no go.

I have tried other places and other ways to refer to that label...

In the end, I wrote the following code in GridView_Data_PreRender:
____________________________________________________________________________
If Session("PinToSearch").ToString <> vbNullString Then
'Select all matching rows:
Dim intFirstSelectedRow As Integer
For intRecordCounter As Integer = 0 To CType
(SqlDataSource_Data.Select
(System.Web.UI.DataSourceSelectArguments.Empty),
System.Data.DataView).Count - 1
Dim strExistingValue As String = CType
(SqlDataSource_Data.Select
(System.Web.UI.DataSourceSelectArguments.Empty),
System.Data.DataView).Item(intRecordCounter).Item("PIN").ToString()

'Get the page the row is on:
If (intRecordCounter \ GridView_Data.PageSize) <>
GridView_Data.PageIndex Then
GridView_Data.PageIndex = intRecordCounter \
GridView_Data.PageSize
End If

If strExistingValue = Session
("PinToSearch").ToString Then
GridView_Data.Rows(intRecordCounter Mod
GridView_Data.PageSize).RowState = DataControlRowState.Selected
If intFirstSelectedRow = 0 Then
intFirstSelectedRow = intRecordCounter
Session("PinsFound") = CInt(Session
("PinsFound").ToString) + 1
Else
GridView_Data.Rows(intRecordCounter Mod
GridView_Data.PageSize).RowState = DataControlRowState.Normal
End If
Next intRecordCounter

If CInt(Session("PinsFound").ToString) > 0 Then
Dim strMessage As String = Session
("PinsFound").ToString + " records found for PIN \'" + Session
("PinToSearch").ToString + "\'"
ScriptManager.RegisterStartupScript(Page,
Me.GetType(), "RecordsFound", "alert('" + strMessage + "');", True)
'Reset:
Session("PinToSearch") = vbNullString
txtSearchAllPins.Value = vbNullString
Session("PinsFound") = 0
'Get the page the first selected row is on:
GridView_Data.PageIndex = intFirstSelectedRow \
GridView_Data.PageSize
Else
Dim strMessage As String = "No records found for
PIN \'" + Session("PinToSearch").ToString + "\'"
ScriptManager.RegisterStartupScript(Page,
Me.GetType(), "RecordsFound", "alert('" + strMessage + "');", True)
Session("PinToSearch") = vbNullString
txtSearchAllPins.Value = vbNullString
GridView_Data.PageIndex = 0
End If
End If
____________________________________________________________________________

This works great. It finds my records and hilites them. It also tells
me "2 records found for PIN 666" and positions itself appropriately.
However, on changing pages, the selection gets lost (setting the
status to "Selected" is only a graphical thing - it is not the same as
setting SelectedIndex, and I cannot have multiple selection with this
griview (saying SelectedIndex={1, 4, 6, 8, 19}, for instance).

So I though to add some code in the RowDataBound event, like this:

If Session("PinToSearch").ToString <> vbNullString Then
If e.Row.RowType = DataControlRowType.DataRow Then
If String.Compare(CType(e.Row.DataItem,
System.Data.DataRowView).Item("PIN").ToString, Session
("PinToSearch").ToString) = 0 Then
e.Row.RowState = DataControlRowState.Selected
End If
End If
End If

This works fine... except that when I try to change the page in the
gridview (clicking on the paging numbers to go, for instance, on page
2 of the grid) I get the message:

"Invalid postback or callback argument. Event validation is enabled
using <pages enableEventValidation="true"/> in
configuration................ If the data is valid end expected, use
the ClientScriptManager.RegisterForEventValidation method in order to
register the postback or callback data for validation."

Which postback ? There is not postback here other than requiring a new
gridview page. How does this interfere with my testing
If String.Compare(CType(e........").ToString) = 0
Then
above ?

So this does not work. I don't understand why. Is there any other
method for searching in a grid, selecting the records found, and
making those selections stick, when gridview is inside updatepanels (I
assume that if I take Ajax out, this would work).

Thank you very much.
Alex
 
C

Cowboy \(Gregory A. Beamer\)

In normal paging, the only records client side, at any time, are the ones
showing. You then will have the entire dataset stored somewhere. If you are
using default paging, ala declarative in ASP.NET, the only records on the
server side are the ones you are showing - as well. You then take a trip
back to get the next page.

In AJAX, you are still doing the same thing (taking the trip back), but the
trip is hidden from the user as it is only updating the controls in the
UpdatePanel. Everything else stays.

From what I read, it sounds like you have created JavaScript to search the
grid, meaning client side JavaScript. If so, then you are not making the
trip back to the server in your search code (ie, using AJAX).

NOTE: Even if AJAX is used, you will have to search the database UNLESS you
store the DataSet server side where it is accessible for AJAX. But, then,
you cannot use the default drag and drop bits, as you are custom paging at
that point, even if you are glomming off the MS bits. The only other option
is to pull everything client side, which is unwise if you are talking 100
pages worth of data.

BTW, sorting and paging, at the same time, is a bane for all of us. Filter
and page is not as hard, as long as you either save the DataSet or save the
filtered SQL query.

Have fun!
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top