简体   繁体   中英

Avoid page refresh when clicking on LinkButton in GridView

I have a GridView control, and inside of this control I have defined a link button using GridView.ItemTemplate .

I am using this to open a new window on click. When I click on the link button, however, the page refreshes before opening the new window.

How can I stop the page from refreshing after the link button is clicked?

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>

Codebehind

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

I would not use a LinkButton, but a straight html link.

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

And then you can define some javascript in the page.

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

Also, I would stay away from using UpdatePanel, but that's just my preference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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