简体   繁体   English

ASP.NET中的警报消息

[英]Alert message in ASP.NET

I am new to ASP.NET programming. 我是ASP.NET编程的新手。 I need to display a message if RadioButton is not clicked. 如果未单击RadioButton则需要显示一条消息。 I have already written JavaScript for the onclick event to handle single selection. 我已经为onclick事件编写了JavaScript,以处理单个选择。

My code: 我的代码:

if(rbTest.Checked == true)
{
        //my code
}
else
{
    string message = "Please select case to download";
    lnkbtnDownload.Attributes.Add("onclick", "alert('" + message + "');return false;");
}

This keeps on displaying alert message even if I have selected a radio button. 即使我选择了一个单选按钮,它也会继续显示警报消息。 Why? 为什么?

您必须检查在lnkbtnDownload的onclick处理程序中是否单击了单选按钮。

lnkbtnDownload.Attributes.Add("onclick","if(!document.getElementById('rbTest').checked))alert('message')");

You need to understand the difference between server-side code and client-side code. 您需要了解服务器端代码和客户端代码之间的区别。 Your code is written in C# which runs on the server. 您的代码是用服务器上运行的C#编写的。 Here is what I assume is happening: 我认为这是正在发生的事情:

a. 一种。 Your page renders for the first time and the user does not choose the relevant radiobutton. 您的页面首次呈现,用户未选择相关的单选按钮。 He/she then submits the page (possibly via the LinkButton). 然后,他/她提交页面(可能通过LinkBut​​ton)。

b. b。 Your page submits and the code you pasted runs on the server. 页面提交,粘贴的代码在服务器上运行。 You check if the radiobutton is checked. 您检查单选按钮是否已选中。 If not, you add an attribute to a LinkButton so that when the LinkButton is clicked, it will raise an alert. 如果没有,请向LinkBut​​ton添加一个属性,以便在单击LinkBut​​ton时将发出警报。

c. C。 Your page renders after postback with the new attribute added to the LinkButton. 回发呈现页面,并将新属性添加到LinkBut​​ton。

d. d。 On the click of the LinkButton, you get an alert with the message. 单击LinkBut​​ton时,您会收到一条带有消息的警报。 Since you have set it to return false, the page will not submit again and will keep on showing you the alert. 由于您已将其设置为返回false,因此该页面将不会再次提交,并将继续向您显示警报。

Do you see what's happening here? 你看到这里发生了什么吗? Your alert condition needs to be checked on the client itself. 您的警报条件需要在客户端上进行检查。 The snippet provided by @Phoenix should be a good starting point. @Phoenix提供的代码片段应该是一个很好的起点。

use 采用

var r = documet.getElementById("rbTest")

and then compare with r[0].checked 然后与r[0].checked

use index, because radiobutton r can be an array 使用索引,因为单选按钮r可以是数组

This is relatively ugly but would do the trick: 这比较丑陋,但是可以解决问题:

<script>
  var rbSelected = false;
</script>

<form onsubmit="if (!rbSelected) { alert('You must select something!";return false}">
 <input type="radio" name="rbTest" onclick="rbSelected=true" value=1>a
 <input type="radio" name="rbTest" onclick="rbSelected=true" value=2>b
 <input type="radio" name="rbTest" onclick="rbSelected=true" value=3>c
 <input type="submit value="Submit">
</form>

If you've got more than one radio button (which I'm hoping you do), then you really can't use getElementById without all this extra looping logic etc... This avoids the looping. 如果您有多个单选按钮(我希望您这样做),那么在没有所有这些额外循环逻辑等的情况下,您真的不能使用getElementById ...这样可以避免循环。

See example: http://jsbin.com/edoke 参见示例: http//jsbin.com/edoke

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM