目录
点击UpdatePanel中的按钮弹出javascript对话框收藏
在UpdatePanel控件内使用Response.Write()弹出对话框
点击UpdatePanel中的按钮弹出javascript对话框收藏
/// <summary>
/// 执行javascript 点击UpdatePanel中的按钮弹出javascript对话框
/// </summary>
/// <param name="msg">提示的信息</param>
/// <param name="url">提示后链接的地址</param>
protected void runJavascript(string msg, string url)
{
string javascriptStr = "";
if (url.Length == 0)
{
javascriptStr = "alert('" + msg + "');";
}
else
{
javascriptStr = "alert('" + msg + "');location.href='" + url + "'";
}
ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "SuccessAlert", javascriptStr, true);
}
/// <summary>
/// 弹出对话框 如果没有用到Ajax中的UpdatePanel,可以用以下方法弹出提示框
/// </summary>
/// <param name="p"></param>
/// <param name="strmessage"></param>
/// <author></author>
/// <createdate></createdate>
public static void ShowMessage(Page p, string strmessage)
{
string strScript = "<script>alert('" + strmessage + "');</script>";
p.RegisterStartupScript("script", strScript);
}
在UpdatePanel控件内使用Response.Write()弹出对话框 | |
|
ASP.NET弹出对话框确认
我们在ASP.NET程序的开发过程中,常常需要向用户给出提示信息,比如是否“操作成功”,“确定”还是“取消”操作。
(1) 点击页面上的按钮,弹出一个对话框提示是“确定”还是“取消”操作,我们采用在按钮中添加属性来完成:
例: public System.Web.UI.WebControls.Button btnDelRow;
btnDelRow.Attributes.Add("onclick", "return confirm('确定要删吗?');");
(2)点击页面上的链接,弹出一个对话框提示是“确定”还是“取消”操作,可在Page_Load()事件中,给希望给出确认提示的按钮增加属性:
例:
Link.Attributes.Add("onclick", "return confirm('你要执行这个操作吗?');");
(3) 对于页面完成一个操作后,弹出一个对话框提示是否“操作成功”。
例:
Response.Write("<script>alert('删除成功!')</script>");
(4)允许 ASP.NET 服务器控件在 Page 中发出客户端脚本块:
public virtual void RegisterStartupScript(string key,string script);
例:
if(!this.IsStartupScriptRegistered("hello"))
this.RegisterStartupScript("hello","<script>alert('你好!')</script>");
ASP.NET的GridView 删除确认对话框
Gridview 弹出对话框
实现方法:
双击GridView的OnRowDataBound事件;
在后台的GridView1_RowDataBound()方法添加代码,最后代码如下所示:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//如果是绑定数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:/"" + e.Row.Cells[1].Text + "/"吗?')");
}
}
}
GridView实现自动编号
双击GridView的OnRowDataBound事件;
在后台的GridView1_RowDataBound()方法添加代码,最后代码如下所示:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//如果是绑定数据行 //清清月儿http://blog.youkuaiyun.com/21aspnet
if (e.Row.RowType == DataControlRowType.DataRow)
{
鼠标经过时,行背景色变
//e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
鼠标移出时,行背景色变
//e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
当有编辑列时,避免出错,要加的RowState判断
//if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
//{
// ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:/"" + e.Row.Cells[1].Text + "/"吗?')");
//}
}
if (e.Row.RowIndex != -1)
{
int id = e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
}
}