GridView不会过滤结果

时间:2014-02-03 12:35:39

标签: c# gridview filter expression aspxgridview

我将SqlDataSource附加到网格视图。

在我的SqlDataSource中,我使用控制参数来过滤数据,我有一个按钮,使用文本框内的文本过滤数据。

主要问题是没有任何东西被过滤。

这是我的aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Template.Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head_breadcrumb" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentInfoBarName" runat="server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="ContentInfoBarApprovalStatus" runat="server">
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="ContentInfoBarState" runat="server">
</asp:Content>
<asp:Content ID="Content6" ContentPlaceHolderID="content" runat="server">

    <tr>

        <td width="100" align="right" bgcolor="#eeeeee" class="header1">Power Web</td>
        <td align="center" bgcolor="#FFFFFF">
        <asp:TextBox ID="txtNB" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtSystem" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtObjectID" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtObjectDescription" runat="server"></asp:TextBox>
        <asp:Button ID="btnSearch" runat="server" Text="Search" />
        <asp:GridView ID="gvwExample" runat="server" OnClick="filter" AutoGenerateColumns="False" DataSourceID="GridDataSource" OnRowDataBound="gvwExample_RowDataBound" CssClass="basix" >
        <columns>
           <asp:BoundField DataField="NB" HeaderText="NB" />
           <asp:BoundField DataField="Name" HeaderText="Name" />
           <asp:BoundField DataField="CLevel" HeaderText="CLevel" />
           <asp:BoundField DataField="CC Host" HeaderText="CC Host" />
           <asp:BoundField DataField="System" HeaderText="System" />
           <asp:BoundField DataField="Object Type" HeaderText="Object Type" />
           <asp:BoundField DataField="Object ID" HeaderText="Object ID" />
           <asp:BoundField DataField="Object Description" HeaderText="Object Description" />
        <asp:BoundField DataField="Excl Mngr" HeaderText="Excl Mngr" />
        </columns>
        </asp:GridView> 
        <asp:SqlDataSource ID="GridDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:PowerWeb%>"
            SelectCommand="SELECT * FROM Table1" FilterExpression="NB LIKE '{0}%' AND System LIKE '{1}%' AND Object ID LIKE '{2}%' AND Object Description LIKE '{3}%'">
            <FilterParameters>
                <asp:ControlParameter Name="NB" ControlID="txtNB" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" />
                <asp:ControlParameter Name="System" ControlID="txtSystem" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" />
                <asp:ControlParameter Name="Object ID" ControlID="txtObjectID" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" />
                <asp:ControlParameter Name="Object Description" Type="String"  ControlID="txtObjectDescription" PropertyName="Text"  />
            </FilterParameters>
        </asp:SqlDataSource>
        <asp:label ID="lblStatus" runat="server"></asp:label></td>

    </tr>

</asp:Content>

这是我背后的代码:

 public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void gvwExample_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int index = GetColumnIndexByName(e.Row, "Excl Mngr");
                if (e.Row.Cells[index].Text == "False")
                {
                    e.Row.Cells[index].Text = "";
                }
                else if(e.Row.Cells[index].Text == "True")
                {
                    e.Row.Cells[index].Text = "Yes";
                }

            }
        }

        int GetColumnIndexByName(GridViewRow row, string SearchColumnName)
        {
            int columnIndex = 0;
            foreach (DataControlFieldCell cell in row.Cells)
            {
                if (cell.ContainingField is BoundField)
                {
                    if (((BoundField)cell.ContainingField).DataField.Equals(SearchColumnName))
                    {
                        break;
                    }
                }
                columnIndex++;
            }
            return columnIndex;
        }

        public void filter(Object sender, EventArgs e)
        {

            gvwExample.DataSource = GridDataSource;
            gvwExample.DataBind();
        }
    }

编辑: 名为content的contentplaceholder位于我的母版页上:

 <asp:UpdatePanel ID="updateMain" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
                            <ContentTemplate>
                                <asp:ContentPlaceHolder ID="content" runat="server"/>
                            </ContentTemplate>
                         </asp:UpdatePanel>

是因为这个吗?

1 个答案:

答案 0 :(得分:1)

您是否尝试从母版页中删除UpdatePanel并将其添加到您的页面。最佳实践建议您使用更新面板包装内容,仅在需要更新的区域中。这样您就不会产生太多流量。 在这种情况下,UpdatePanel将无法与内容/页面内的按钮很好地绑定,并且不会在更新面板上触发更新。