Solved

Gridview search using form or querystring parameter

Posted on 2008-10-23
6
1,889 Views
Last Modified: 2012-08-13
Hi,

I have a simple gridview on a page which is populated with a query that has a parameter in it.  Here's the query.

SELECT     db_key, email_address, company_name, date_entered, valid_email
FROM         dbo.tbl_Email2
WHERE     (email_address LIKE '%' + @email + '%')

I have a text box called searchTextBox on the same web page as the gridview. There is also a regular button.  I want the user to be able to enter a search string, click search and see the filtered results.

<asp:TextBox ID="searchTextBox" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Search" />

If I use the querystring parameter and type the url with a query parameter, it works fine.  But if I use the form parameter it doesn't return any records.

 <asp:FormParameter FormField="searchTextBox" Name="email" Type="string" />

I thought that postback was enabled by default.  Am I missing something?

Thanks,
Bonnie
0
Comment
Question by:Bonnie_K
  • 3
  • 2
6 Comments
 
LVL 8

Assisted Solution

by:mkosbie
mkosbie earned 500 total points
ID: 22784888
You need to tell the gridview to re-databind during the postback by handling Button1's Click event.  Not sure of the expected behavior when you provide two values for the same parameter on a datafield (ie a QueryStringParameter AND a FormParameter), but probably would be unpredictable.  Sample VB code below:
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click

    GridView1.DataBind()

End Sub

Open in new window

0
 

Author Comment

by:Bonnie_K
ID: 22785055
mkosbie - I added that and am still not getting any records.  

I am only using one parameter or the other at a time, I removed the FormParameter and tried the QueryStringParameter for testing, then put the FormParameter back in.

    protected void Button1_Click(object sender, EventArgs e)
    {
        GridView1.DataBind();

    }

0
 
LVL 8

Expert Comment

by:mkosbie
ID: 22785340
Just to make sure, in C#, to handle the event, you need to assign event handling on the element level (rather than with a Handles clause).  If you have it set that way already and are still having problems, will you post the code for us to review?
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click" Text="Search" />

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Bonnie_K
ID: 22785975
yes, it's there...

<asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click" />

I will post the page and code.

Now that it is pasted, I think it has to do with the fact that I am using a master page...
<%@ Page Language="C#" MasterPageFile="~/Email_List/MasterPage2.master" AutoEventWireup="true" CodeFile="search.aspx.cs" Inherits="Email_List_search" Title="Search Email List" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<div style="text-align:center">

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

    <asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click" /><br />

    <br />

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"

        BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataKeyNames="db_key"

        DataSourceID="ObjectDataSource1" ForeColor="Black" GridLines="Vertical">

        <FooterStyle BackColor="#CCCCCC" />

        <Columns>

            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />

            <asp:BoundField DataField="email_address" HeaderText="Email Address" SortExpression="email_address" >

                <ItemStyle HorizontalAlign="Left" />

            </asp:BoundField>

            <asp:BoundField DataField="company_name" HeaderText="Company Name" SortExpression="company_name" >

                <ItemStyle HorizontalAlign="Left" />

            </asp:BoundField>

            <asp:BoundField DataField="date_entered" HeaderText="Date Updated" SortExpression="date_entered" />

        </Columns>

        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />

        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />

        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" Font-Names="Tahoma" Font-Size="Small" />

        <AlternatingRowStyle BackColor="#CCCCCC" />

        <RowStyle Font-Names="Tahoma" Font-Size="Small" />

    </asp:GridView>

    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete" OldValuesParameterFormatString="original_{0}" SelectMethod="GetEmailbySearch"

        TypeName="DataTableAdapters.tbl_Email_Blast2TableAdapter" UpdateMethod="Update">

        <DeleteParameters>

            <asp:Parameter Name="Original_db_key" Type="Int32" />

        </DeleteParameters>

        <UpdateParameters>

            <asp:Parameter Name="email_address" Type="String" />

            <asp:Parameter Name="company_name" Type="String" />

            <asp:Parameter Name="date_entered" Type="DateTime" />

            <asp:Parameter Name="valid_email" Type="String" />

            <asp:Parameter Name="Original_db_key" Type="Int32" />

        </UpdateParameters>

        <SelectParameters>

        

            <asp:QueryStringParameter QueryStringField="test" Name="email" Type="string" />

            

        </SelectParameters>

    </asp:ObjectDataSource>
 
 
 

</div>

</asp:Content>
 
 

---------Code-------------
 
 

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;
 

public partial class Email_List_search : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {
 

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        GridView1.DataBind();

      

    }

}

Open in new window

0
 

Accepted Solution

by:
Bonnie_K earned 0 total points
ID: 22786043
Oops, in the previous post it had the wrong query parameter.  I did test it with

            <asp:FormParameter FormField="searchTextBox" Name="email" Type="string" />

and it still didn't work

I just removed the master page and changed it to an independant page and now it works.

Why won't is work with a master page?

0
 

Expert Comment

by:IntercareSupport
ID: 23205961
Did this work for you?  I can't translate what was in this posting to my problem.

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23996251.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

21 Experts available now in Live!

Get 1:1 Help Now