弹出确认

时间:2013-07-15 08:44:47

标签: c# asp.net gridview

我需要在按钮上弹出确认点击使用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 个答案:

答案 0 :(得分: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() ;
        }
    }

答案 1 :(得分:1)

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

HTML:

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

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

答案 2 :(得分:0)

我们假设你有一个<asp:Button>控件。

只需添加OnClientClick事件,例如:

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

confirm javascript调用将打开OK / Cancel警报,如果用户按下true,则会返回OK,否则您将返回按钮。< / p>

只有在您返回__Post时,该按钮才会继续调用true事件。

答案 3 :(得分: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()函数,否则 - 不会。