【问题标题】:GridView onrowcommand confirm boxGridView onrow 命令确认框
【发布时间】:2014-12-22 14:06:29
【问题描述】:

我在 Gridview 中有一个 ButtonField,我正在使用 onRowCommand 来触发操作。如果用户单击“取消”,我需要 onRowCommand 事件中的确认框来返回/跳过代码

这里是grid.aspx.cs

  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string commandname = e.CommandName;

        if (commandname.Equals("atender"))
        {
              // here there are the codes...I need a confirmation box here which skip these codes if the user click in cancel

          }
       } 

这是我gridview的asp

          <asp:GridView  ID="GridView1" runat="server" CellPadding="4" 
        BorderStyle="None" BorderWidth="0px" CellSpacing="1" Width="100%" 
            GridLines="Vertical" AllowPaging="True" onrowcommand="GridView1_RowCommand" 
            onselectedindexchanged="GridView1_SelectedIndexChanged" 
            onpageindexchanging="GridView1_PageIndexChanging" 
            onrowdatabound="GridView1_RowDataBound" PageSize="5" HorizontalAlign=Left
            >
                            <PagerStyle HorizontalAlign="Center" />
                            <RowStyle CssClass="tabela_texto2" HorizontalAlign="Center" 
                                VerticalAlign="Middle" />
                <AlternatingRowStyle CssClass="tabela_texto1" />

        <Columns>
            <asp:ButtonField Text="Status" CommandName="atender" ButtonType="Button" />
            <asp:ButtonField Text="Ver no mapa" CommandName="ver" ButtonType="Button" />
        </Columns>
    </asp:GridView>

【问题讨论】:

  • 确认框需要javascript,并且必须在服务器受到点击之前打开它
  • 如何在这段代码中添加这个javascript?你能帮帮我吗?

标签: asp.net .net visual-studio


【解决方案1】:

您应该使用 Javascript 或 Jquery 在浏览器(客户端)中进行控制,以使其:
1)在ButtonField中添加一个ControlStyle-CssClass:

<asp:ButtonField ControlStyle-CssClass="botonTransaccional" Text="Status" CommandName="atender" ButtonType="Button" />


2)从http://code.jquery.com/jquery-1.11.2.min.js下载JQuery库(单个JS文件)
3) 将文件保存在应用程序的目录中(在我的 JS 文件夹中的示例中)
4)在HTML中添加引用,在客户端添加jquery功能来控制事件之后(您可以在HTML文档的Head中添加)。

   <script src="/js/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function() {
      $(".botonTransaccional").click(funtion(e) {
        if (confirm("Are you sure?") == false)
        {
          e.preventDefault();
        }  
      });
    });
  </script>
<br>

e.PreventDefault() 函数防止控件的Event Default,在按钮元素中Default 是PostBack o 提交到服务器。

【讨论】:

  • 如何在cs中添加jquery?
  • 感谢!呸呸呸
  • 好吧,espero que te sirva,no olvides marcar como solución a tu pregunta y votar si así lo 决定。
猜你喜欢
  • 1970-01-01
  • 2021-04-15
  • 2022-01-20
  • 2011-10-13
  • 1970-01-01
  • 2021-03-03
  • 1970-01-01
  • 1970-01-01
  • 2021-04-06
相关资源
最近更新 更多