Asp.net SQLDataSource未立即返回的行数

Asp.net SQLDataSource未立即返回的行数,asp.net,gridview,count,Asp.net,Gridview,Count,我有一个功能,这种工作,但只有当手动点击。。。让我解释一下 加载页面时,Label1设置为Visible=false;。这很有效。然后,当我单击btnSearch时,设置Label1.Visible=true;并应做到: if (!string.IsNullOrEmpty(txtSearch.Text)) { Label1.Text = "Found " + GridView1.Rows.Count + " rows matching keyword '" + txtSe

我有一个功能,这种工作,但只有当手动点击。。。让我解释一下

加载页面时,Label1设置为Visible=false;。这很有效。然后,当我单击btnSearch时,设置Label1.Visible=true;并应做到:

if (!string.IsNullOrEmpty(txtSearch.Text))
    {
        Label1.Text = "Found " + GridView1.Rows.Count + " rows matching keyword '" + txtSearch.Text + "'.";
    }
这在某种程度上可以说是一件好事。它从我的SQLDataSource ie 885返回行的总数,而不是它应该返回的13行。如果我再次单击BTN搜索,它将正确更新Label1并显示

找到13行匹配关键字“21.15”

这是我的代码隐藏:

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 + "'.";
    }
}
}
这些是我迄今为止尝试过的选项,但没有一个会在第一次按下按钮时给出正确的结果

要提供更好的视觉表现,请执行以下操作: 这是初始加载数据,出于隐私考虑,从侧面切断,但从另一方面看,它显示885行数据

然后,当我在搜索框中输入:21.15并点击搜索时,我得到了以下信息:

显示:找到885行匹配关键字“21.15”

它应该显示的是通过再次按下搜索按钮实现的:

这是frontpage代码:

<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>

在设置标签文本之前,需要确保数据已绑定到网格

试试这个:

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 + "'.";
    }
}
请确保从其他位置删除标签设置代码,一旦开始工作,您只需要在一个位置使用它

另外,我个人认为,您可能会从失去数据源控制和学习从代码中查询数据库中获益。以下是一些相关链接:


再环顾四周,我得到了一个非常有效的答案

@泰布里兹,我非常感谢你的帮助。我还有一组页面使用了codebehind SQL选择,但要对其进行排序却很麻烦

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";
        }
    }
}

你说的话让我相信你漏掉了一些帮助你解决问题所必需的东西:它从我的SQLDataSource ie 885返回的行总数,而不是它应该返回的13行。为什么只显示13?您的数据源在哪里从885过滤到13?我在你提供的代码中都没有看到。我已经用图片更新了我的帖子,希望能有所帮助。我仍然看不到你的代码在哪里根据搜索客户文本过滤网格。这对于解决您的问题非常重要。因为如果在设置标签文本后网格过滤,那么这就是导致问题的原因,操作顺序。添加了frontpage代码。感谢您的帮助,但出现了两个问题。1排序因以下原因而中断:GridView“GridView1”触发了未处理的事件排序。初始页面加载,在执行搜索之前没有任何行。听起来您可能已经从GridView1属性中删除了它:DataSourceID=GridDataSource-是吗?只有在学习从代码隐藏查询数据库,或者在需要绑定时指定GridView1.DataSource=GridDataSource和GridView1.DataBind时,才能执行此操作。。。听起来你应该看看我提供的链接,或者对GridView和数据源做更多的阅读。。。您还应该研究gridview排序,因为它听起来更像是您需要一个关于gridview和数据源的教程,而不是其他任何东西。。。但我很高兴你找到了适合你目前情况的方法。