博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET Alerts: how to display message boxes from server-side code?
阅读量:4316 次
发布时间:2019-06-06

本文共 2730 字,大约阅读时间需要 9 分钟。

Pasted from:

 

One of the most common mistakes beginner developers make is to call MsgBox.Show from their server-side code. It is a mistake because this code runs server-side and will just display the message box on the server where it's not going to be very useful. Well, in version 2.0, it will actually throw an exception.

Once the developers understand the disconnected way HTTP works, it becomes quite natural, but the need to trigger an alert box from the server-side remains.

That's why I just published a new that contains an Alert server control and a ConfirmButton server control that you can use both client-side and server-side. The controls should work fine in all browsers, but will look a little nicer in IE thanks to the (non-standard) modal dialog feature.

Here's how you use the controls...

First, if you want to show an alert from server-side code, you've got to first declare the alert in your page's markup (or new up a new .Samples.Alert.Alert control and set its properties and subcontrols if you're that sort of developer):

This is a rich alert box that was triggered by
server-side code.

And then simply call the Show() method on the Alert instance from server-side code:

ServerAlert.Show();

You can handle the user's choice server-side by handling the OnChoice event and act accordingly:

public void ServerAlertChoice(object sender, AlertChoiceEventArgs e) {  AlertResult.Text = "You clicked " + e.Result.ToString();  AlertResult.Visible = true;}

You can also trigger the alert's display client-side and handle its response client-side:

This is a rich alert box that was triggered by
client-side code.
The button will take the value that you chose.

Or any combination that's useful for you.

You can also use the ConfirmButton as you would use a regular button (it derives from Button) to ask the user for confirmation before posting back to the server:

And so that you can use such confirm buttons in a repeated control such as a GridView, instances of a repeated confirm button can reference a single alert to save HTML rendering and not repeat the alert text for each data row:

Are you sure you want to select this row?

Check out the Alert.aspx page for these examples in context.

I hope this is useful. As always, feedback is welcome.

UPDATE: The first release was missing a file. I've corrected the version in source control and the release file. Thanks for pointing that out.

UPDATE 2: the project migrated to CodePlex.

转载于:https://www.cnblogs.com/Glen/archive/2007/08/28/872599.html

你可能感兴趣的文章
为什么 Redis 重启后没有正确恢复之前的内存数据
查看>>
No qualifying bean of type available问题修复
查看>>
第四周助教心得体会
查看>>
spfile
查看>>
Team Foundation Service更新:改善了导航和项目状态速查功能
查看>>
WordPress资源站点推荐
查看>>
Python性能鸡汤
查看>>
android Manifest.xml选项
查看>>
Cookie/Session机制具体解释
查看>>
ATMEGA16 IOport相关汇总
查看>>
JAVA基础-多线程
查看>>
面试题5:字符串替换空格
查看>>
[Codevs] 线段树练习5
查看>>
Amazon
查看>>
component-based scene model
查看>>
Echart输出图形
查看>>
hMailServer搭建简单邮件系统
查看>>
从零开始学习jQuery
查看>>
Spring+SpringMVC+MyBatis深入学习及搭建(四)——MyBatis输入映射与输出映射
查看>>
opacity半透明兼容ie8。。。。ie8半透明
查看>>