1

我需要弹出确认按钮单击使用 c# 从 asp.net gridview 删除行。下面是用于从 gridview 中删除行的按钮单击代码。我需要在删除之前弹出确认。

这是代码

protected void annDelete_Click(object sender, EventArgs e)
{
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("chk_delete");

                if (cb.Checked && cb != null)
                {
                    {
                        int ID_No = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
                        MySqlCommand com = new MySqlCommand();

                        com.CommandText = "Delete from announcement where id = '" + ID_No + "'";
                        com.Connection = con;

                        //con.Open();
                        com.ExecuteNonQuery();
                        con.Close();
                        GridView1.DataSource = null;

                    }

                }

            }
            LoadIssueData();
}
4

4 回答 4

2

这适用于我在gridview中的删除按钮:html:

    <asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" 
                        AllowPaging="True" AllowSorting="True" 
                                            onpageindexchanging="gv_PageIndexChanging" 
                                            onsorting="gv_Sorting" GridLines="None" DataKeyNames="ID" 
                                               PageSize="20" onrowdeleting="gv_RowDeleting" 
                                               onrowdatabound="gv_RowDataBound" >
                        <PagerSettings FirstPageText="First" LastPageText="Last" 
                    Mode="NumericFirstLast" NextPageText="Next" PageButtonCount="5" 
                    PreviousPageText="Previous" />

                        <Columns>
                            <asp:BoundField DataField="SECURITIES_NAME" HeaderText="Securities Name" 
                                SortExpression="SECURITIES" />

                            <asp:BoundField DataField="FIN_YEAR" HeaderText="Financial Year" 
                                SortExpression="FIN_YEAR" />
                            <asp:BoundField DataField="RECORD_DATE" HeaderText="Record Date" 
                                SortExpression="RECORD_DATE" />
                            <asp:BoundField DataField="AGM_DATE" HeaderText="AGM Date" 
                                SortExpression="AGM_DATE" />
                            <asp:BoundField DataField="CA_SEQ_NO" HeaderText="CA SEQ NO" 
                                SortExpression="CA_SEQ_NO" />
                            <asp:TemplateField HeaderText="Action">
                                <ItemTemplate>
                                    <div class="actions">
                                        <asp:LinkButton ID="lbView" runat="server" CausesValidation="True" CommandName="Select" Text="View"></asp:LinkButton>
                                        <asp:LinkButton ID="lbEdit" runat="server" CausesValidation="True" CommandName="Edit" Text="Edit"></asp:LinkButton>
                                        <asp:LinkButton ID="lbDelete" runat="server" OnClientClick="return confirm('Are you sure want to delete the Corporate Action?')" CausesValidation="False" 
                                            CommandName="Delete" Text="Delete"></asp:LinkButton>
                                    </div>
                                </ItemTemplate>
                            </asp:TemplateField>            
                        </Columns>
                        <PagerStyle CssClass="pagination-flickr" BorderStyle="None" />
                        <HeaderStyle BorderStyle="None" />
                        <AlternatingRowStyle CssClass="altrow" />
                    </asp:GridView> 
