populating a gridview with a button's click event

0 votes
asked Dec 7, 2009 by david-padi

I have a page that has two dropdownlists(one for the locations, and the other for departments), an employee search textbox and a button. On the other page, I have a gridview. Now, what I want to achieve is that when a user types an employee's name in the textbox control, selects a location from the location dropdownlist, and a department from the departments dropdownlist, and click the button(search), the gridview on the other page must show the required information of a SINGLE employee. Only one row must show.

I have created a database for the employees. I know how to do this with the autopostback but i have not tried it using a button's click. NB: the gridview should show only one row of a selected employee. I'm using ASP.NET VB

Your help will high appreciated.

2 Answers

0 votes
answered Dec 7, 2009 by chendur-pandian

Try this

<asp:Button ID="srchButton" Text="BindData" runat="server" OnClick="BindData" />

    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>

protected void BindData(object sender, EventArgs e)
{
    Gridview1.Datasource = YourDataSource;
    GridView1.DataBind()
}
0 votes
answered Dec 8, 2009 by philpursglove

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" />
<br />
<asp:DropDownList runat="server" ID="LocationDropDownList">
    <asp:ListItem Text="Springfield" Value="Springfield" />
    <asp:ListItem Text="Shelbyville" Value="Shelbyville" />
</asp:DropDownList>
<br />
<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:DropDownList>
<br />
<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 property. 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
        Response.Redirect("search.aspx")
    Else
        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)

        End If
    End If

End Sub

Private Sub bindData(ByVal employeeName As String, ByVal locationName As String, ByVal departmentName As String)

    With searchResultsGridView
        .DataSource = 'Some code that passes the search parameters to the database
        .DataBind()
    End With

End Sub

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!

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...