GridView的DropDownList过滤器无法正常工作

时间:2012-03-14 07:27:06

标签: c# asp.net

在我正在处理的基于Web的应用程序中,我有两个GridView。在第一个GridView上方有一个DropDownList,用于根据用户的划分过滤建议。将第二个GridView放入UpdatePanel控件后,此DropDownList不再起作用,我不知道为什么或这与此之间的关系是什么。有什么想法吗?

在UpdatePanel控件中添加第二个GridView之前一切正常,但是现在用于过滤第一个GridView中的建议的过滤器不起作用。任何的想法?有什么帮助吗?

我的ASP.NET代码:

<strong> Division </strong>
    <asp:DropDownList ID="ddlDivision" runat="server" AppendDataBoundItems="True" 
        AutoPostBack="True" DataSourceID="sqlDataSourceDivision" 
        DataTextField="DivisionShortcut" DataValueField="DivisionShortcut"  
        Width="175px" EnableViewState="False">
        <asp:ListItem Value="%">All</asp:ListItem>
    </asp:DropDownList>    
    <br />  <br />  
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                        AllowSorting="True" AutoGenerateColumns="False" 
                        DataKeyNames="ID" 
                        width="950px" CssClass="mGrid"
                        AlternatingRowStyle-CssClass="alt" 
                        RowStyle-HorizontalAlign="Center" 
                        DataSourceID="SqlDataSource1" 
                        OnRowDataBound="GridView1_RowDataBound" >
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <HeaderStyle Font-Bold = "true" ForeColor="Black" Height="20px"/> 
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"  ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="Description" HeaderText="Description"  SortExpression="Description" />
                <asp:BoundField DataField="Name" HeaderText="Name"  SortExpression="Name" />
                <asp:BoundField DataField="Username" HeaderText="Username"  SortExpression="Username" />
                <asp:BoundField DataField="DivisionShortcut" HeaderText="Division"  SortExpression="DivisionShortcut" />
                <asp:TemplateField HeaderText="Status">
                    <ItemTemplate>
                        <asp:DropDownList ID="DropDownList" runat="server" 
                          DataSourceID="SqlDataSource2"
                          Font-Bold="True" ForeColor="#006666" 
                          AppendDataBoundItems="false" 
                          DataTextField="Status" DataValueField="ID" 
                          AutoPostBack="true" 
                          OnDataBound="DropDownList_DataBound" 
                          OnSelectedIndexChanged ="DropDownList_SelectedIndexChanged">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
            SelectCommand="SELECT dbo.SafetySuggestionsLog.ID, dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.employee.Name, dbo.SafetySuggestionsLog.Username, dbo.Divisions.DivisionShortcut FROM dbo.employee INNER JOINdbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN dbo.Divisions ON dbo.employee.DivisionCode = dbo.Divisions.SapCode"
                      FilterExpression="[DivisionShortcut] like '{0}%'">
                      <FilterParameters>
                        <asp:ControlParameter ControlID="ddlDivision" 
                           Name="DivisionShortcut" PropertyName="SelectedValue" 
                           Type="String" />
                    </FilterParameters>
        </asp:SqlDataSource>

        <%--For the DropDownList--%>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server"
           ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
           SelectCommand="SELECT * FROM [SafetySuggestionsStatus]">
        </asp:SqlDataSource>  
        <%--Filtering by Division--%>
        <asp:SqlDataSource ID="sqlDataSourceDivision" runat="server" 
        ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
        SelectCommand="SELECT [DivisionShortcut] FROM [Divisions]"></asp:SqlDataSource>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
           <Triggers>
                <asp:AsyncPostBackTrigger ControlID="ddlDivision" 
                 EventName="SelectedIndexChanged" />
           </Triggers>
           <ContentTemplate>
           <br />
           <p><h3><b><u>PMOD Divisions' Safety Suggestions List</u></b></h3>
               <p></p><br />
               <asp:Panel ID="Panel1" runat="server">
                  <asp:GridView ID="GridView2" runat="server" AllowSorting="True" AlternatingRowStyle-CssClass="alt" AutoGenerateColumns="False" CssClass="mGrid" RowStyle-HorizontalAlign="Center" 
                      DataSourceID="SqlDataSource4" >
                      <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                      <HeaderStyle Font-Bold="true" ForeColor="Black" Height="20px" />
                      <Columns>
                        <asp:BoundField DataField="SubmittedMonth" HeaderText="Month Submitted" ReadOnly="True"  SortExpression="SubmittedMonth" />
                        <asp:BoundField DataField="DivisionShortcut"  HeaderText="Division" SortExpression="DivisionShortcut" />
                        <asp:BoundField DataField="Username" HeaderText="NetworkID"  SortExpression="Username" />
                        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                        <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                        <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
                        <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
                        <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
                       </Columns>
                       <RowStyle HorizontalAlign="Center" />
                     </asp:GridView>
                  </asp:Panel>
                  <asp:SqlDataSource ID="SqlDataSource4" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT TOP (100) PERCENT LEFT(DATENAME(month, dbo.SafetySuggestionsLog.DateSubmitted), 3) + '-' + DATENAME(year, dbo.SafetySuggestionsLog.DateSubmitted)  AS SubmittedMonth, dbo.Divisions.DivisionShortcut, dbo.SafetySuggestionsLog.Username, dbo.employee.Name, dbo.SafetySuggestionsLog.Title,  dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsType.Type, dbo.SafetySuggestionsStatus.Status FROM  dbo.Divisions INNER JOIN dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode INNER JOIN dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN dbo.SafetySuggestionsType ON dbo.SafetySuggestionsLog.TypeID = dbo.SafetySuggestionsType.ID INNER JOIN dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID WHERE     (DATEDIFF(month, dbo.SafetySuggestionsLog.DateSubmitted, GETDATE()) &lt; 3) ORDER BY dbo.SafetySuggestionsLog.DateSubmitted DESC">
                   </asp:SqlDataSource>
     <asp:Button ID="btnPrint" runat="server" OnClick="btnPrint_Click" Text="Print" />
     <p></p></p>    
   </ContentTemplate>
 </asp:UpdatePanel>

