WebUI.cs

 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;



/// <summary>
/// WebUI 的摘要说明
/// </summary>
public class WebUI
{
    public WebUI()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    #region MD5加密
    /// <summary>
    /// MD5加密
    /// </summary>
    /// <param name="pwd">需要加密的字符串</param>
    /// <param name="move">偏移量</param>
    /// <returns>加密后字符串</returns>
    public static string GetMD5Str(string pwd, string move)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] bytValue, bytHash;
        bytValue = System.Text.Encoding.UTF8.GetBytes(move + pwd);
        bytHash = md5.ComputeHash(bytValue);
        md5.Clear();
        string pwdMD5Str = "";
        for (int i = 0; i < bytHash.Length; i++)
        {
            pwdMD5Str += bytHash[i].ToString("x").PadLeft(2, '0');
        }
        return pwdMD5Str;
    }

    //WebUI.GetMD5Str(pwd, "")  
    #endregion

    #region Cookie登陆验证
     public static void isLogin()
    {
        if (HttpContext.Current.Request.Cookies["myCookie"] == null)
        {
            HttpContext.Current.Response.Redirect("~/Login.aspx");
        }
    }
    #endregion

    #region Alert()提示
    /// <summary>
    /// 弹出JavaScript小窗口
    /// </summary>
    /// <param name="js">窗口信息</param>
    public static void Alert(string message, Page page)
    {
        string js = @"<Script language='JavaScript'>
                    alert('" + message + "');</Script>";
        if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "alert"))
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "alert", js);
        }
    }

    /// <summary>
    /// 弹出消息框并且转向到新的URL
    /// </summary>
    /// <param name="message">消息内容</param>
    /// <param name="toURL">连接地址</param>
    public static void AlertAndRedirect(string message, string toURL, Page page)
    {
        string js = "<script language=javascript>alert('{0}');window.location.href('{1}')</script>";
        if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "AlertAndRedirect"))
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "AlertAndRedirect", string.Format(js, message, toURL));
        }
    }
    #endregion

    #region 上传图片
    /// <summary>
    /// 上传图片
    /// </summary>
    /// <param name="fileUpName">FileUpload控件名</param>
    /// <param name="imageUrl">上传文件夹路径</param>
    /// <param name="dataUrl">插入数据库的路径值</param>
    /// <returns></returns>
    public static string UpLoadImage(System.Web.UI.WebControls.FileUpload fileUpName, string imageUrl)
    {
        string dataUrl = "";
        if (fileUpName.HasFile)
        {
            string fullfilename = fileUpName.PostedFile.FileName;//上传文件 本地 路径
            string type = fullfilename.Substring(fullfilename.LastIndexOf(".") + 1).ToLower(); //得到加载文件的扩展名
            if (type.Equals("jpg") || type.Equals("bmp") || type.Equals("gif") || type.Equals("png") || type.Equals("jpeg"))
            {
                Random rd = new System.Random();
                //string filename = fullfilename.Substring(fullfilename.LastIndexOf("\\") + 1);       //上传的文件名
                dataUrl = DateTime.Now.ToString("yyy-MM-dd") + rd.Next(10000).ToString() + "." + type;    //数据库字段值
                string serverpath = System.Web.HttpContext.Current.Server.MapPath(imageUrl) + dataUrl;  //上传路径
                fileUpName.PostedFile.SaveAs(serverpath);
            }
        }
        return dataUrl;
    }

    #endregion

    #region 上传文件
    /// <summary>
    /// 上传文件
    /// </summary>
    /// <param name="fileUpName">FileUpload控件名</param>
    /// <param name="fileUrl">上传文件夹路径</param>
    /// <param name="dataUrl">插入数据库的路径值</param>
    /// <returns></returns>
    public static string UpLoadFile(System.Web.UI.WebControls.FileUpload fileUpName, string fileUrl)
    {
        string dataUrl = "";
        if (fileUpName.HasFile)
        {
            Random rd = new System.Random();
            string filepath = fileUpName.PostedFile.FileName;       //上传文件 本地 路径
            //dataUrl = filepath.Substring(filepath.LastIndexOf("\\") + 1);       //上传的文件名
            string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);       //上传的文件名
            dataUrl = filename;    //数据库字段值
            string serverpath = System.Web.HttpContext.Current.Server.MapPath(fileUrl) + dataUrl;  //上传路径
            fileUpName.PostedFile.SaveAs(serverpath);
        }
        return dataUrl;
    }
    #endregion

    #region 截取字符串
    /// <summary>
    /// 截取字符串
    /// </summary>
    /// <param name="strString">字符串</param>
    /// <param name="intLength">长度</param>
    /// <returns></returns>
    public static string SubString(string input, int p)
    {
        Encoding encode = Encoding.GetEncoding("gb2312");
        byte[] byteArr = encode.GetBytes(input);
        if (byteArr.Length <= p) return input;

        int m = 0, n = 0;
        foreach (byte b in byteArr)
        {
            if (n >= p) break;
            if (b > 127) m++; //重要一步:对前p个字节中的值大于127的字符进行统计
            n++;
        }
        if (m % 2 != 0) n = p + 1; //如果非偶:则说明末尾为双字节字符,截取位数加1

        return encode.GetString(byteArr, 0, n);
    }
    #endregion

    #region Seo设置
    /// <summary>   
    /// Seo设置   
    /// </summary>   
    /// <param name="seoTitle">标题</param>   
    /// <param name="seoKeyWords">关键字</param>   
    /// <param name="seoDescription">描述</param>   
    /// <param name="page"></param>   
    public static void setMeta(string seoTitle, string seoKeyWords, string seoDescription, Page page)
    {
        page.Title = seoTitle;
        System.Web.UI.HtmlControls.HtmlMeta hmKey = new HtmlMeta();
        hmKey.Name = "keywords";
        hmKey.Content = seoKeyWords;
        System.Web.UI.HtmlControls.HtmlMeta hmDes = new HtmlMeta();
        hmDes.Name = "description";
        hmDes.Content = seoDescription;
        System.Web.UI.Page p = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
        p.Header.Controls.Add(hmKey);
        p.Header.Controls.Add(hmDes);
    }

    / <summary>
    / 调用示例
    / </summary>
    //public void BindSeo()
    //{
    //    string sql = string.Format("select Title,KeyWord,Contents from T_Seo where id=1");
    //    DataTable dt = DBHelper.GetDataTable(sql);
    //    WebUI.setMeta(dt.Rows[0]["Title"].ToString(), dt.Rows[0]["KeyWord"].ToString(), dt.Rows[0]["Contents"].ToString(), this.Page);
    //}
    #endregion
}

