繁体   English   中英

在GridView中单击LinkBut​​ton时避免页面刷新

[英]Avoid page refresh when clicking on LinkButton in GridView

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

我用它来在点击时打开一个新窗口。 但是,当我单击链接按钮时,页面会刷新,然后再打开新窗口。

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

HTML

      <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)
                {
                }              
          }
     }
}

我不会使用LinkBut​​ton,而是直接使用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,但这只是我的偏爱。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM