【问题标题】:GridView TemplateField Confirmation BoxGridView TemplateField 确认框
【发布时间】:2018-04-03 04:16:22
【问题描述】:

我正在使用带有超链接的 gridview 进行回发以删除一些数据,这是我的代码

<asp:GridView ID="downloadFilesTable" runat="server" BorderStyle="Solid"
    AutoGenerateColumns="False" DataKeyNames="DOCID" 
    DataSourceID="SqlDataSource1" 
    OnSelectedIndexChanged="downloadFilesTable_SelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="DOCID" HeaderText="DOCID" InsertVisible="False" ReadOnly="True" SortExpression="DOCID" />
        <asp:BoundField DataField="FILENAME" HeaderText="FILENAME" SortExpression="FILENAME" />
        <asp:BoundField DataField="EXTENSION" HeaderText="EXTENSION" SortExpression="EXTENSION" />
        <asp:HyperLinkField  
            DataNavigateUrlFields="DOCID"
            DataNavigateUrlFormatString="DownloadFile.aspx?id={0}"
            HeaderText="File" 
            Text="Open" />
        <asp:TemplateField HeaderText="File">
            <ItemTemplate>
                <asp:HyperLink runat="server" 
                    NavigateUrl='<%# "~/ViewDocument.aspx?table="
                        + Request.QueryString["table"].ToString() 
                        + "&id=" + Request.QueryString["id"].ToString() 
                        + "&docid=" + Request.QueryString["docid"].ToString() 
                        + "&remove=" + Server.UrlEncode(Eval("docid").ToString())%>' 
                    datanavigateurlfields="docid" 
                    HeaderText="File" 
                    Text="Remove" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

用户将单击 ItemTemplate 超链接以删除数据,但问题是,当我想删除数据时,我想要一个确认框,如果用户单击是,我将删除数据,这是一个 gridview我认为我无法访问 onclick 功能,有什么线索吗? (例如。在服务器端做什么?)非常感谢每一个帮助,在此先感谢!

【问题讨论】:

    标签: javascript asp.net confirmation


    【解决方案1】:

    每次加载网格时,它一次加载一行,并且有一个您可以使用的事件,称为RowDataBound

    将您的控件更改为 LinkBut​​ton,因为它具有 OnClientClick 事件。

    protected void gvw_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow) 
        {
            // reference the Delete button. (i think it's cell 4.)
            LinkButton lbtn = (LinkButton)e.Row.Cells[4].Controls[0]; 
    
            lbtn.OnClientClick = "return confirm('Are you certain you want to delete?');"
        }
    }
    

    【讨论】:

    • 注意:我在gridview的内置删除按钮上使用了这个,所以返回“否”会自动取消删除。我认为这仍然有效,但您必须解决实际删除问题。
    • 谢谢,但是 LinkBut​​ton lbtn = (LinkBut​​ton)e.Row.Cells[4].Controls[0];将返回错误,我将其更改为 LinkBut​​ton lbtn = (LinkBut​​ton)e.Row.Cells[4].FindControls("linkbutton");
    【解决方案2】:

    更好你可以使用弹出窗口并做一些设计

      <html>
     <head runat="server">
    <title></title>
     <link href="CSS/LoginPage.css" type="text/css" rel="Stylesheet" />
    <link href="CSS/ModalPopup.css" type="text/css" rel="Stylesheet" />
    
    <style type="text/css">
        .style1
        {
            height: 139px;
        }
        .style2
        {
            width: 298px;
        }
        .style3
        {
            height: 62px;
        }
        .style4
        {
            width: 298px;
            height: 62px;
        }
        .style5
        {
            width: 572px;
        }
        .style6
        {
            width: 572px;
            height: 62px;
        }
    </style>
    <script language="javascript" type="text/javascript">
    </script>
         </head>
         <body>
          <form id="form1" runat="server">
         <table cellpadding="0" cellspacing="0" align="center" 
              style="height: 138px; width: 375px; position: absolute; top: 0px; left: 0px;">
                <tr>
                    <td  align="center" id="tdsave" runat="server" class="style1">
    
    
                                <div class="ModalDragHeader">
                                    <table style="border: thin solid #6FB1E3; width: 376px" >
                                        <tr>
                                            <td  
                                                style="font-family: arial, Helvetica, sans-serif; font-size: medium; font-weight: normal; font-style: normal; font-variant: normal; text-transform: none; color: #FFFFFF;">
                                                Bluekode</td>
                                            <td class="style5">
                                                </td>
                                            <td class="style2">
                                                &nbsp;</td>
                                        </tr>
                                        <tr>
                                            <td class="style3" >
                                                <asp:Image ID="imgmsg" runat="server" Height="45px" Width="64px" /></td>
                                            <td  
    
                                                style="font-family: arial, Helvetica, sans-serif; color: #000000; font-size: large; font-weight: normal; font-style: normal; font-variant: normal; text-transform: none;" 
                                                class="style6">
                                              <asp:Label ID="LSave" runat="server" Font-Names="arial" Font-Size="Large">You want to delete?</asp:Label></td>
                                            <td  
    
                                                style="font-family: arial, Helvetica, sans-serif; color: #000000; font-size: large; font-weight: normal; font-style: normal; font-variant: normal; text-transform: none;" 
                                                class="style4">
                                                </td>
                                        </tr>
                                        <tr>
                                            <td >
                                               </td>
                                            <td id="btnbtnok" runat="server">
    
                                                    <asp:Button ID="btnok" runat="server" CssClass="buttonL"  Text="yes" Width="60px" 
                                                        Height="24px" />
    
    
    
                                                    <asp:Button ID="btnok0" runat="server" CssClass="buttonL"  Text="no" Width="60px" 
                                                        Height="24px" />
    
                                            </td>
                                            <td >
                                               </td>
                                        </tr>
    
                                    </table>
                                </div>
    
    
                    </td>
                </tr>
            </table>
    </form>
    

    在你的屏幕上使用 session 并传递它

    在您的页面上

    <script language="javascript" type="text/javascript">
    function myFunction() {
        window.open("msgbx.aspx", "_blank", "toolbar=yes,scrollbars=no,resizable=yes,top=100,left=200,width=250,height=200");
    }
    

    在链接按钮上

            <var><button onclick="myFunction()" text="Delete"</button> </var>
    

    【讨论】:

      猜你喜欢
      • 2013-10-09
      • 2021-04-15
      • 1970-01-01
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多