代码隐藏:

protected void btnPrint_Click(object sender, EventArgs e)
    {
        Session["ctrl"] = Panel1;
        ScriptManager.RegisterStartupScript(GridView2, GridView2.GetType(), "onclick", "window.open('Print.aspx','PrintMe','height=400px,width=800px,scrollbars=1');", true);
    }    
    int i = 1;
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Text = i.ToString();
            i++;    
        }
    }  
    protected void DropDownList_DataBound(object sender, EventArgs e)
    {
        if (!IsPostBack)
            ((DropDownList)sender).Items.Insert(0, new ListItem("--Select--", ""));
    }    
    protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        int suggestionStatus = int.Parse(ddl.SelectedValue);
        GridViewRow row = (GridViewRow)ddl.NamingContainer;
        string strID = GridView1.DataKeys[row.RowIndex]["ID"].ToString();
        int ID = Int32.Parse(strID);
        //For inserting the status in the database
        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
        string updateCommand = "UPDATE SafetySuggestionsLog  SET [StatusID] = @StatusID WHERE [ID] = @ID";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
            {
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@StatusID", suggestionStatus);
                cmd.Parameters.AddWithValue("@ID", ID);
                cmd.ExecuteNonQuery();
            }
            conn.Close();
        }    
        GridView2.DataBind();    
    }

4 个答案:

答案 0 :(得分:0)

为什么不尝试将DropDownList放在updatepanel中并将回发触发器添加到DropDownList

答案 1 :(得分:0)

您要填充的网格视图应位于“更新面板”


您还应该在“更新”面板中考虑UpdateMode,因为它是Always by default


您可能会错过gridview 1

DropDownList handler的绑定

答案 2 :(得分:0)

我认为发生的事情是,一旦将ddlDivision设置为UpdatePanel的触发器,它将不再执行正常的回发。相反,它将导致UpdatePanel部分回发过程。由于您的Gridview1未包含在UpdatePanel的ContentTemplate中,因此在回发后将不再更新。这就是UpdatePanel的工作方式 - 他们只返回对其中项目的更改,而不是页面上的其他项目。

在将UdpatePanel与下拉列表作为触发器之前,ddlDivision上的AutoPostback设置会导致整页刷新,并且您的gridview将被过滤,因为您的数据源使用了ddl作为参数。

答案 3 :(得分:0)

首先,您添加了ScriptManager吗?

无论如何,查看您的代码,我可以看到您为 ddlDivision 添加了 AsyncPostBackTrigger ,但您尚未为创建 SelectedIndexChanged 事件strong> ddlDivision ,试试这个:

<asp:DropDownList ID="ddlDivision" runat="server" AppendDataBoundItems="True" 
AutoPostBack="True" DataSourceID="sqlDataSourceDivision" DataTextField="DivisionShortcut" 
DataValueField="DivisionShortcut" Width="175px" EnableViewState="False" 

OnSelectedIndexChanged="ddlDivision_SelectedIndexChanged">

    <asp:ListItem Value="%">All</asp:ListItem>
</asp:DropDownList>

然后,当然,添加你的代码来处理事件。