遍历页面控键

//----------------------------------------------------------------------------------------------------
//模块名称: 常用操作类
//功    能: 完成页面的常用操作
//作    者: liuling
//创建日期:
//----------------------------------------------------------------------------------------------------
//修改记录:
//日期  修改人  描述
//----------------------------------------------------------------------------------------------------
//08.8.9   liuling      create
//----------------------------------------------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Reflection;
using System.IO;
using ICE.BLL;
using ICE.Controls;

namespace ICE.Web.Common
{
    public class CommonOperate :System.Web.UI.Page
    {
        protected PageControl mPageControl = new PageControl();

        #region 增加系统日志
        /// <summary>
        /// 增加系统日志
        /// </summary>
        /// <param name="logContent">日志内容</param>
        public void WriteLog(string logContent)
        {
            StreamWriter sw = File.AppendText(Server.MapPath(".") + "\\log\\log.txt");
            sw.WriteLine(System.DateTime.Now.ToString() + " Erro Infomation:" + logContent);
            sw.WriteLine("------------------------------------------------------------------------");
            sw.Flush();
            sw.Close();
        }
        #endregion

        #region 获取页面需要存翻译内容的控件ID数组
        /// <summary>
        /// 获取页面需要存翻译内容的控件ID
        /// 注:每个前台页面上都需要放置一个存放需要转换语言控件的字符串,字符串变量为Public类型,名称为ControlID
        ///
        /// 如:页面中放置了txtUserName文本框和btnSubmit提交按钮两个控件,
        /// 那么我们在当前页面中需要加入这句代码:Public string ControlID = "txtUserName,btnSubmit";
        /// </summary>
        /// <returns></returns>
        public string[] getControls(string path, string ClassName)
        {
            Assembly ass = Assembly.Load(path);

            object obj = ass.CreateInstance(path + "." + ClassName);

            string[] controlID = obj.GetType().GetField("ControlID").GetValue(obj).ToString().Split(',');

            return controlID;
        }
        #endregion

        #region Get Current page name
        /// <summary>
        /// Get Current page name
        /// </summary>
        /// <returns></returns>
        public string GetCurrentPageName()
        {
            string CurrentPage = String.Empty;

            int tempIndex = HttpContext.Current.Request.FilePath.ToString().LastIndexOf("/");

            CurrentPage = HttpContext.Current.Request.FilePath.ToString().Substring(tempIndex + 1);

            return CurrentPage;
        }
        #endregion

        #region Load Page Language
        /// <summary>
        /// Load Page Language
        /// </summary>
        /// <param name="page">Page</param>
        public void LoadPageLanguage(Page page)
        {
            if (Session["Language_ID"] != null)
            {
                string ctlType = "";
                string CurrentPage = GetCurrentPageName();

                DataSet ds = mPageControl.GetControlLanage(CurrentPage.Substring(0,CurrentPage.Length-5), Session["Language_ID"].ToString());

                //foreach (Control ctl in page.Form.Controls)
                //{
                    LoadLanguage(page,ds);
                //}
               
            }
        }
        #endregion

        public void LoadLanguage(Control ctl, DataSet ds)
        {
            string ctlType = "";

            for (int i = 0; i < ctl.Controls.Count; i++)
            {
                if(ctl.Controls[i].Controls.Count != 0)
                    LoadLanguage(ctl.Controls[i], ds);

                DataRow[] dr;
                if (ctl.Controls[i].ID != null)
                    dr = ds.Tables[0].Select("PageControlID='" + ctl.Controls[i].ID + "'");
                else
                    continue;

                if (dr != null)
                {
                    ctlType = ctl.Controls[i].GetType().ToString();

                    switch (ctlType)
                    {
                        case "System.Web.UI.WebControls.Label":

                            ((Label)ctl.Controls[i]).Text = dr[0]["ControlText"].ToString();
                            break;
                        //case "ICE.Controls.HFLabel":
                        //    ((HFLabel)ctl.Controls[i]).Text = dr[0]["ControlText"].ToString();
                        //    break;
                        case "ICE.Controls.HFButton":
                          
                            ((HFButton)ctl.Controls[i]).Text = dr[0]["ControlText"].ToString();
                            break;
                        case "System.Web.UI.WebControls.Button":

                            ((Button)ctl.Controls[i]).Text = dr[0]["ControlText"].ToString();
                            break;
                        case "System.Web.UI.WebControls.LinkButton":

                            ((LinkButton)ctl.Controls[i]).Text = dr[0]["ControlText"].ToString();
                            break;
                        case "System.Web.UI.WebControls.HyperLink":
                           
                            ((HyperLink)ctl.Controls[i]).Text = dr[0]["ControlText"].ToString();
                            break;
                    }
                }
            }
        }
    }
}

 

posted @ 2008-08-19 15:26  HIT上帝视角  阅读(192)  评论(0编辑  收藏  举报