GridView OnPageIndexChanging事件不会触发

时间:2014-03-19 15:52:32

标签: c# asp.net gridview paging page-index-changed

我的GridView和代码背后的一切似乎都很好,但事件不会触发。

这是我的网格:

   <asp:UpdatePanel ID="ISOSearchUpdatePanel" runat="server">
  <ContentTemplate>           
   <div id="divIsoSelect" style="width:700px; height:60px; background-color:white">

    <fieldset>
        <legend style="font-size: larger; font-weight: bold">Search ISO by</legend>
    <table>
    <tr>
        <td><asp:DropDownList ID="ddlISOSearch" runat="server" AutoPostBack="true"  OnSelectedIndexChanged="ddlISOSearch_SelectedIndexChanged">
            <asp:ListItem Value="">Select...</asp:ListItem>
            <asp:ListItem Value="ISO Number" >ISO Number</asp:ListItem>
            <asp:ListItem Value="Name">Name</asp:ListItem>
            <asp:ListItem Value="Region" >Region</asp:ListItem>
            <asp:ListItem Value="Division">Division</asp:ListItem>

         </asp:DropDownList></td>
   <td >&nbsp;</td>
    <td>
        <asp:Panel ID="pnlSearchEntry" runat="server" Visible="false"   style="width:500px; border:gainsboro; border-width: thick;">
            <table>
            <tr>
                <td valign="middle" style="width:120px"><asp:Label ID="lblEntry"  runat="server" Text=""></asp:Label></td>

                <td valign="top"><asp:TextBox ID="txtSearchValue" runat="server">  </asp:TextBox></td>

                <td valign="top">&nbsp;&nbsp;<asp:Button ID="btnFind" runat="server" Text="Select" OnClick="btnFind_Click"/></td>
            </tr>
            </table>
        </asp:Panel>
    </td>
    </tr>
</table>               
</fieldset>
</div> 
               <asp:GridView ID="grvIsoSearchResults" runat="server" 
                AutoGenerateColumns="false" PageSize="15"
                AllowPaging = "true" Visible="false"  BorderColor="Red" GridLines="Both"
                OnRowCommand="grvIsoSearchResults_RowCommand"
                DataKeyNames="isonum" 
                OnPageIndexChanged="grvIsoSearchResults_PageIndexChanged"
                OnPageIndexChanging="grvIsoSearchResults_PageIndexChanging"
                OnSorting="grvIsoSearchResults_Sorting"                    
                style="position:absolute; top: 120px;">
                <PagerSettings firstpagetext="First" lastpagetext="Last" nextpagetext="Next" pagebuttoncount="100" />
                <Columns>
                   <asp:TemplateField HeaderText="ISONUM" SortExpression="isonum">
                        <ItemTemplate>
                            <asp:Label ID="txtgvIsoNum" Text = '<%# Eval("ISONUM")%>' runat="server" Width="70px" style="text-align:center" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="OFFICE NAME" SortExpression="isoofficename">
                        <ItemTemplate>
                            <asp:Label ID="txtgvIsoOfficeName" Text = '<%# Eval("ISOOFFICENAME")%>' runat="server" Width="200px" style="text-align:center" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="REGION" SortExpression="region">
                        <ItemTemplate>
                            <asp:Label ID="txtgvRegion" Text = '<%# Eval("REGION")%>' runat="server" Width="50px" style="text-align:center" />
                        </ItemTemplate>
                    </asp:TemplateField> 
                    <asp:TemplateField HeaderText="DIVISION" SortExpression="client_id">
                        <ItemTemplate>
                            <asp:Label ID="txtgvDivision" Text = '<%# Eval("CLIENT_ID")%>' runat="server" Width="50px" style="text-align:center" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="EMAIL ADDRESS">
                        <ItemTemplate>
                            <asp:TextBox ID="txtgvEmailAddress" Text = '<%# Eval("EMAIL")%>' runat="server" Width="200px" />
                            <asp:Button ID="btnEmailUpdate" Text = "Update" runat="server" CommandName="UpdateEmail" onClientClick="return ValidateEmail(this)" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <pagerstyle backcolor="#005482" ForeColor="White" height="30px" verticalalign="Bottom" horizontalalign="Center"/>
            </asp:GridView> 
    </ContentTemplate>
    </asp:UpdatePanel>

