一.给按钮加确认框
给一个asp的button按钮一个响应函数,在后台的响应函数里面增添确认框,确认点击操作:
this.Button1.Attributes.Add("onclick ", "return confirm( '记录已存在,是否覆盖? '); "); (确认框不刷新页面,确认框有两个按钮),confirm()可返回值,返回true表示点击的确定。
后台有Button按钮点击后的响应函数,点击确认直接执行响应函数,点击否就不执行响应函数。
二. 弹出不刷新的信息框
ASP.NET后台的弹出框可以用Response.write(“< script >alert(‘message’)</script>“);
这种js信息框会使页面刷新,弹出信息框后,后面全是空白的页面,所以使页面不刷新需要写客户端js
以下是在csdn上找的一段代码,可以实现页面不刷新
public void ShowMessage(string Message)
{
// Define the name and type of the client scripts on the page.
String csname1 = "PopupScript";
String csname2 = "ButtonClickScript";
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, csname1))
{
String cstext1 = "alert('" + Message + "');";
cs.RegisterStartupScript(cstype, csname1, cstext1, true);
}
// Check to see if the client script is already registered.
if (!cs.IsClientScriptBlockRegistered(cstype, csname2))
{
StringBuilder cstext2 = new StringBuilder();
cstext2.Append("<script type=/"text/javascript/"> function DoClick() {");
cstext2.Append("Form1.Message.value='Text from client script.'} </");
cstext2.Append("script>");
cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
}