帮我找出一下代码的错误,“/程序”应用程序中的服务器错误。 “gvProducts”上同时定义了 DataSource 和 DataSourceID。请移除一个定义。 说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidOperationException: “gvProducts”上同时定义了 DataSource 和 DataSourceID。请移除一个定义。 源错误: 行 39: this.gvProducts.DataSource = ds; 行 40: this.gvProducts.DataKeyNames = new string[] { "id" }; 行 41: this.gvProducts.DataBind(); 行 42: da.Dispose(); 行 43: conn.Dispose(); 源文件: c:\Users\86136\Desktop\第2组-网上书店系统\网上书店系统\程序\Myorderlist.aspx.cs 行: 41 堆栈跟踪: [InvalidOperationException: “gvProducts”上同时定义了 DataSource 和 DataSourceID。请移除一个定义。] System.Web.UI.WebControls.DataBoundControl.ConnectToDataSourceView() +8658325 System.Web.UI.WebControls.DataBoundControl.GetData() +4 System.Web.UI.WebControls.DataBoundControl.PerformSelect() +60 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73 System.Web.UI.WebControls.GridView.DataBind() +4 Myorderlist.bind_ordertables() in c:\Users\86136\Desktop\第2组-网上书店系统\网上书店系统\程序\Myorderlist.aspx.cs:41 Myorderlist.BindShopBasket() in c:\Users\86136\Desktop\第2组-网上书店系统\网上书店系统\程序\Myorderlist.aspx.cs:71 Myorderlist.Page_Load(Object sender, EventArgs e) in c:\Users\86136\Desktop\第2组-网上书店系统\网上书店系统\程序\Myorderlist.aspx.cs:26 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值