使用 JavaScript 在 ASP.NET 中显示确认框

发布于 2024-08-20 19:19:29 字数 57 浏览 4 评论 0原文

我需要显示确认框“您确定要继续吗?”如果“是”,我需要清除 ASP.NET 文本框值。否则不应清除。

I need to show the confirm box "Are you sure You Want To continue?" If "Yes" I need the ASP.NET textbox value to be cleared out. Otherwise it should not be cleared.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

分开我的手 2024-08-27 19:19:29

在您的 asp 文本框标记中添加以下内容:

OnClientClick="javascript:testDeleteValue();"

...
并添加此脚本:

<script>
function testDeleteValue()
{
   if (window.confirm('Are you sure You Want To continue?'))
      document.getElementById("<%=<th id of your textbox>.ClientID%>").value = '';
}  
</script>

如果您希望在单击单选框时发生这种情况,请将其放入此标记中,然后将 onclientclick 替换为 onclick。

<input type='radio' onclick='testDeleteValue()'/>

In your asp textbox tag add this:

OnClientClick="javascript:testDeleteValue();"

...
And add this script:

<script>
function testDeleteValue()
{
   if (window.confirm('Are you sure You Want To continue?'))
      document.getElementById("<%=<th id of your textbox>.ClientID%>").value = '';
}  
</script>

If you want this to happen on click of your radio box, put it in this tag and just replace onclientclick with onclick.

<input type='radio' onclick='testDeleteValue()'/>
万水千山粽是情ミ 2024-08-27 19:19:29
function doConfirm(){
  if (confirm("Are you sure you want to continue?")){
     var mytxtbox = document.getElementById('<% =myAspTextBox.ClientID %>');
     mytxtbox.value = '';
  }    

}

注意 myAspTextBox 指的是 asp:textbox 控件 ID 属性的名称

<asp:textbox ID="myAspTextBox" runat="server" OnClientClick="javascript:doConfirm();"

希望这有帮助

function doConfirm(){
  if (confirm("Are you sure you want to continue?")){
     var mytxtbox = document.getElementById('<% =myAspTextBox.ClientID %>');
     mytxtbox.value = '';
  }    

}

Note the myAspTextBox refers to the name of the asp:textbox controls ID property

<asp:textbox ID="myAspTextBox" runat="server" OnClientClick="javascript:doConfirm();"

Hope this helps

怎言笑 2024-08-27 19:19:29

如果您下载AjaxControlToolkit,您可以使用ConfirmButtonExtender在按钮后向用户显示一个简单的确认框单击可继续操作或取消

您可以在此处查看 获取示例,此处获取有关如何实现此功能的教程

好吧,我刚刚注意到了这一点关于单选按钮,无论如何,如果您想在 .Net 项目中实现 JavaScript 解决方案,AjaxControlToolkit 都是一个不错的起点

If you download the AjaxControlToolkit you can use the ConfirmButtonExtender to display a simple confirmation box to a user after a button is clicked to proceed with the action or cancel

You can see here for an example and here for a tutorial on how to implement this

Okay I just noticed the bit about radio buttons, in any case the AjaxControlToolkit is a good place to start if you want to implement JavaScript solutions in .Net projects

埋情葬爱 2024-08-27 19:19:29

如果这是您的文本框标记:

<asp:textbox id="txtInput" runat="server" />

然后这是将触发确认的按钮:

<asp:button id="btnSumbit" runat="server" onclientclick="return clearOnConfirm();" text="Submit" />

那么您将需要以下 javascript:

<script type="text/javascript">
function clearOnConfirm() {
    if (confirm("Are you sure you want to continue?")) {
        document.getElementById("<%=txtInput.ClientID %>").value = '';
        return true;
    } else {
        return false;
    }
}
</script>

如果您只想清除文本框但始终继续回发,那么您不需要曾经需要像上面一样返回 false,但总是像下面一样返回 true。在这种情况下,您应该重新考虑向用户显示的消息。

<script type="text/javascript">
function clearOnConfirm() {
    if (confirm("Are you sure you want to continue?")) {
        document.getElementById("<%=txtInput.ClientID %>").value = '';
    } 
    return true;
}
</script>

if this is your textbox markup:

<asp:textbox id="txtInput" runat="server" />

and then this is the button that will trigger the confirm:

<asp:button id="btnSumbit" runat="server" onclientclick="return clearOnConfirm();" text="Submit" />

then you'll need the following javascript:

<script type="text/javascript">
function clearOnConfirm() {
    if (confirm("Are you sure you want to continue?")) {
        document.getElementById("<%=txtInput.ClientID %>").value = '';
        return true;
    } else {
        return false;
    }
}
</script>

If all you want to do is to clear the textbox but always continue with the postback then you don't ever need to return false as above but always return true as below. In this scenario you should rethink the message you display to the user.

<script type="text/javascript">
function clearOnConfirm() {
    if (confirm("Are you sure you want to continue?")) {
        document.getElementById("<%=txtInput.ClientID %>").value = '';
    } 
    return true;
}
</script>
那片花海 2024-08-27 19:19:29
function stopTimer() {        
if (window.confirm('Are you sure You Want To continue?')) {
$find('Timer1')._stopTimer()
return true;
}
else {
return false;
}

<asp:Button ID="Btn_Finish" runat="server" Text="Finish" Width="113px" OnClick="Btn_Finish_Click" OnClientClick="return stopTimer();" Height="35px" 

protected void Btn_Finish_Click(object sender, EventArgs e)
{
Timer1.Enabled = false;
// if any functions to be done eg: function1();
Response.Redirect("~/Default2.aspx");
}

函数中还有一个定时器停止做的事情。如果按“确定”,确认框计时器将停止,并且它会重定向到新页面“Default2.aspx”,

否则如果选择取消,则不会发生任何情况。

function stopTimer() {        
if (window.confirm('Are you sure You Want To continue?')) {
$find('Timer1')._stopTimer()
return true;
}
else {
return false;
}

<asp:Button ID="Btn_Finish" runat="server" Text="Finish" Width="113px" OnClick="Btn_Finish_Click" OnClientClick="return stopTimer();" Height="35px" 

protected void Btn_Finish_Click(object sender, EventArgs e)
{
Timer1.Enabled = false;
// if any functions to be done eg: function1();
Response.Redirect("~/Default2.aspx");
}

There is also a timer stop doing in the function. The confirmation box if press "Ok" timer stops and also its redirected to new page "Default2.aspx"

else if chosen cancel then nothing happens.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文