【问题标题】:LinkButton in gridview to open modal at the position the scroll is in but it refreshes page back to the top and opens modal andgridview 中的 LinkBut​​ton 在滚动所在的位置打开模式,但它将页面刷新到顶部并打开模式和
【发布时间】:2019-11-26 01:12:36
【问题描述】:

我有一个 gridview,它有一个包含链接按钮的模板字段。当用户单击此链接按钮时,我正在调用一个 OnClick 事件 DisplayModal,该事件运行一个方法以将动态值从数据库获取到数据表以绑定到模态网格视图。此方法运行 ClientScript.RegisterStartupScript 以调用显示模态的 jquery 函数 openModal()。这很好用。

我在页面上有三个网格视图。当我向下滚动第二个 gridview 并单击链接按钮时,模式会以模式上的正确值打开,但它会刷新页面,因此滚动条回到顶部,而不是我单击 gridview 然后打开模式的位置。

我想阻止它刷新页面。在模式之后,我在 gridview 和 PostBackTrigger 标记周围有一个更新面板。

    protected void DisplayModal(object sender, EventArgs e)
{



    int rowIndex = Convert.ToInt32(((sender as LinkButton).NamingContainer as GridViewRow).RowIndex);
    GridViewRow row = gvTeamCAOTDashboard.Rows[rowIndex];

    row.Focus();

    MeasurecodeClicked = (row.FindControl("lnkBtnEdit") as LinkButton).Text;
    GetKPICriteria(MeasurecodeClicked); //calls sql method to bind data

    H1.InnerText = "KPI Criteria - " + MeasurecodeClicked;

    ClientScript.RegisterStartupScript(this.GetType(), "Pop", "openModal();", true);

}
  function openModal() {

        $("#myModal").modal('show');

    }

<div class="table-responsive">
                   <asp:updatepanel id="Updatepanel1" runat="server">
               <ContentTemplate>
           <asp:GridView ID="gvTeamCAOTDashboard" class="table table-bordered" Width="100%" DataKeyNames="ControlDesc" runat="server" CellPadding="0" AutoGenerateColumns="false" OnRowDataBound="gvTeamCAOTDashboard_RowDataBound" HeaderStyle-HorizontalAlign="Center">
               <Columns>


                      <asp:BoundField DataField="IsWorkingDay" HeaderText="IsWorkingDay" ReadOnly="true" SortExpression="IsWorkingDay" ItemStyle-CssClass="Hide" HeaderStyle-CssClass="Hide"  >
                          <ItemStyle CssClass="Hide" />
                 </asp:BoundField>



                             <asp:TemplateField HeaderText=""  ItemStyle-HorizontalAlign ="Center" > 
                    <ItemTemplate>

                        <asp:LinkButton ID="lnkBtnEdit"  ForeColor="Black" runat="server" Text='<%# Eval("MeasureCode") %>' 
                            OnClick="DisplayModal" ></asp:LinkButton>

                </ItemTemplate>
                </asp:TemplateField>




                        <asp:TemplateField HeaderText="Total" SortExpression="TotalCases" ItemStyle-HorizontalAlign ="Center" ControlStyle-ForeColor="Black">
                           <ItemTemplate>
                              <asp:HyperLink ID="TotalCases"  runat="server" Text='<%# Eval("TotalCases") %>' />
                           </ItemTemplate>
                        </asp:TemplateField>

                            <asp:BoundField DataField="Target" HeaderText="Standard" ReadOnly="true" SortExpression="Target" ItemStyle-HorizontalAlign ="Center" >
                           <HeaderStyle />
                        </asp:BoundField>

                   <asp:BoundField DataField="AvgDaysTaken" HeaderText="Average" ReadOnly="true" SortExpression="AvgDaysTaken" ItemStyle-HorizontalAlign ="Center" >
                           <HeaderStyle />
                        </asp:BoundField>

               </Columns>
           </asp:GridView>


                                  <!-- Modal -->
<div class="modal fade bd-example-modal-lg" id="myModalTeam" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" runat="server" id="H1"></h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
          <asp:GridView ID="gvModalTeam" runat="server">


                                </asp:GridView>
      </div>
      <div class="modal-footer">
        <button type="button" id="closebutton" runat="server" class="btn btn-secondary" data-dismiss="modal">Close</button>

      </div>
    </div>
  </div>
</div>



                </ContentTemplate>
                  <Triggers >
      <asp:PostBackTrigger ControlID="gvTeamCAOTDashboard"  />
</Triggers>

            </asp:updatepanel> 

           </div>

【问题讨论】:

  • 你应该尝试在 openModal() 函数中使用 e.PreventDefault 吗?!
  • 函数 openModal() { $("#myModal").modal('show'); e.preventdefault();这不起作用 - 仍然刷新页面。这是正确的吗?
  • 你能不能把两行交换一下放在开头?
  • 它现在根本不显示模态

标签: javascript asp.net webforms bootstrap-modal postback


【解决方案1】:

如果您在浏览器中检查生成的 html,您会发现一个 标记,其 id 为 lnkBtnEdit,属性为“href="#”。正如所讨论的here你可以找到类似的问题。

尝试将 href="#/" 添加到链接按钮

【讨论】:

  • 这个问题是我需要调用 DisplayModal 方法,因为它有一些 sql 连接来获取绑定到模式主体中 gridview gvModalTeam 的数据。这只会打开一个内部没有数据的模式。目前我使用onclick调用asp方法
  • 你没有提到限制。代码更新了。您能否尝试手动添加 href="#/"
  • 不调用onclick方法(不出现模态)但URL为localhost:63705/Dashboard.aspx# ​​ton>
  • 更新了 DisplayModal c# 方法的代码 - 添加 row.Focus() 仍然不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
  • 2014-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多