避免在GridView中单击LinkButton时刷新页面

本文关键字:刷新 LinkButton 单击 GridView | 更新日期: 2023-09-27 18:14:30

我有一个GridView控件,在这个控件中我使用GridView.ItemTemplate定义了一个链接按钮。

我用这个打开一个新的窗口点击。但是,当我点击链接按钮时,在打开新窗口之前页面会刷新。

单击链接按钮后,如何阻止页面刷新?

      <asp:UpdatePanel ID="UpdatePanel4" runat="server">
                <ContentTemplate>
                                 <asp:GridView ID="DataGrid1"  style="visibility:visible" runat="server" AlternatingRowStyle-BackColor="#E9EDF5"  Font-Names="Arial"
                                        ForeColor="#09538A" Font-Size="12px" BackColor="#ffffff" BorderColor="DarkGray" Font-Bold="true" 
                                        HeaderStyle-BackColor="#298DC7" EnableViewState="false"  CellSpacing="20" 
                                        CellPadding="10"  HeaderStyle-Font-Bold="true"  AutoGenerateColumns="False" OnRowCommand="DataGrid1__RowCommand" OnRowDataBound="DataGrid1__RowDataBound" >
                                        <HeaderStyle Font-Names="Arial;" CssClass="MyHeaderStyle" Font-Size="13px" ForeColor="White" Font-Bold="True" Height="20"  BackColor="#298DC7" ></HeaderStyle>
<asp:templatefield headertext="NDC" ItemStyle-CssClass="col" ItemStyle-HorizontalAlign="Justify" HeaderStyle-Width="10%" ItemStyle-Width="10%">                                     <Columns>               
            <itemtemplate>
            <asp:linkbutton id="productcode"   ForeColor="#09538A" runat="server"  text='<%#Eval("product code")%>'></asp:linkbutton>                               
                                             </itemtemplate>
                                </asp:templatefield>
           </asp:GridView>
                        </ContentTemplate>
        </asp:UpdatePanel>

                                <AjaxToolkit:ModalPopupExtender ID="ModalPopupExtender1"   BackgroundCssClass="modalBackground" CancelControlID="cancel" TargetControlID="Button3" runat="server" PopupControlID="pnlpopup"> </AjaxToolkit:ModalPopupExtender>
           <asp:Button ID="Button3" runat="server" style="visibility:hidden" Text="Button" />
                    <asp:Panel ID="pnlpopup" CssClass="PanelPopup"  runat="server">
                                <div  style="width:inherit;text-align:center;">Products of <%=ndc %> Product Family</div>
                                <asp:GridView ID="GridView1"  runat="server" AlternatingRowStyle-BackColor="#f1f4f8"  Width="980px" Font-Names="Arial"
                                    Font-Bold="True" ForeColor="#09538A" Font-Size="13px" BackColor="#ffffff" BorderColor="DarkGray"
                                    HeaderStyle-BackColor="#99cccc" EnableViewState="false"  CellSpacing="0" style="padding:10px;"
                                    CellPadding="3" ShowFooter="false" AllowPaging="True" AutoGenerateColumns="False" OnRowDataBound="productInfo_RowDataBound" >
                                    <HeaderStyle Height="10%" CssClass="MyHeaderStyle" Font-Size="13px" ForeColor="#FFFFFF" Font-Bold="true"  BackColor="#298DC7"></HeaderStyle>
                                    <rowstyle Height="20px" />
                <alternatingrowstyle  Height="20px"/>
                                    <Columns>
        <asp:boundfield  datafield="product code" sortexpression="customers " ItemStyle-CssClass="col" headertext="NDC"/>
     <div id="div<%#  Convert.ToString(Eval("customer"))+ Convert.ToString(Eval("ManufacturingPartner"))+ Convert.ToString(Eval("product code"))+ Convert.ToString(Eval("Sales Person")) %>" style="display: none; position: relative; left: 15px; overflow: auto">
                                <asp:GridView ID="gvOrderInfo" runat="server" ForeColor="#09538A"  AutoGenerateColumns="false" BorderStyle="Double"  BorderColor="#df5015"  Width="500px" OnRowDataBound="gvOrderInfo_RowDatabound">
                                <HeaderStyle CssClass="MyHeaderStyle"  Font-Size="13px" ForeColor="#FFFFFF" Font-Bold="True"  BackColor="#298DC7"></HeaderStyle>
                                <RowStyle BackColor="#E1E1E1" />
                                <AlternatingRowStyle BackColor="White" />
                                <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
                                <Columns>
      <asp:BoundField DataField="Order Number" HeaderText="Order Number" ItemStyle-Width="75px" ItemStyle-CssClass="col" HeaderStyle-HorizontalAlign="Left" />
          </Columns>   
      </Columns>                            
                                </asp:GridView>
                            </div>                         
                                    </asp:GridView>
     </asp:Panel>
后台代码>

protected void DataGrid1__RowDataBound(Object sender, GridViewRowEventArgs e)
{    
     this.UpdatePanel4.Update();
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
          LinkButton lnk = e.Row.FindControl("ppp") as LinkButton;
          lnk.Click += new EventHandler(link_Click);
          //ScriptManager.GetCurrent(this).RegisterPostBackControl(lnk); 
          // string a = String.Format("0:N}",Convert.ToDecimal(e.Row.Cells[3].Text));
          if (e.Row.Cells[0].Text != "Total")
          {
                //M1-Fmodification starts from here
                if (ListBox2.Items.Count > 0)
                //if (DataGrid1.Columns[0].Visible == true)
                {
                }              
          }
     }
}

避免在GridView中单击LinkButton时刷新页面

我不会使用LinkButton,而是直接使用html链接。

<ContentTemplate>
    <a href="#" ID='<%#Eval("product code")%>' onclick="doSomething();">
        <%#Eval("productcode")%>
    </a>
</ContentTemplate>

然后你可以在页面中定义一些javascript。

function doSomething(ProductCode) {
    // enter the code here to open the window
}

另外,我会远离使用UpdatePanel,但这只是我的偏好。