仅使用textbox方法和属性在confirm ok和cancel的代码隐藏中执行不同的代码

execute different codes in code behind on confirm ok and cancel using textbox methods and property only

本文关键字:代码 隐藏 执行 cancel confirm textbox 方法 属性 ok      更新时间:2023-09-26

这是脚本,我想在前面的代码中使用它我使用了按钮的clintclick属性我想在不使用按钮的情况下使用这个代码

<script type = "text/javascript">
     function Confirm() {
         var confirm_value = document.createElement("INPUT");
         confirm_value.type = "hidden";
         confirm_value.name = "confirm_value";
         if (confirm("Do you want to save data?")) {
             confirm_value.value = "Yes";
         } else {
             confirm_value.value = "No";
         }
         document.forms[0].appendChild(confirm_value);
     }
    </script>
<asp:TextBox ID="TextBox1" runat="server"  ontextchanged="TextBox1_TextChanged"></asp:TextBox>

我还能做什么才能实现

protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
       // TextBox1.Attributes.Add("OnClientClick", "Confirm()");
        string confirmValue = Request.Form["confirm_value"];
        if (confirmValue == "Yes")
        {
            //Your logic for OK button
        }
        else
        {
            //Your logic for cancel button
        }
    }
 public void OnConfirm(object sender, EventArgs e)
    {
    }

在页面上留下一个按钮,使用css隐藏它,然后在设置值后从JavaScript调用click():

<script type = "text/javascript">
    function Confirm() {
        var confirm_value = document.createElement("INPUT");
        confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";
        if (confirm("Do you want to save data?")) {
            confirm_value.value = "Yes";
        } else {
            confirm_value.value = "No";
        }
        document.forms[0].appendChild(confirm_value);
        document.getElementById("Button1").click();
    }
</script>
<asp:TextBox ID="TextBox1" runat="server" onchange="Confirm()"></asp:TextBox>
<div style="display:none;"><asp:Button ID="Button1" ClientIDMode="static" runat="server" onclick="Button1_Clicked" /></div>

然后在你的代码后面:

protected void Button1_Clicked(object sender, EventArgs e)
{
    string confirmValue = Request.Form["confirm_value"];
    if (confirmValue == "Yes")
    {
        //Your logic for OK button
    }
    else
    {
        //Your logic for cancel button
    }
}