0

I have a function that sort of works but only when manually clicked... So let me explain

When I load the page, Label1 is set to Visible = false;. That works. Then when I click btnSearch is sets the Label1.Visible = true; and should also do :

if (!string.IsNullOrEmpty(txtSearch.Text))
    {
        Label1.Text = "Found " + GridView1.Rows.Count + " rows matching keyword '" + txtSearch.Text + "'.";
    }

Which is sort of does. It returns the TOTAL amount of rows from my SQLDataSource (ie 885) instead of 13 that it should. If I click the btnSearch again, it updates the Label1 correctly and displays

"Found 13 rows matching keyword '21.15'."

This is my CodeBehind:

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Visible = false;
        if (Page.IsPostBack)
        {
            if (!string.IsNullOrEmpty(txtSearch.Text))
            {
                Label1.Text = "Found " + GridView1.Rows.Count + " rows matching keyword '" + txtSearch.Text + "'.";
            }
        }
    }

protected void btnSearch_Click(object sender, EventArgs e)
{
    Label1.Visible = true;
    if (!string.IsNullOrEmpty(txtSearch.Text))
    {
        Label1.Text = "Found " + GridView1.Rows.Count + " rows matching keyword '" + txtSearch.Text + "'.";
    }
}
protected void onSelectedData(object sender, SqlDataSourceStatusEventArgs e)
{
    Label1.Visible = true;
    if (!string.IsNullOrEmpty(txtSearch.Text))
    {
        Label1.Text = "Found " + GridView1.Rows.Count + " rows matching keyword '" + txtSearch.Text + "'.";
    }
}
}

These are the options I have tried so far, but none will give me the correct result on the first press of the button.

To give a better visual Representation : This is the initial Load (data is cutoff on the sides for privacy, but other wise it displays 885 rows of data) Initial Load

Then, when I enter : 21.15 in the search box and hit "Search", I get this back: Initial Press of Search

Which shows : Found 885 rows matching keyword '21.15'.

What it should display is this (which is achieved by pressing the "Search" button again): Second Press of Search

This is the frontpage Code :

