Page_Load question in ASPX

Discussion in 'ASP .Net' started by fniles, Mar 1, 2009.

  1. fniles

    fniles Guest

    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
    fniles, Mar 1, 2009
    #1
    1. Advertising

  2. 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/
    =========================
    "fniles" <> wrote in message news:...
    > 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
    >
    >
    Juan T. Llibre, Mar 1, 2009
    #2
    1. Advertising

  3. fniles

    fniles Guest

    Thank you !
    That did it !


    "Juan T. Llibre" <> wrote in message
    news:...
    > 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/
    > =========================
    > "fniles" <> wrote in message
    > news:...
    >> 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
    >>
    >>

    >
    >
    fniles, Mar 1, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stevie_mac
    Replies:
    3
    Views:
    2,958
    Stevie_mac
    Aug 12, 2003
  2. bminder
    Replies:
    1
    Views:
    2,151
    nfedin
    Feb 23, 2004
  3. z. f.
    Replies:
    0
    Views:
    549
    z. f.
    Oct 19, 2004
  4. John M
    Replies:
    1
    Views:
    1,111
    Kumar Reddi
    May 29, 2005
  5. rockdale
    Replies:
    1
    Views:
    647
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Nov 16, 2006
Loading...

Share This Page