做项目中会用到的一些共公的方法

本文介绍了一些ASP.NET中的实用技巧,包括如何弹出警告框并跳转页面、如何为GridView的行添加删除确认等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 弹出Alert后再转向某页面

 /// <summary>
        /// 弹出Alert然后转向
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="url"></param>
        protected void ShowAlertAndRedirect(string msg, string url)
        {
            ResponseScript("alert('" + msg + "');window.location='" + url + "'");
        }

2 弹出alert提示

/// <summary>
        /// 弹出Alert
        /// </summary>
        /// <param name="msg"></param>
        protected void ShowAlert(string msg)
        {
            ResponseScript("alert('" + msg + "');");
        }

3 执行客户端脚本

 /// <summary>
        /// 执行客户端脚本
        /// </summary>
        /// <param name="script">脚本</param>
        protected void ResponseScript(string script)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "script", "<script>" + script + "</script>");
        }

4 GridView数据绑定的时候一般都会有删除这一项功能,所以都要用删除提示功能,也可以写成公共方法

/// <summary>
        /// 为GridView行添加功能
        /// </summary>
        /// <param name="e">行</param>
        /// <param name="c">"删除"LinkButton所在单元格</param>
        protected void GvRowAddFun(GridViewRowEventArgs e, int c)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.EnableViewState = false;
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E4F4FD'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
                if (c > 0)
                    ((LinkButton)(e.Row.Cells[c].FindControl("delButton"))).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
            }
        }

5 重写OnError方法

protected override void OnError(EventArgs e)
        {
            Exception exception = Server.GetLastError();
            string errorInfo = "<br>Error Message: " + exception.Message;
            Response.Write(errorInfo);
            Server.ClearError();
            base.OnError(e);
        }

转载于:https://www.cnblogs.com/yuanweisen/archive/2008/12/09/1351319.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值