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
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