欢迎光临
我们一直在努力

服务器端Confirm对话框的实现-.NET教程,评论及其它

建站超值云服务器,限时71元/月

我们在使用asp+com编程时,可以利用asp代码的灵活性随意操控ui(user interface)的表现形式,但是升级到了.net后,编程的思想变了,由结构化变为了面向对象,许多封装好的控件应用成为我们编程的重点。在方便使用的同时也带来了一些灵活性的限制,比如传统asp编程时选取多个记录后,点击《删除》按钮,一般是触发一段javascript,例如:

相应的javascript为:

相信大家都能看懂这段代码,也不做过多的解释了。那么在.net环境中编程,该如何实现弹出确认对话框呢?我一直在想这个问题,一些asp.neter也有一些方法,例如,将javascript封装在cs文件中,然后向客户端抛出,得到返回值后操作(如果返回值为true)。例如:

aspx文件:

????

?????

??????

?????

????

cs文件:

??private void page_load(object sender, system.eventargs e)

??{

??????????? system.data.sqlclient.sqldataadapter sda=new system.data.sqlclient.sqldataadapter("select * from 新闻","server=localhost; uid=sa; pwd=sa; database=sbg");

??????????? system.data.dataset ds=new system.data.dataset();

??????????? sda.fill(ds,"a");

??????????? datagrid1.datasource=ds.tables["a"].defaultview;

???datagrid1.databind();

??}

??private void datagrid1_itemdatabound(object sender, system.web.ui.webcontrols.datagriditemeventargs e)

??{

???if(e.item.itemindex>=0)

???{

????linkbutton a =new linkbutton();

????a=(linkbutton)e.item.cells[0].controls[0];

????a.attributes["onclick"]="javascript:return confirm(确定?);";

???}

??}

以上做法完全可以,但仔细想想,他的代码重用性太低了,不符合面向对象的思想。那么下面介绍一种customcontrol的办法,就是重载button类的一些方法。大家可以试着做,以后会很方便的使用,因为它支持拖放:)

第一步:写一个confirmbutton的类

可以新建一个控件库

图一:

填加以下代码:

using system;

using system.web.ui;

using system.web.ui.webcontrols;

using system.componentmodel;

[assembly:clscompliant(true)]

?

namespace clientsidecontrols

{

?///

?/// summary description for confirmbutton.

?///

?

?[defaultproperty("text"),

?toolboxdata("<{0}:confirmbutton runat=server>")]

?public class confirmbutton : button

?{

??[bindable(true),

??category("appearance"),

??defaultvalue("")]

??public string popupmessage

??{

???get

???{

????// see if the item exists in the viewstate

????object popupmessage = this.viewstate["popupmessage"];

????if (popupmessage != null)

?????return this.viewstate["popupmessage"].tostring();

????else

?????return "are you sure you want to continue?";

???}

???set

???{

????// assign the viewstate variable

????viewstate["popupmessage"] = value;

???}

??}

??protected override void addattributestorender(htmltextwriter writer)

??{

???base.addattributestorender(writer);

???string script = @"return confirm(""%%popup_message%%"");";

???script = script.replace("%%popup_message%%",

????this.popupmessage.replace("\"", "\\\""));

???writer.addattribute(htmltextwriterattribute.onclick, script);

??}

?}

}

?

先不用管他为什么这么写了,把他编译后生成.dll文件

?

第二步:创建一个web工程

图二:

?

第三步:将刚才confirmbutton类填加到web工程中

图三:

图四:

将刚才confirmbutton类编译后的dll添加到工具栏中,此confirmbutton.dll在刚才工程中的\bin\debug\中,注意,当引用后会自动加一个confirmbutton的控件,但不可用,我也不知道为什么,所以先把他删除再添加新的。

另外,在工具栏中也是添加那个dll文件

图五:

图六:

?

?

第四步:编程实现

将刚才confirmbutton控件拖放到web窗体上,并修改弹出对话框的文字属性:

图七:

填加一个lable标签做测试用:

?

编写codebehind的cs代码:(注意:如果用户点击取消按钮则不触发confirmbutton1_click事件,所以只有当用户点击了确定按钮才触发这个事件,我们可以在此编写要实现的操作)

图八:

?

ok,大家可以自己试试。

如果可以,则把这个文件封装好,以后像用label,button等服务器端控件一样去用confirmbutton就可以了

?

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 服务器端Confirm对话框的实现-.NET教程,评论及其它
分享到: 更多 (0)

相关推荐

  • 暂无文章