Page_Load question in ASPX

  • Thread starter Thread starter fniles
  • Start date Start date
F

fniles

When I load my ASP.Net page myPage.ASPX the first time, I would like to read
from table myTbl from myDB.mdb, and shows everything there.
In the same page I have a drop down list (cboSearch), text box (txtSearch)
and a button (cmdSearch).
The drop down list is populated with the columns from myTbl. Say 1 of the
column is "company"
Say I select "company" from the drop down list cboSearch, and I enter "IBM"
in the text box txtSearch.
When I click the button cmdSearch, I want to show the myPage.ASPX with only
those records that meet the criteria "company" = "IBM".

So, in my Page_Load of myPage.ASPX, I call sub GetInfo and do a query
"select * from myTbl".
When I click on the button cmdSearch I call sub GetInfo and do a query that
meets the criteria.
If in Page_Load I call sub GetInfo and do a query "select * from myTbl",
every time I do a search, i will show everything from myTbl, plus the one
that meets the criteria, because when
I click on the button cmdSearch, it calls Page_Load again.
How can I show everything when I load the page, but only show the result the
meet the criteria when I click on the button cmdSearch ?

Thank you.

Partial Class WebForm1
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim sSQL As String

g_dbPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\myDB.mdb"
sSQL = "select * from myTbl"
GetInfo(sSQL)
End Sub

Sub GetInfo(ByVal sSQL As String)
'declaration of variables here

AddHeaderCell(tempRow, "Number", 0, 0, Color.LightBlue, 100)
tblResources.Rows.Add(tempRow)
'I read database here using the parameter sSQL
For Each aRow In rs.Rows
Dim Row As New TableRow
AddOtherCell(Row, rs.Fields("col1", lRow), 0, 0,
Color.White, 1000, False)
tblResources.Rows.Add(Row)
lRow = lRow + 1
Next
End Sub

Protected Sub cmdSearch_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdSearch.Click
Dim sSQL As String

If Trim(txtSearch.Text) <> "" Then
Select Case cboSearch.SelectedIndex
Case 0
sSQL = "select * from myTbl where col2 LIKE '%" &
txtSearch.Text & "%'"
Case 1
sSQL = "select * from myTbl where col3 LIKE '%" &
txtSearch.Text & "%'"
Case 2
sSQL = "select * from myTbl where col4 LIKE '%" &
txtSearch.Text & "%'"
End Select
Else
sSQL = "select * from myTbl"
End If
GetInfo(sSQL)
End Sub
End Class
 
re:
!> How can I show everything when I load the page, but only show the
!> results that meet the criteria when I click on the button cmdSearch ?

Add an "If not isPostback" clause to your Page_Load routine.

Here's sample code :
http://books.google.com/books?id=_t...oHfDQ&sa=X&oi=book_result&resnum=10&ct=result

Basically :

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not IsPostBack Then

' Code here that needs to be executed only on first page_load

End If
End Sub




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
=========================
 
Thank you !
That did it !


Juan T. Llibre said:
re:
!> How can I show everything when I load the page, but only show the
!> results that meet the criteria when I click on the button cmdSearch ?

Add an "If not isPostback" clause to your Page_Load routine.

Here's sample code :
http://books.google.com/books?id=_t...oHfDQ&sa=X&oi=book_result&resnum=10&ct=result

Basically :

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

If Not IsPostBack Then

' Code here that needs to be executed only on first page_load

End If
End Sub




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
=========================
 
Back
Top