【问题标题】:How to determine which button was clicked in gridview如何确定在gridview中单击了哪个按钮
【发布时间】:2014-03-12 09:24:59
【问题描述】:

我有一个 gridview,我的 gridview 有一个用于图像按钮的模板字段,用于删除一行。 目前,我为此图像按钮使用 rowcommand 和 rowargument 来识别要删除的行。但我需要实现以下场景:

我希望当用户单击图像按钮时,不发生回发,打开模式框(引导模式),并且仅当用户单击“是”时,我确定模式中的按钮,回发发生并删除数据...

这可能与asp.net gridview,如果是的话,我怎么能确定在“是的我确定按钮”点击哪个按钮???

谢谢

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    是的,这是可能的。请尝试以下代码:

    <script type="text/javascript">
    
        function ShowModal(gridItemIndex) 
        {
            $(document).ready(function () 
                {
                    $('#mymodal').modal('show');
                    var row = gridItemIndex.parentNode.parentNode;
                    var rowIndex = row.rowIndex-1; //row index of that row.
                    document.getElementById("<%=hfID.ClientID %>").value =rowIndex;
                });
        }
    
    </script>
    
    <asp:gridview id="yourGridView" onrowcommand="GridView_RowCommand"
        <asp:TemplateField>
            <ItemTemplate>
                <asp:ImageButton name="btnDelete" CommandName="Delete" OnClientClick="return ShowModal(this);" ImageUrl="/imagepath.pgn" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </asp:gridview>
    

    然后在后面的代码中使用GridView的以下事件。

    protected void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName=="Delete")
        {
            // Write your delete code here...
        }
    }
    

    【讨论】:

    • 谢谢,但是我想用bootstrap modal,有什么办法吗?
    • 是的,有办法。您可以在其中创建一个带有引导代码的 javascript 函数。然后像这样调用这个javascript函数: OnClientClick='return yourJavascriptFunction();'
    • 好的,但是如何在模态确认按钮事件中传递gridview rowindex?
    • 我更新了我的答案。 将给出 Grid 项目索引。
    • 它不起作用:(。我写了一个javascript函数并传递了 ,但它传递了这个 () 作为字符串而不是行索引
    猜你喜欢
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多