需要C#代码以在单击按钮时显示警报框 [英] Need C# code to display an Alert box on button click

查看:108
本文介绍了需要C#代码以在单击按钮时显示警报框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是.NET Framework 1.1,单击保存"按钮时,我需要C#代码才能通过Javascript警报框显示成功消息框.我不希望该页面在单击按钮时重定向到另一个页面.而且页面不应该刷新.

更多信息:我正在使用DropdownList和一个Button.在DropdownList中进行选择后,当单击Button时,将该值保存到表中.单击按钮时,我已经对DropdownList进行了验证检查.

我的按钮单击事件处理程序的末尾有以下代码.

I am using .NET Framework 1.1 and I need C# code to display a success message-box via a Javascript Alert box when the Save button is clicked. I do not want the Page to redirect to another page on clicking the button. And the page should not refresh.

More Info: I am using a DropdownList and a Button. After making a selection in the DropdownList, I save that value to a table when the Button is clicked. I already have validation checks for the DropdownList when the button is clicked.

I have the the following code at the end of my button click event handler.

Response.Write("<script>alert(''Record Added Successfully'');</script>");



当我运行此代码时,每当我单击按钮时页面就会刷新,并弹出警报消息.

我该如何解决此问题?



When I run this code, the page refreshes whenever I click the button and the alert message pops up.

How do I resolve this problem?

推荐答案

每次编写该代码,它就会运行,这似乎很明显.因此,仅在按下保存按钮时(在click事件中),才编写代码.如果那对您不明显,您应该停止正在做的事情(闻起来像真实的工作),并学习编程的绝对基础,这是我们在四年制课程的第一周在西方学到的东西我们开始要求人们付出我们的努力之前就采取了行动.

哦,我想我明白了.您还保存了单击按钮,它还可以执行其他操作?好吧,对我来说,这似乎不是一个很好的用户界面,但是,在那种情况下,通常应该在保存值之前检查该值是否已更改,然后仅在这种情况下才显示该消息.在我看来,正在发生的事情完全是合乎逻辑的,您需要分解设计步骤,以查看何时需要消息,何时不希望消息以及如何编写代码来区别.在不需要时进行保存也很不好,因为除非它是一个利用率低的网站,否则您应该在使用数据库访问之类的资源时更加节俭.

另一件事-我怀疑它在这里是否适用,但是每个人都很难理解您的问题,因此我将它丢出去是为了什么.您可以使用OnClientClick方法在回发之前运行javascript.因此,您可以使用它在回发之前显示一个消息框,或者返回false来阻止回发发生.
Every time you write that code, it will run, that seems obvious. So, write the code only when the save button is pushed ( in the click event ). If that''s not obvious to you, you should stop what you''re doing ( which smells like real work ) and learn the absolute basics of programming, the stuff we in the west learned in the first week of the four year course we took before we started asking people to pay for our efforts.

Oh, I think I get it now. You save on a button click that does other things, too ? Well, that doesn''t seem like great UI to me, but, in that case, you should in general be checking if the value was changed before saving it, and then only show the message if that was the case. It still sounds to me like what is happening is perfectly logical, and you need to break down the steps of your design to see when you want the message, when you do not, and how to write code to tell the difference. It''s also bad to be saving when you don''t need to, because unless it''s a low use site, you should be more frugal with resources like database access.

One other thing - I doubt it applies here, but everyone is having a hard time decoding your question, so I throw it out there for what it''s worth. You can use the OnClientClick method to run javascript BEFORE the postback. So you can use that to show a message box before a postback, or return false to stop the postback occuring.


您可以在Button WebControl的单击上显示字符串消息,而无需刷新通过在form_Load事件上相应地设置Button的 Attributes 属性来设置页面.
通过设置此属性,它将在单击按钮时添加JavaScript代码.
You can show a string message on the Button WebControl''s click without refreshing the page by setting the Button''s Attributes property accordingly on the form_Load event.
By Setting this property it will add javascript code at button click..
btn.Attributes.Add("onclick", "return confirm('" & strMessage & "');")


这篇关于需要C#代码以在单击按钮时显示警报框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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