C#,ASP.NET:如何在满足条件时从服务器端代码获取确认消息框 [英] C#, ASP.NET: how to get a confirm message box from server side code when a condition is met

查看:201
本文介绍了C#,ASP.NET:如何在满足条件时从服务器端代码获取确认消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的页面中有一个按钮,用于保存动态填充的gridview数据。在保存之前我想检查一些条件。 javascript函数如下:

Hi,
I have a button in my page which is used to save data of gridview which is dynamically populated. Before saving I want to check some condition. The javascript function is as follows:

<script type="text/javascript">
        function Confirm() {
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";
            if (confirm("This will completely delete the project. Are you sure?")) {
                confirm_value.value = "Yes";
            }
            else {
                confirm_value.value = "No";
            }
            document.forms[0].appendChild(confirm_value);
        }
    </script>
<asp:Button runat="server" ID="lnkBtn" onClick="lnkBtn_Click" onClientClick="Confirm()"></button>



背后的代码如下:


The code behind is as follows:

protected void lnkBtn_Click(object sender, EventArgs e)
        {
            string str=gdView.HeaderRow.Cells[8].Text;
            System.Web.UI.WebControls.TextBox txtID = (System.Web.UI.WebControls.TextBox)gdView.Rows[0].Cells[8].FindControl(str);
            if (txtID.Text != "")
            {
                string confirmValue = Request.Form["confirm_value"];
                if (confirmValue == "Yes")
                {
                    MyAlert("Yes clicked");
                }
                else
                {
                    MyAlert("No clicked");
                }
            }
            else
            {
                MyAlert("No Text found.");
            }
        }



现在问题是因为我在onClientClick事件中有函数Confirm(),当用户出现确认对话框点击实际上没有打算的按钮,而不是只有在txtID中找到一个字符串时才会出现。



我尝试了什么:



我尝试通过删除onClientClick事件来修改代码,如下所示:


Now the problem is since I have the function Confirm() in the onClientClick event, the confirm dialog appears the moment the user clicks the button which is actually not intended, instead it should appear only when a string is found in txtID.

What I have tried:

I tried to modify the code by removing the onClientClick event as follows:

<asp:Button runat="server" ID="lnkBtn" onClick="lnkBtn_Click" ></button>




protected void lnkBtn_Click(object sender, EventArgs e)
        {
            string str=gdView.HeaderRow.Cells[8].Text;
            System.Web.UI.WebControls.TextBox txtID = (System.Web.UI.WebControls.TextBox)gdView.Rows[0].Cells[8].FindControl(str);
            
            if (txtID.Text != "")
            {
                
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "confirm", "Confirm();", true);
                string confirmValue = Request.Form["confirm_value"];
                
                if (confirmValue == "Yes")
                {
                    MyAlert("Yes clicked");
                }
                else
                if (confirmValue == "No")
                {
                    MyAlert("No clicked");
                }
            }
        }



这里它也无法正常工作。 confirmValue获取值,但仅在随后单击按钮时使用它。它不传递在当前点击事件期间存储的值,而是将先前存储的值传递给if块。请帮忙找到我应该做的事情。

谢谢。


Here also it is not working properly. The confirmValue is getting the value but using it only during the subsequent click of the button. It is not passing value stored during the present click event instead the previously stored value is passed to if block. Please help to find what should I do get it right.
Thanks.

推荐答案

你需要牢记asp.net页面生命周期。您的.net代码完整运行并生成html,该html将发送给客户端进行解释,因此您无法混合服务器代码和客户端代码流,这实际上就是您想要做的。有一些关于如何让客户确认你想继续服务器端的技术



要求用户确认他们想要继续行动| ASP.NET论坛 [ ^ ]
You need to bear in mind the asp.net page life cycle. Your .net code runs in its entirety and generates html which is sent to the client to interpret so you can't mix server code and client code flow, which is effectively what you want to do. There are a few techniques on how you can get a client to confirm you want to continue with something server-side here

Asking the user to confirm that they want to continue with an action | The ASP.NET Forums[^]


这篇关于C#,ASP.NET:如何在满足条件时从服务器端代码获取确认消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