<asp:TextBox ID="txtSearch" runat="server" Width="265px" Height="22px" CssClass="myBox"></asp:TextBox>
                &nbsp;<asp:Button ID="btnSearch" runat="server" Text="Search" class="myButton" OnClick="btnSearch_Click"/>
            <br />
                <br />
                <asp:Label ID="Label1" runat="server">Rows Returned : </asp:Label>
                <br />

            <br />
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" DataSourceID="GridDataSource" AllowPaging="False" EnableModelValidation="True" PageSize="50" CellPadding="4" EnableTheming="True" ForeColor="#333333" GridLines="None" Width="100%" style="margin-top: 0px; text-align: center;" AllowSorting="True">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />


                </asp:GridView>
                <asp:SqlDataSource ID="GridDataSource" runat="server" DataSourceMode="DataSet" ConnectionString="<%$ ConnectionStrings:Enforsys_Systems_Inc_MSCRMConnectionString %>"
                    SelectCommand="SELECT 
                                    ab.NAME as [Customer] 
                                    ,ISNULL(ab.TELEPHONE1,'') as [Phone #] 
                                    ,ISNULL(pb.NAME,'') as [Product] 
                                    ,ISNULL(aeb.NEW_PRODUCTVERSION,'') as [Version] 
                                    ,CASE WHEN ab.STATUSCODE = 1 THEN 'Active' ELSE 'Inactive' END as [Status] 
                                    ,ISNULL('Sal : ' + c.SALUTATION + ' / ','') 
                                        + ISNULL('Title : ' + c.JOBTITLE + ' / ','') 
                                        + ISNULL(a.PRIMARYCONTACTIDNAME,'') as [Primary Contact] 
                                    ,ISNULL(c.TELEPHONE1,'') as [Contact Phone] 
                                FROM 
                                    ACCOUNTBASE ab LEFT JOIN ACCOUNTEXTENSIONBASE aeb on ab.ACCOUNTID = aeb.ACCOUNTID 
                                    LEFT JOIN PRODUCTBASE pb on aeb.NEW_PRIMARYPRODUCTID = pb.PRODUCTID 
                                    LEFT JOIN ACCOUNT a on ab.ACCOUNTID = a.ACCOUNTID 
                                    LEFT JOIN CONTACT c on a.PRIMARYCONTACTID = c.CONTACTID ORDER BY ab.NAME" 
                    FilterExpression="Customer LIKE '%{0}%' or Product LIKE '%{0}%' or Version LIKE '%{0}%' or Status LIKE '{0}%' or [Primary Contact]  LIKE '%{0}%'">
                    <FilterParameters>
                        <asp:ControlParameter Name="Customer" ControlID="txtSearch" PropertyName="Text" />
                    </FilterParameters>
                </asp:SqlDataSource>
JohnZ
  • 382
  • 2
  • 8
  • 20
  • You say something that leads me to believe you are leaving something out that is necessary to help you solve your problem: "It returns the TOTAL amount of rows from my SQLDataSource (ie 885) instead of 13 that it should." Why should it only show 13? Where does your datasource filter from 885 to 13? I do not see either of these in the code you provided. – Taylor Brown Dec 22 '14 at 15:31
  • I have updated my post with pictures, hopefully that helps. – JohnZ Dec 22 '14 at 15:54
  • i still don't see where your code is that filters the grid based on the "Search Customer" text. This is very important to solving your problem. – Taylor Brown Dec 22 '14 at 16:01
  • Because if the grid filters after the label text is set, then this is what is causing your problem, order of operations. – Taylor Brown Dec 22 '14 at 16:08
  • Added the frontpage code. Thank you for the help – JohnZ Dec 22 '14 at 16:13

2 Answers2

0

You need to make sure the data is bound to the grid before you set the label's text.

Try this:

protected void btnSearch_Click(object sender, EventArgs e)
{

    GridView1.DataSource = GridDataSource
    GridView1.DataBind()

    Label1.Visible = true;
    if (!string.IsNullOrEmpty(txtSearch.Text))
    {
        Label1.Text = "Found " + GridView1.Rows.Count + " rows matching keyword '" + txtSearch.Text + "'.";
    }
}

Make sure you remove the label setting code from the other places, you only need it in one place once you get it to work.

Also, my own personal opinion, you may benefit from losing the datasource control and learning to query the database from your code behind. Here are some links for that:

GridView's DataSource in Code Behind

http://www.dotnetfunda.com/articles/show/1594/how-to-populate-gridview-from-code-behind

Community
  • 1
  • 1
Taylor Brown
  • 1,689
  • 2
  • 17
  • 33
  • Worked, but 2 issues come up. #1 Sorting becomes broken with :The GridView 'GridView1' fired event Sorting which wasn't handled. and #2 The initial page load, has no rows at all until search is performed – JohnZ Dec 22 '14 at 16:55
  • It sounds like you may have removed this from your GridView1 attributes: DataSourceID="GridDataSource" -- did you? You can only do that if you learn to query the database from code behind OR if you specify GridView1.DataSource = GridDataSource and GridView1.DataBind() when you need them to be bound... It sounds like you should look at the links I provided or do some more reading on gridviews and datasources. – Taylor Brown Dec 22 '14 at 17:08
0

After a little more looking around, I have come to an answer that works great.

@Taybriz , I do appreciate all your help though. I have another set of the page which uses the codebehind SQL selection, but getting Sorting to work on it is proving a hassle.

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Visible = false;
        if (Page.IsPostBack)
        {
            GridView1.DataBind();
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Label1.Visible = true;
        if (!string.IsNullOrEmpty(txtSearch.Text))
        {
            Label1.Text = "Found " + GridView1.Rows.Count + " rows matching keyword '" + txtSearch.Text + "'.";
        }

        if (string.IsNullOrEmpty(txtSearch.Text))
        {
            Label1.Text = "Found " + GridView1.Rows.Count + " rows";
        }
    }
}
JohnZ
  • 382
  • 2
  • 8
  • 20
  • this looks more like a workaround than a solution... you should look into gridview sorting as well because it sounds more like you need a tutorial on gridviews and datasources than anything else... but i am glad u found something that works for your situation for the time being. – Taylor Brown Dec 22 '14 at 18:16