Pregunta

I am attaching a SqlDataSource to a grid view.

Inside my SqlDataSource I use control parameters to filter the data, I have a button to filter the data using the text inside the textboxes.

The main problem is that nothing is being filtered.

Here is my 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>

And here is my code behind:

 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();
        }
    }

EDIT: The contentplaceholder named content is inside this on my master page:

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

Is because this?

¿Fue útil?

Solución

Have you tried to remove the UpdatePanel from the Master Page and add it to your page. Best practices recomend that you wrap your content with the update panel, only in the areas you need to update. This way you won't generate much traffic. In this case, the UpdatePanel, won't bind well with the button inside the content/page, and like so, it won't trigger the update on the update panel.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top