避免页面刷新的GridView中LinkBut​​ton控件上,当点击 [英] Avoid page refresh when clicking on LinkButton in GridView

查看:99
本文介绍了避免页面刷新的GridView中LinkBut​​ton控件上,当点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GridView控件控件,而这个控件中我已经定义使用​​链接按钮 GridView.ItemTemplate

我使用这个打开的点击一个新窗口。当我点击了链接按钮,但是,网页打开新窗口之前刷新。

如何从清爽停止页面链接被点击按钮后?

HTML

 < ASP:的UpdatePanel ID =UpdatePanel4=服务器>
                <&的ContentTemplate GT;                                 < ASP:GridView控件ID =DataGrid1中的风格=能见度:可见=服务器AlternatingRowStyle-背景色=#E9EDF5FONT-名称=宋体
                                        前景色=#09538AFONT-SIZE =12px的背景色=#FFFFFFBORDERCOLOR =深灰字体粗体=真
                                        HeaderStyle-背景色=#298DC7的EnableViewState =假CELLSPACING =20
                                        CELLPADDING =10HeaderStyle-字体粗体=真的AutoGenerateColumns =FALSEOnRowCommand =DataGrid1__RowCommandOnRowDataBound =DataGrid1__RowDataBound>
                                        < HeaderStyle字体-名称=宋体;的CssClass =MyHeaderStyleFONT-SIZE =13px的前景色=白字体粗体=真HEIGHT =20背景色=#298DC7>< / HeaderStyle>
&所述; asp的:的TemplateField HEADERTEXT =NDCItemStyle-的CssClass =栏ItemStyle-Horizo​​ntalAlign =对齐HeaderStyle宽度=10%ItemStyle宽度=10%> <柱体和GT;
            <&ItemTemplate中GT;
            < ASP:LinkBut​​ton的ID =产品code前景色=#09538A=服务器文本='<%#的eval(产品code)%>'>< / ASP:LinkBut​​ton的>                                             < / ItemTemplate中>
                                < / ASP:的TemplateField>
           < / ASP:GridView的>
                        < /&的ContentTemplate GT;
        < / ASP:的UpdatePanel>
                                < AjaxToolkit:ModalPopupExtender ID =ModalPopupExtender1BackgroundCssClass =modalBackgroundCancelControlID =取消的TargetControlID =按钮3=服务器PopupControlID =pnlpopup> < / AjaxToolkit:ModalPopupExtender>           < ASP:按钮的ID =按钮3=服务器的风格=visibility:hidden的文本=按钮/>
                    < ASP:面板ID =pnlpopup的CssClass =PanelPopup=服务器>
                                < D​​IV的风格=宽度:继承;文本对齐:中心;>%= NDC%GT;的&LT的产品;产品系列和LT; / DIV>
                                < ASP:GridView控件ID =GridView1=服务器AlternatingRowStyle-背景色=#f1f4f8WIDTH =980pxFONT-名称=宋体
                                    字体粗体=真前景色=#09538AFONT-SIZE =13px的背景色=#FFFFFFBORDERCOLOR =深灰
                                    HeaderStyle-背景色=#99CCCC的EnableViewState =假CELLSPACING =0的风格=填充:10px的;
                                    CELLPADDING =3ShowFooter =假AllowPaging =真的AutoGenerateColumns =FALSEOnRowDataBound =productInfo_RowDataBound>
                                    < HeaderStyle高度=10%的CssClass =MyHeaderStyleFONT-SIZE =13px的前景色=#FFFFFF字体粗体=真正的背景色=#298DC7>< / HeaderStyle>
                                    < rowstyle HEIGHT =20像素/>
                < alternatingrowstyle HEIGHT =20像素/>
                                    <柱体和GT;
        < ASP:BoundField的数据字段=产品code索特克斯pression =客户ItemStyle-的CssClass =山坳HEADERTEXT =NDC/>     < D​​IV ID =DIV<%#Convert.ToString(EVAL(客户))+ Convert.ToString(EVAL(ManufacturingPartner))+ Convert.ToString(EVAL(产品code))+ Convert.ToString(EVAL(销售人员))%GT;风格=显示:无;位置:亲属;左:15px的;溢出:汽车>
                                < ASP:GridView控件ID =gvOrderInfo=服务器前景色=#09538A的AutoGenerateColumns =false的边框样式=双BORDERCOLOR =#df5015WIDTH =500px的OnRowDataBound =gvOrderInfo_RowDatabound>
                                < HeaderStyle的CssClass =MyHeaderStyleFONT-SIZE =13px的前景色=#FFFFFF字体粗体=真背景色=#298DC7>< / HeaderStyle>
                                < RowStyle的BackColor =#E1E1E1/>
                                < AlternatingRowStyle背景色=白/>
                                < HeaderStyle背景色=#df5015FONT-粗体=真前景色=白/>
                                <柱体和GT;
      < ASP:BoundField的数据字段=订单号的HeaderText =订单号ItemStyle-WIDTH =960x75像素ItemStyle-的CssClass =山坳HeaderStyle-Horizo​​ntalAlign =左/>
          < /专栏>
      < /专栏>
                                < / ASP:GridView的>
                            < / DIV>
                                    < / ASP:GridView的>
     < / ASP:面板>

codebehind

 保护无效DataGrid1__RowDataBound(对象发件人,GridViewRowEventArgs E)
{
     this.UpdatePanel4.Update();
     如果(e.Row.RowType == DataControlRowType.DataRow)
     {
          LinkBut​​ton的LNK = e.Row.FindControl(PPP)作为LinkBut​​ton的;
          lnk.Click + =新的EventHandler(link_Click);          //ScriptManager.GetCurrent(this).RegisterPostBackControl(lnk);          //字符串=的String.Format(0:N},Convert.ToDecimal(e.Row.Cells [3]。文本));
          如果(e.Row.Cells [0]。文本!=合计)
          {
                // M1-Fmodification从这里开始
                如果(ListBox2.Items.Count大于0)
                //如果(DataGrid1.Columns [0]。可见== true)而
                {
                }
          }
     }
}


解决方案

我不会用一个LinkBut​​ton,但直HTML链接。

 <&的ContentTemplate GT;
    < A HREF =#ID ='<%#的eval(产品code)%GT;'的onclick =DoSomething的();>
        <%#的eval(产品code)%GT;
    &所述; / A>
< /&的ContentTemplate GT;

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

 函数DoSomething的(产品code){
    //输入code这里打开窗口
}

另外,我使用的UpdatePanel望而却步,但是这只是我的preference。

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.

这篇关于避免页面刷新的GridView中LinkBut​​ton控件上,当点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