the codebehind cs:
protected void Page_Load(object sender, EventArgs e)
    {
        IsAuthenticated.CheckSession();
        if (!IsPostBack)
        {
            if (Session["setFlash"] != null)
            {
                if (Session["setFlash"].ToString() == "1")
                {
                    ltrMsg.Text = @"<div class='message-green'>Save Successfully</div>";
                    Session["setFlash"] = null;
                }
                else
                {
                    ltrMsg.Text = @"<div class='message'>Not Save Successfully</div>";
                    Session["setFlash"] = null;
                }
            }

            common.gv(@"Select SECURITIES_ID,BO_FNC_GET_SECURITIES_NAME(SECURITIES_ID) SECURITIES_NAME,
                CA_SEQ_NO,FIN_YEAR,RECORD_DATE,AGM_DATE,SETUP_DATE,ID,ROWID From CA_MST", gv);
            ViewState["sortingOrder"] = string.Empty;
            DataBindGrid("", "");  

        }
    }
    protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string wh = "ID='" + gv.DataKeys[e.RowIndex].Values[0].ToString() + "'";
        DataProcess.Save("CA_MST", wh);

        common.gv(@"Select SECURITIES_ID,BO_FNC_GET_SECURITIES_NAME(SECURITIES_ID) SECURITIES_NAME,
                CA_SEQ_NO,FIN_YEAR,RECORD_DATE,AGM_DATE,SETUP_DATE,ID,ROWID FROM CA_MST", gv);
    }

    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv.PageIndex = e.NewPageIndex;
        common.gv(@"Select SECURITIES_ID,BO_FNC_GET_SECURITIES_NAME(SECURITIES_ID) SECURITIES_NAME,
                CA_SEQ_NO,FIN_YEAR,RECORD_DATE,AGM_DATE,SETUP_DATE,ID,ROWID FROM CA_MST", gv);

    }
    private void DataBindGrid(string sortExpr, string sortOrder)
    {
        // GetDataTable returns a filled table
        DataTable dt = DataProcess.getDataTable(@"Select SECURITIES_ID,BO_FNC_GET_SECURITIES_NAME(SECURITIES_ID) SECURITIES_NAME,
                CA_SEQ_NO,FIN_YEAR,RECORD_DATE,AGM_DATE,SETUP_DATE,ID,ROWID FROM CA_MST");
        // any check to validate dt
        if (dt != null)
        {
            DataView dv = dt.DefaultView;

            if (sortExpr != string.Empty)
                dv.Sort = sortExpr + " " + sortOrder;

            this.gv.DataSource = dv;
            this.gv.DataBind();
        }
        else
        {
            this.gv.DataSource = null;
            this.gv.DataBind();
        }
    }
    protected void gv_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataBindGrid(e.SortExpression, sortingOrder);
    }
    public string sortingOrder
    {
        get
        {
            if (ViewState["sortingOrder"].ToString() == "desc")
                ViewState["sortingOrder"] = "asc";
            else
                ViewState["sortingOrder"] = "desc";

            return ViewState["sortingOrder"].ToString();
        }
        set
        {
            ViewState["sortingOrder"] = value;
        }
    }
    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lbv = (LinkButton)e.Row.FindControl("lbView");
            lbv.PostBackUrl = "CaMstVw.aspx?rid=" + gv.DataKeys[e.Row.RowIndex].Values[0].ToString();

            LinkButton lb = (LinkButton)e.Row.FindControl("lbEdit");
            lb.PostBackUrl = "CaMstIns.aspx?rid=" + gv.DataKeys[e.Row.RowIndex].Values[0].ToString() ;
        }
    }
于 2013-07-15T09:00:21.300 回答
1

假设 annDelete 是一个 asp.net 按钮控件,您可以输入以下代码来询问客户端确认:

HTML:

<asp:Button ID="annDelete" OnClientClick="if(!confirm('Confirm delete?')) return false;"></asp:Button>

return false将避免服务器端命中。

于 2013-07-15T08:51:09.623 回答
0

假设您有一个<asp:Button>控件。

只需添加如下OnClientClick事件:

<asp:Button id="btnDelete" OnClick="annDelete_Click"
            OnClientClick="return confirm('Are you sure?');" />

confirmjavascript调用将打开一个警报OK / Canceltrue如果用户按下OKfalse则返回,否则您将返回按钮。

仅当您返回时,该按钮才会继续调用该__Post事件true

于 2013-07-15T08:49:30.433 回答
0

您可以添加 OnClientClick 属性:

<asp:Button ID="annDelete" OnClientClick="return confirm('do you really want to delete?');" runat="server" Text="Button" OnClick="annDelete_Click" />

如果用户选择ok,则确认函数将返回true并调用 __doPostBack() 函数,否则 - 不。

于 2013-07-15T08:52:28.813 回答