这是我的代码隐藏方法:

    private void SetViewState(DataSet myDataSet)
    {
       ViewState["Data"] = myDataSet;
    } 

    private DataSet GetViewState()
    {
        return (DataSet)ViewState["Data"];
    }

    private void LoadData(String selection, String userInput)
    {
        DataSet ds = new DataSet();
        .
        .
        .
        ds = ISOProcessing.GetISOData(selection, userInput);

        grvIsoSearchResults.DataSource = ds;
        if (ds.Tables[0].Rows.Count > 0)
        {
            SetViewState(ds);
            grvIsoSearchResults.DataBind();
            grvIsoSearchResults.Visible = true;

        }
        else
        {
            lblMsg.ForeColor = System.Drawing.Color.Red;
            this.lblMsg.Text = "No data for current selection";
        }
    }

    protected void grvIsoSearchResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
         grvIsoSearchResults.PageIndex = e.NewPageIndex;
         grvIsoSearchResults.DataSource = GetViewState();
         grvIsoSearchResults.DataBind();
    } 

似乎所有内容都已到位,但是当我想在调试期间转到另一个页面时,我的grvIsoSearchResults_PageIndexChanging()方法内部甚至没有触发断点。我不确定为什么会这样。

需要你的帮助。

谢谢

2 个答案:

答案 0 :(得分:2)

尝试为其添加触发器:

    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="grvIsoSearchResults" EventName="PageIndexChanging" />
    </Triggers>
</asp:UpdatePanel>

答案 1 :(得分:0)

Gridview的AutoPostback属性未启用,请将其设置为true。

<asp:GridView ID="grvIsoSearchResults" AutoPostback="True" runat="server" 
            AutoGenerateColumns="false" PageSize="15"
            AllowPaging = "true" Visible="false"  BorderColor="Red" GridLines="Both"
            OnRowCommand="grvIsoSearchResults_RowCommand"
            DataKeyNames="isonum" 
            OnPageIndexChanged="grvIsoSearchResults_PageIndexChanged"
            OnPageIndexChanging="grvIsoSearchResults_PageIndexChanging"
            OnSorting="grvIsoSearchResults_Sorting"                    
            style="position:absolute; top: 120px;">
            <PagerSettings firstpagetext="First" lastpagetext="Last" nextpagetext="Next" pagebuttoncount="100" />
            <Columns>
               <asp:TemplateField HeaderText="ISONUM" SortExpression="isonum">
                    <ItemTemplate>
                        <asp:Label ID="txtgvIsoNum" Text = '<%# Eval("ISONUM")%>' runat="server" Width="70px" style="text-align:center" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="OFFICE NAME" SortExpression="isoofficename">
                    <ItemTemplate>
                        <asp:Label ID="txtgvIsoOfficeName" Text = '<%# Eval("ISOOFFICENAME")%>' runat="server" Width="200px" style="text-align:center" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="REGION" SortExpression="region">
                    <ItemTemplate>
                        <asp:Label ID="txtgvRegion" Text = '<%# Eval("REGION")%>' runat="server" Width="50px" style="text-align:center" />
                    </ItemTemplate>
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="DIVISION" SortExpression="client_id">
                    <ItemTemplate>
                        <asp:Label ID="txtgvDivision" Text = '<%# Eval("CLIENT_ID")%>' runat="server" Width="50px" style="text-align:center" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EMAIL ADDRESS">
                    <ItemTemplate>
                        <asp:TextBox ID="txtgvEmailAddress" Text = '<%# Eval("EMAIL")%>' runat="server" Width="200px" />
                        <asp:Button ID="btnEmailUpdate" Text = "Update" runat="server" CommandName="UpdateEmail" onClientClick="return ValidateEmail(this)" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <pagerstyle backcolor="#005482" ForeColor="White" height="30px" verticalalign="Bottom" horizontalalign="Center"/>
        </asp:GridView>