yuanweisen

 

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

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);
        }

posted on 2008-12-09 18:18    阅读(180)  评论(0编辑  收藏  举报

导航