示例#1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            UserInfoBoxControl userInfoBoxControl = (UserInfoBoxControl)LoadControl("~/UserInfoBoxControl.ascx");

            userInfoBoxControl.UserName    = "******";
            userInfoBoxControl.UserAge     = 123;
            userInfoBoxControl.UserCountry = "Australia";
            phUserInfoBox.Controls.Add(userInfoBoxControl);
            HelloWorldLabel.Text = "Hello,";
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            HelloWorldLabel.Text = "Hello, World";

            // Caching

            ArrayList datestamps;

            UserInfoBoxControl userInfoBoxControl =
                (UserInfoBoxControl)LoadControl("~/UserInfoBoxControl.ascx");

            userInfoBoxControl.UserName    = "******";
            userInfoBoxControl.UserAge     = 78;
            userInfoBoxControl.UserCountry = "Spain";
            phUserInfoBox.Controls.Add(userInfoBoxControl);

            if (Cache["datestamps"] == null)
            {
                datestamps = new ArrayList();
                datestamps.Add(DateTime.Now);
                datestamps.Add(DateTime.Now);
                datestamps.Add(DateTime.Now);

                Cache.Add("datestamps", datestamps, null,
                          System.Web.Caching.Cache.NoAbsoluteExpiration,
                          new TimeSpan(0, 0, 60),
                          System.Web.Caching.CacheItemPriority.Default, null);
            }
            else
            {
                datestamps = (ArrayList)Cache["datestamps"];
            }

            // To work with update panel postback.

            if (!IsPostBack)
            {
                foreach (DateTime dt in datestamps)
                {
                    Response.Write(dt.ToString() + "<br />");
                }
            }


            // State

            if (Session["BackgroundColor"] != null && !this.IsPostBack)
            {
                ColorSelector.SelectedValue            = Session["BackgroundColor"].ToString();
                div_Sessions.Style["background-color"] = ColorSelector.SelectedValue;
            }

            if (ViewState["NameOfUser"] != null)
            {
                NameLabel.Text = ViewState["NameOfUser"].ToString();
            }
            else
            {
                NameLabel.Text = "Not set yet...";
            }

            // Localization

            var currentUICulture = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;

            if (currentUICulture.Equals("en", StringComparison.OrdinalIgnoreCase))
            {
                // Direct Access
                lblHelloWorldGlobal.Text = Resources.MyGlobalResources.HelloWorldLabel_Text;
            }
            else if (currentUICulture.Equals("de", StringComparison.OrdinalIgnoreCase))
            {
                // Local Resource Object
                lblHelloWorldGlobal.Text = GetLocalResourceObject("lblHelloWorld.Text").ToString();
            }
            else if (currentUICulture.Equals("es", StringComparison.OrdinalIgnoreCase))
            {
                // Global Resource Object
                lblHelloWorldGlobal.Text =
                    GetGlobalResourceObject("MyGlobalResources_es", "HelloWorldLabel.Text").ToString();
            }
            else // default to en
            {
                lblHelloWorldGlobal.Text = Resources.MyGlobalResources.HelloWorldLabel_Text;
            }

            // MySQL

            try
            {
                var connString = Base64Decode(ConfigurationManager.ConnectionStrings["MySQLConnStr"].ConnectionString);

                using (OdbcConnection connection =
                           new OdbcConnection(connString))
                {
                    connection.Open();
                    using (OdbcCommand command =
                               new OdbcCommand("SELECT id, name FROM test_users", connection))
                        using (OdbcDataReader dr = command.ExecuteReader())
                        {
                            ddlUsers.DataSource = dr;
                            ddlUsers.DataBind();
                            dr.Close();
                        }
                    connection.Close();
                }
            }
            catch (Exception ex)
            {
                Response.Write("An error occured:" + ex.Message);
            }
        }