c#点击服务器端按钮实现confirm功能

说一下我接触到的两种按钮实现confirm功能

普通服务器button控件:

 

<asp:Button ID="Button1" runat="server" OnClientClick="if(!confirm('Submit form?')){return false;}" OnClick="Button1_Click" Text="Button" />

 

在元件上註冊一個OnClientClick的事件,在submit前先讓使用者確認,確認後就submit,否則就不submit。

 

GridView中的command按钮:

        protected void gvProject_RowCommand(object sender, GridViewCommandEventArgs e)

        {

            try

            {

                string[] parameters = e.CommandArgument.ToString().Split('|');

                int rowIndex = Convert.ToInt32(parameters[0]);

                bool confirmResult = false;//弹出框确认激活结果,首次进入该方法为false

                if (parameters.Length == 2)

                {

                    bool.TryParse(parameters[1], out confirmResult);

                }

                if (!confirmResult)//首次进入该方法或确认结果为否

                {

                    //激活前请用户再次确认

                    string commandArgument = e.CommandArgument.ToString() + "|true";

                    string commandProjectName = this.gvProject.Rows[rowIndex].Cells[0].Text;

                    string scriptString = @"

                                            <script>

                                                if(window.confirm('你确认要激活该项目{" + commandProjectName + @"}吗?'))

                                                {

                                                    __doPostBack('gvProject','$" + commandArgument + @"')

                                                }

                                             </script>";

                    ClientScript.RegisterClientScriptBlock(this.GetType(), "billConfirmAlert", scriptString);

                }

                else//第二次进入该方法并且确认激活

                {

                    //获取DataKeys中的projectID

                    int projectID = Convert.ToInt32(this.gvProject.DataKeys[rowIndex].Values["ProjectID"]);

                    projectController.ActivateProject(projectID, "");

                    //激活完毕后刷新列表

                    ShowNotActivatedProject();

                }

            }

            catch (Exception ex)

            {

                this.lblResult.Text = string.Format("激活操作出错:{0}", ex.Message);

            }

        }

注释很详细大家自己看吧。

 

 

你可能感兴趣的:(confirm)