OK, cross-page postbacks in ASP.NET. Here we go.
Start with your search page, which we'll call search.aspx - this has your dropdownlists, textbox and button.
Employee Name: <asp:TextBox runat="server" ID="SearchTextBox" />
<asp:DropDownList runat="server" ID="LocationDropDownList">
<asp:ListItem Text="Springfield" Value="Springfield" />
<asp:ListItem Text="Shelbyville" Value="Shelbyville" />
<asp:DropDownList runat="server" ID="DepartmentDropDownList">
<asp:ListItem Text="Nuclear Power" Value="Power" />
<asp:ListItem Text="Dr. Frink's Lab" Value="Research" />
<asp:ListItem Text="Mr. Burn's Office" Value="Management" />
<asp:Button runat="server" ID="SearchButton" Text="Search" PostBackUrl="~/SearchResults.aspx" />
Note that the button has a PostBackUrl attribute - this is what posts the request off to the results page. We also need to change the search.aspx.designer.vb so that the dropdownlists and textbox are public properties, not protected.
Public WithEvents SearchTextBox As Global.System.Web.UI.WebControls.TextBox
The results page, which will be searchresults.aspx, has the GridView on it.
<asp:gridview runat="server" id="SearchResultsGridView" />
Now, how to handle the cross-page postback in the code. In the
Page_Load event for searchresults.aspx, we check the
PreviousPage could be
Nothing (if, say, the user typed in searchresults.aspx directly), so if it is we'll redirect back to search.aspx. If
PreviousPage is something, then we can check the
IsCrossPagePostback property. If this is True, then we've probably got here from our search.aspx page (this may not be a completely valid assumption, but it's good enough for right now). If this is the case, then we can cast PreviousPage to the underlying class of search.aspx, and since we made the dropdownlist and textbox controls public, we can then access them as properties in our code here.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim employeeName As String
Dim department As String
Dim location As String
Dim searchPage As Search
If PreviousPage Is Nothing Then
If PreviousPage.IsCrossPagePostBack Then
searchPage = DirectCast(PreviousPage, Search)
employeeName = searchPage.SearchTextBox.Text
department = searchPage.DepartmentDropDownList.SelectedValue
location = searchPage.LocationDropDownList.SelectedValue
Call bindData(employeeName, department, location)
Private Sub bindData(ByVal employeeName As String, ByVal locationName As String, ByVal departmentName As String)
.DataSource = 'Some code that passes the search parameters to the database
As for your requirement that the search results should only show a single row, consider whether or not it is possible to have two employees with the same name, in the same department, in the same location. It might be unlikely, but I don't think it's impossible and I'm not sure you should have a restriction that you shouldn't show it. If this was, say, a payroll system, you could end up with a record you'd never be able to get to through the UI, so you'd never be able to stop paying that particular employee - probably not what you'd want!