Solved

ASP.NET VB GridView search display on alternate page in Sharepoint 3

Posted on 2010-11-09
3
957 Views
Last Modified: 2012-05-10
Currently, I have a simple GridView search setup on a page within my Sharepoint site. It's very basic (code is attached).

What I need to do is have a small web part on the home page with a small search field and a search button that displays the results on the existing search page. I'm not sure what the easiest way to accomplish this is since my current search does not use query strings.

I'm up for any recommendations. Thanks.
<%@ Page masterpagefile="~masterurl/default.master" language="vb" AutoEventWireup="false" title="|" inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" meta:progid="SharePoint.WebPartPage.Document" %>

<asp:Content id="Content1" runat="server" contentplaceholderid="PlaceHolderAdditionalPageHead">

	<link rel="stylesheet" type="text/css" href="../../layout.css">

	<script runat="server">



    Dim SearchString As String = ""



    Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As  _

            System.EventArgs)

        ' Simple clean up text to return the Gridview to it's default state

        txtSearch.Text = ""

        SearchString = ""

        Gridview1.DataBind()

    End Sub



    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As  _

            System.EventArgs)

        ' Set the value of the SearchString so it gets 

        SearchString = txtSearch.Text

    End Sub

    

 </script>

</asp:Content>

	

<asp:Content ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">

	SERVER IT/AT - COMPANY Projects

</asp:Content>



<asp:Content id="Content2" runat="server" contentplaceholderid="PlaceHolderMain">

	<asp:TextBox ID="txtSearch" runat="server"/>

    <asp:Button ID="btnSearch" OnClick="btnSearch_Click" runat="server" Text="Search"/>

	<asp:Button ID="btnClear" OnClick="btnClear_Click" runat="server" Text="Clear"/><br /><br />

	<asp:GridView runat="server" id="GridView1" AutoGenerateColumns="False" DataSourceID="VWvisProjects" width="100%" BorderWidth="0px" GridLines="None" AllowPaging="True" PageSize="200">

								<Columns>

												

												<asp:boundfield DataField="projectno" HeaderText="Project Number" ReadOnly="True" SortExpression="projectno">

												</asp:boundfield>

												<asp:boundfield DataField="fullnumber" HeaderText="Full Number" ReadOnly="True" SortExpression="fullnumber">

												</asp:boundfield>

												<asp:boundfield DataField="Name" HeaderText="Name" SortExpression="Name">

												</asp:boundfield>

												<asp:boundfield DataField="PrincipalName" HeaderText="Principal Name" ReadOnly="True" SortExpression="PrincipalName">

												</asp:boundfield>

												<asp:boundfield DataField="ProjManagerName" HeaderText="Project Manager Name" ReadOnly="True" SortExpression="ProjManagerName">

												</asp:boundfield>

								</Columns>

								<FooterStyle BackColor="Tan" />

								<HeaderStyle CssClass="ms-vh" />

        <RowStyle CssClass="ms-vb" />

        <AlternatingRowStyle CssClass="ms-vb ms-alternating" />

        <PagerStyle  CssClass="pagerstyle"/>

				</asp:GridView>

				<asp:SqlDataSource runat="server" ID="VWvisProjects" ProviderName="System.Data.SqlClient" ConnectionString="Data Source=SERVER;Initial Catalog=COMPANY;User ID=sa;Password=password" SelectCommand="SELECT [projectno], [fullnumber], [Name], [PrincipalName], [ProjManagerName] FROM [VWvisProjects] ORDER BY [projectno], [fullnumber]"

				    FilterExpression="projectno like '%{0}%' or name like '%{1}%' or fullnumber like '%{2}%' or ProjManagerName like '%{3}%' or PrincipalName like '%{4}%'">

    <FilterParameters>

        <asp:ControlParameter Name="projectno" ControlID="txtSearch" PropertyName="Text" />

        <asp:ControlParameter Name="name" ControlID="txtSearch" PropertyName="Text" />

        <asp:ControlParameter Name="fullnumber" ControlID="txtSearch" PropertyName="Text" />

        <asp:ControlParameter Name="ProjManagerName" ControlID="txtSearch" PropertyName="Text" />

        <asp:ControlParameter Name="PrincipalName" ControlID="txtSearch" PropertyName="Text" />

    </FilterParameters>



				</asp:SqlDataSource>

