单击gridview上的页面索引不调用PageIndexChanging事件

时间:2014-01-09 02:41:44

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

我有一个gridview gdvNotification。第一次加载页面时:我的gridview有分页。

我的问题是:

  • 当页面加载第一次时:我点击页面索引,但页面索引没有变化。我发现:点击页面索引时,调用codebehide但不调用gdvNotification_PageIndexChanging事件。
  • 在浏览器上我想在浏览器的控制台中使用脚本javascript:__doPostBack('ctl00$MainContent$gdvNotification','Page$2'),它正常工作。调用了gdvNotification_PageIndexChanging事件。

以下是我的观点:

<asp:GridView ID="gdvNotification" runat="server" AutoGenerateColumns="False" Style="width: 100%;"
                            BorderWidth="0px" CellSpacing="0" CellPadding="0" GridLines="None" PageSize="1"
                            AllowPaging="True" ShowHeader="false" OnPageIndexChanging="gdvNotification_PageIndexChanging">
                            <HeaderStyle CssClass="BasicColumnTitle" />
                            <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:Panel ID="panel1" runat="server" Width="100%" Height="618" ScrollBars="Vertical">
                                            <table style="width: 100%;">
                                                <tbody>
                                                    <tr style="height: 40px">
                                                        <td class="BasicValue" style="vertical-align: bottom;padding-left:2em">
                                                            <div>
                                                                <asp:Label ID="lblhead" CssClass="bold" runat="server" Text='<% #Eval("Subject")%>' Style=" font-weight: bold"></asp:Label>
                                                            </div>
                                                        </td>
                                                    </tr>
                                                    <tr style="height: 40px">
                                                        <td class=" BasicValue" style="vertical-align: bottom ;padding-left: 2em;">
                                                            <div>
                                                                <asp:Label ID="Label2" runat="server" Style=" font-weight: bold" Text='<% #Eval("AuthorName")%>'></asp:Label><asp:Label
                                                                    ID="Label3" runat="server" Style="float: right" Text='<%# string.Format("{0:yyyy/MM/dd hh:mm}",Eval("BulletinDateFrom"))%>'></asp:Label></div>
                                                        </td>
                                                    </tr>
                                                    <tr style="height: 40px">
                                                        <td class="BasicValue" style="vertical-align: bottom">
                                                            <div>
                                                                <asp:Label ID="lblSection" runat="server" Style="padding-left: 2em;" Text='<%# GetSectionName(Eval("Id").ToString())%>'></asp:Label>
                                                            </div>
                                                    </tr>
                                                    <tr style="height: 497px;">
                                                        <td class="BasicValue" style="text-align: left; vertical-align: top; padding-left:2em ">
                                                            <asp:Label ID="lblContent" runat="server"
                                                                Text='<% #Eval("Contents")%>'></asp:Label>
                                                        </td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </asp:Panel>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>

页面加载方法:

