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)
Then, when I enter : 21.15 in the search box and hit "Search", I get this back:
Which shows : Found 885 rows matching keyword '21.15'.
What it should display is this (which is achieved by pressing the "Search" button again):
This is the frontpage Code :
<asp:TextBox ID="txtSearch" runat="server" Width="265px" Height="22px" CssClass="myBox"></asp:TextBox>
<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>