</asp:Content>

<asp:Content id="Content3" runat="server" contentplaceholderid="PlaceHolderPageTitleInTitleArea">



		  COMPANY Projects</asp:Content>

Open in new window

0
Comment
Question by:renazonse
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
carlnorrbom earned 500 total points
ID: 34100460
Hi,

I change your logic to programatically clear / add the filer parameters instead of declaratively specifying them. That way you can easily implement logic to manage a search string coming in through a querystring as well. I have not made any changes to your declarative code/markup apart from removing the declared eventhandler (as it is no longer used and also removed from the script block). What you do now is to point to your original search page and just append ?searchString=whateveryouwanttosearchfor. So if you want to implement a small web part with a search field and a button simply so something like the below in the click eventhandler:

Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
    Response.Redirect("~/YourSearchPage.aspx?searchString=" & txtSearch.Text)
End Sub

/Carl.
<script runat="server">
    Dim SearchString As String = ""

    Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As  _
            System.EventArgs)
        ' Simple clean up text to return the Gridview to it's default state
        txtSearch.Text = ""
        SearchString = ""
        VWvisProjects.FilterParameters.Clear()
        VWvisProjects.FilterParameters.Add("projectno", SearchString)
        VWvisProjects.FilterParameters.Add("name", SearchString)
        VWvisProjects.FilterParameters.Add("fullnumber", SearchString)
        VWvisProjects.FilterParameters.Add("ProjManagerName", SearchString)
        VWvisProjects.FilterParameters.Add("PrincipalName", SearchString)
        Gridview1.DataBind()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If Not Request("searchString") Is Nothing Then
                Dim p As String = Request("searchString").ToString()
                VWvisProjects.FilterParameters.Clear()
                VWvisProjects.FilterParameters.Add("projectno", p)
                VWvisProjects.FilterParameters.Add("name", p)
                VWvisProjects.FilterParameters.Add("fullnumber", p)
                VWvisProjects.FilterParameters.Add("ProjManagerName", p)
                VWvisProjects.FilterParameters.Add("PrincipalName", p)
                GridView1.DataBind()
            End If
        Else
            SearchString = txtSearch.Text
            VWvisProjects.FilterParameters.Clear()
            VWvisProjects.FilterParameters.Add("projectno", SearchString)
            VWvisProjects.FilterParameters.Add("name", SearchString)
            VWvisProjects.FilterParameters.Add("fullnumber", SearchString)
            VWvisProjects.FilterParameters.Add("ProjManagerName", SearchString)
            VWvisProjects.FilterParameters.Add("PrincipalName", SearchString)
            GridView1.DataBind()
        End If
    End Sub
</script>

Open in new window

0
 
LVL 30

Author Comment

by:renazonse
ID: 34105593
Perfect.

The web part would not take the VB code in the page so I just added a little JavaScript snippet to run the query string instead.


function gosearchCon() {
    var SearchString = document.forms['aspnetForm'].txtSearch;
    window.location = "Projects/default.aspx?SearchString=" + SearchString.value;
}

function checkEnter(e) {
    var characterCode
    var SearchString = document.forms['aspnetForm'].txtSearch;
    if (e && e.which) {
        e = e
        characterCode = e.which
    }
    else {
        e = event
        characterCode = e.keyCode
    }

    if (characterCode == 13) {
        window.location = "Projects/default.aspx?SearchString=" + SearchString.value;
        return false;
    }
    else {
        return true
    }

}
0
 
LVL 30

Author Closing Comment

by:renazonse
ID: 34105597
Thanks...saved me a great deal of time and suffering.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now