【问题标题】:execute different codes in code behind on confirm ok and cancel using textbox methods and property only仅使用文本框方法和属性在确认 ok 和取消时在后面的代码中执行不同的代码
【发布时间】:2013-11-21 04:53:18
【问题描述】:

这是脚本,我想在之前使用按钮按钮的 clentclick 属性的代码中使用它 我想在不使用按钮的情况下使用此代码

<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)
    {
    }

【问题讨论】:

  • 这里到底有什么问题?
  • 文本框中的onclientclick相当于什么,不调用js代码不会运行
  • 文本框没有点击事件,它们并非设计为按钮。您将需要某种方式通过 JavaScript 或通过按钮将表单提交回服务器。 msdn.microsoft.com/en-us/library/…

标签: c# javascript jquery asp.net .net


【解决方案1】:

在页面上留下一个按钮,使用 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
    }
}

【讨论】:

  • 但是在 text_changed 事件上没有触发确认消息
  • 我的错,onclientchanged 实际上应该是 JS onchange 事件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多