protected void Page_Load(object sender, EventArgs e)
    {
        logger.Debug("Handle event page load.");
        if (!IsPostBack)
        {
                try
                {
                    MasterDataHelper.RefreshCacheHeadLine();
                    //Get time display from system parameter
                    logger.Debug("Get limit new data display list.");
                    IList<SystemParamEntity> limitNewDataDisplayList = MasterDataHelper.GetSystemParamEntityByKbn(limitDataKBN);
                    IList<decimal> limitNewDataDisplayList1 =
                        (from item in limitNewDataDisplayList where item.DelFlg == false select item.Param1).ToList();
                    if (!limitNewDataDisplayList1.Any())
                    {
                        string msg = string.Format(Messages.I00010, "新規表示期間");
                        logger.Error(msg);
                    }
                    else
                    {
                        limitNewDataDisplay = limitNewDataDisplayList1[0];
                        this.hdflimitNewDataDisplay.Value = limitNewDataDisplay.ToString();
                    }
                    //Get data from session and cache
                    GetDataFromSessionAndCache();
                    //TODO: check business input datetime.now
                    var searchresult = ProcessFuzzySearch(DateTime.Now, SessionUtil.GetFromSession<string>(SESSION_FUZZYSEARCH));
                    txtFuzzySearch.Text = SessionUtil.GetFromSession<string>(SESSION_FUZZYSEARCH);
                    lblDate.Text = string.Format("{0}年{1}月{2}日({3})", DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day,
                                                 GetDayOfWeek((int)DateTime.Now.DayOfWeek));
                    pnPageIndex.Visible = false;
                    grvHeadline.DataSource = searchresult;
                    grvHeadline.DataBind();
                    if (searchresult.Count > 0)
                    {

                        string notificationIdSelected = searchresult[0].Id;
                        hdfNotificationIdSelected.Value = searchresult[0].Id;
                        HeadlineSelected(notificationIdSelected, 0);
                        grvHeadline.SelectRow(0);
                    }
                    else
                    {
                        //if: notification.count=0 => disable button edit notification
                        btnEditNotification.Enabled = false;
                        ClearDataAndDisplayForGridNotification();
                    }
                }
                catch (ApplicationException ex)
                {
                    logger.Error(ex);
                }
                //Check permission for button add new head line and update notification
                checkPermission();
            }

        }
    }

为gridview grvNotification方法绑定数据:

private void HeadlineSelected(string notificationIdSelected, int index)
    {
        logger.Debug("Head line selected");
        GetDataFromSessionAndCache();
        IList<InformationEntity> infoNotificationList = new List<InformationEntity>();
        if (string.IsNullOrEmpty(notificationIdSelected))
        {
            //Clear
            ClearDataAndDisplayForGridNotification();
            btnEditNotification.Enabled = false;
        }
        else
        {
            infoNotificationList = MasterDataHelper.GetInformationByHighOrderId(notificationIdSelected);
            logger.Debug("Notification list not null");
            if (infoNotificationList != null)
            {
                if (infoNotificationList.Count == 0)
                {
                    pnPageIndex.Visible = false;
                    hdfNotificationId.Value = string.Empty;
                    btnEditNotification.Enabled = false;
                }
                else
                {
                    if (infoNotificationList.Count == 1)
                    {
                        fakepanel.Visible = true;
                    }
                    else
                    {
                        fakepanel.Visible = false;
                    }
                    if (base.IsPermission(PERMISSION_INFORMATION_FOLLOWUP))
                    {
                        btnEditNotification.Enabled = true;
                    }
                    pnPageIndex.Visible = true;
                    gdvNotification.PageIndex = index;
                    hdfNotificationId.Value = infoNotificationList[index].Id;
                    gdvNotification.DataSource = infoNotificationList;
                    gdvNotification.DataBind();
                }
                logger.Debug("Set value for label total page");
                lblTotalPage.Text = gdvNotification.PageCount.ToString(CultureInfo.InvariantCulture);
                lbPageIndex.Text = gdvNotification.PageCount == 0
                                       ? gdvNotification.PageCount.ToString(CultureInfo.InvariantCulture)
                                       : (gdvNotification.PageIndex + 1).ToString(CultureInfo.InvariantCulture);
                lbTotal.Text = infoNotificationList.Count().ToString(CultureInfo.InvariantCulture);
            }
        }
    }

事件pageindex更改gridview gdvNotification:

protected void gdvNotification_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        logger.Debug("Gridview Headline page index changing .");
        //gdvNotification.PageIndex = e.NewPageIndex;
        HeadlineSelected(hdfNotificationIdSelected.Value, e.NewPageIndex);
        //gdvNotification.DataBind();
        lbPageIndex.Text = (e.NewPageIndex + 1).ToString(CultureInfo.InvariantCulture);
    }

发生了什么事?

1 个答案:

答案 0 :(得分:0)

“调用codebehide但不调用gdvNotification_PageIndexChanging事件。”

如果您正在收到回发但该函数未被调用,则Page_Load方法中的某些内容可能会阻止事件发生。你不是在重新填充GridView吗?你忘了吗

 if (!Page.IsPostBack)