【问题标题】:ASP.NET(4.5) GridView Paging HTML Markup Missing href LinksASP.NET(4.5) GridView 分页 HTML 标记缺少 href 链接
【发布时间】:2015-09-11 16:33:28
【问题描述】:

我在我的 ASP.Net WebForms 应用程序的一个页面上有一个正常工作的asp:GridView,在另一个无法工作的页面上有一个非常相似的工作。问题在于分页;我在asp:GridView 控件上有AllowPaging=true,并在代码隐藏中正确设置了OnPageIndexChanging="searchResultsGridView_PageIndexChanging" 方法。运行时,分页显示正确(即使在PagerStyle 上使用我的自定义css),但后续页面的编号链接按钮在生成的标记中没有设置任何href=""

这是生成的标记:

<tr align="center" class="gridViewPager">
        <td colspan="10">
          <table>
              <tbody>
                <tr>
                  <td>
                    <span>1</span>
                  </td>
                  <td>
                    <a>2</a>
                  </td>
                  <td>
                    <a>3</a>
                  </td>
                  <td>
                    <a>4</a>
                  </td>
                  <td>
                    <a>5</a>
                  </td>
                  <td>
                    <a>6</a>
                  </td>
                  <td>
                    <a>7</a>
                  </td>
                </tr>
              </tbody>
          </table>
        </td>
</tr>

显然,每个&lt;td&gt;&lt;a&gt; 标签上都应该有一个href="" 属性,但没有。以下是页面中的标记,该页面具有正常工作的分页 asp:GridView

<td><a href="javascript:__doPostBack('ctl00$ModalPanelContentPlaceholder$designerSearchResultsGridView','Page$2')">2</a></td>

这是我的 .aspx 代码:

<div id="searchResultsTableGridViewDivContainer" runat="server">
        <asp:GridView CssClass="table table-hover table-bordered" ID="searchResultsGridView"
            AutoGenerateColumns="false" SelectedRowStyle-CssClass="info"
            OnRowDataBound="searchResultsGridView_RowDataBound"
            OnSelectedIndexChanged="searchResultsGridView_SelectedIndexChanged" AllowPaging="true"
            PageSize="15" OnPageIndexChanging="searchResultsGridView_PageIndexChanging"
            runat="server">
            <Columns>
                <asp:BoundField HeaderText="Tracking #" DataField="TrackingNumber" />
                <asp:BoundField HeaderText="Product Type" DataField="ProductType.TypeName" />
                <asp:BoundField HeaderText="Design Type" DataField="DesignType.TypeName" />
                <asp:BoundField HeaderText="PWA #" DataField="PWAProductNumber" />
                <asp:BoundField HeaderText="PWB #" DataField="PWBProductNumber" />
                <asp:BoundField HeaderText="Rev" DataField="Revision" />
                <asp:BoundField HeaderText="Designers" DataField="DesignersCSV" />
                <asp:BoundField HeaderText="Status" DataField="Status.StatusName" />
                <asp:BoundField HeaderText="# Times Released" DataField="NumTimesReleased" />
                <asp:BoundField HeaderText="Queue Date" DataField="FormattedQueueDate" />
            </Columns>
            <PagerStyle HorizontalAlign="Center" CssClass="gridViewPager" />
        </asp:GridView>
    </div>

还有我的相关代码隐藏:

    /// <summary>
    /// EVENT HANDLER: Handles the RowDataBound event of the customerSearchResultsGridView control.
    /// 
    /// Allow the entire row to be clicked as a "Selector".
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="GridViewRowEventArgs"/> instance containing the event data.</param>
    protected void searchResultsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onclick"] =
                ClientScript.GetPostBackClientHyperlink(searchResultsGridView, "Select$" + e.Row.RowIndex);
        }
    }

    protected void searchResultsGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        aboutProductBtnPanel.Enabled = (searchResultsGridView.SelectedRow == null) ? false : true;
    }

    /// <summary>
    /// EVENT HANDLER: Handles the PageIndexChanging event of the searchResultsGridView control.
    /// 
    /// Increment the next page of the GridView for results > 15.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="GridViewPageEventArgs"/> instance containing the event data.</param>
    protected void searchResultsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        searchResultsGridView.PageIndex = e.NewPageIndex;

        // don't refetch the data, just get it from the session
        searchResultsGridView.DataSource = SessionWrapper.Current.CaptureProductSearchResults;
        searchResultsGridView.DataBind();

        // new page, clear the selection
        searchResultsGridView.SelectedIndex = -1;
        aboutProductBtnPanel.Enabled = false;
    }

知道为什么自动生成的 HTML 标记无法添加 href 链接吗?

【问题讨论】:

  • 只是为了比较,你能不能也发布工作中的 GridView 的标记?
  • 您的searchFormView 对象是什么?您对 searchResultsGridView 的所有其他引用都没有此前缀。您可以尝试在此行删除它吗:searchFormView.SearchResultsGridView.DataBind();
  • 啊,@gotmilk13531,因为我的应用程序遵循 MVP 设计模式,所以我最初在外部演示者类中拥有 searchResultsGridView_PageIndexChanging() 方法的内容。为了简化显示的代码,我将外部演示者的帮助方法的内容从代码隐藏复制到事件方法中,但忘记摆脱演示者拥有的视图的句柄。很抱歉造成混乱,但searchFormView 不应该也不会涉及该问题。此后我更新了代码以反映这一点。
  • @Andrei,与 javascript:__doPostBack(... 一起显示的 HTML 标记直接来自正确工作的 GridView 标记。

标签: c# html asp.net gridview


【解决方案1】:

事实证明,问题在于设置searchResultsGridView.Enabled = false; 的杂散线。因此,ASP.NET 仍然会导致页面的标记被呈现,但要“禁用”asp:GridView,它只是巧妙地省略了每个&lt;a&gt; 标记的href=""

哎呀,浪费了几个小时。感谢您的宝贵时间,@gotmilk13531 和 @Andrei。

【讨论】:

    猜你喜欢
    • 2020-12-24
    • 1970-01-01
    • 2016-09-14
    • 2022-12-23
    • 2014-04-17
    • 2017-09-14
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    相关资源
    最近更新 更多