Skip to main content

Posts

Showing posts from February, 2012

.Net C# Date and Time functions and Displaying the Month using a case statment.

//Note: Literal1 refers to a Literal Control that needs to be created on your form.

        protected void dateAndTime()
        {
            // Create a Time Object
            DateTime myTime = DateTime.Now;
            // Display the Year
            Literal1.Text = "The Year is: " + myTime.Year;

            // Display the Time
            Literal1.Text += "<br />  The Time is: ";
            Literal1.Text += myTime.Hour + ":" + myTime.Minute + ":" + myTime.Second;

            // Display the Month
            Literal1.Text += "<br /> The Month is: ";
            String thisMonth;
            thisMonth = getMonth(myTime.Month); // Call the Month Function
            Literal1.Text += myTime.Month + " (" + thisMonth + ")";

        }

        String  getMonth(int monthIn)
        {
            int caseSwitch = monthIn;
            string monthOut;
            switch (caseSwitch)
            {
               …

.NET C# HtmlEncode and HtmlEncode - Turn a String in to Valid HTML - Turn valid HTML In to a normal String. Web

// Turn a String in to Valid HTML
        var myVaildHtml = Server.HtmlEncode("blah & blah");  //  Will turn the & into &amp;
        Response.Write(myVaildHtml);

        //Turn valid HTML In to a normal String
        var myNormalString = Server.HtmlDecode(myVaildHtml);  // Will reverse the &amp; back to &
        Response.Write(myNormalString);

.NET C#, Permanent 301 Redirect, Temporary 302 Redirect. Web

protected void permanentRedirect ()
    {
        // 302 A PERMANENT REDIRECT
        Response.RedirectPermanent("http://www.markdownie.me.uk/");
    }

    protected void temporaryRedirect()
    {
        // 301 A TEMPORARY REDIRECT
        Response.Redirect("http://www.markdownie.me.uk/");
    }

.Net c# Get or Display Browser Information. Web

protected void displayBrowserInfo ()
    {
        Response.Write("<br /> Browser: " + Request.Browser.Browser);
        Response.Write("<br /> Version: " + Request.Browser.Version);
        Response.Write("<br /> Cookies Enabled:" + Request.Browser.Cookies);
        Response.Write("<br /> Javascript Version: " + Request.Browser.JScriptVersion);   
    }

.Net Dynamicaly Create and Remove a Control / Object, such as a button. Web

private void CreateControls()
    {
        // Create a New Button
        Button myButton = new Button();
        // Give the Button Some Properties
        myButton.Text = "A Dynamicaly Created Button";
        // Add Button to PlaceHolder
        // ** NOTE: The Planel must Exist in your design view
        Panel1.Controls.Add(myButton);

        }



    private void RemoveButton()
    {
        // Search for the button in the Page Controls Collection
        Button foundButton = (Button)Page.FindControl("myButton");

        // Remove the Button
        if (foundButton != null)
        {
            foundButton.Parent.Controls.Remove(foundButton);
        }
    }

.Net C# Dynamicaly Creating Page.Header Title, Keywords and Description. Web

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using Accessibility;publicpartialclass_Default : System.Web.UI.Page{

{
Page.Header.Title =
protectedvoid Page_Load(object sender, EventArgs e)"Dynamicaly Titled Page";// VERSION 1: You can use Page.MetaDescription and Page.MetaKeywordsPage.MetaDescription =
Page.MetaKeywords =
"Dynamicaly Created Meta Data Description";"Dynamic, Created, Keywords";// VERSION 2: or use Page.Header.Keywords and Page.Header.DescriptionPage.Header.Keywords =
Page.Header.Description =
}
}"More, Dynamic, Keywords";"More, Dynamic, Created, Keywords";

.Net C# write out all the controls that appear on your page. Web.

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using Accessibility;publicpartialclass_Default : System.Web.UI.Page{

{
protectedvoid Page_Load(object sender, EventArgs e)// Start Examning all the ControlsDisplayControl(Page.Controls, 0);
Response.Write(


}


{

{
"<hr /");privatevoid DisplayControl(ControlCollection controls, int depth)foreach (Control control in controls)// Use the depth paramater to indent the control tree.Response.Write(
new

.NET C# Page Flow. Web

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using Accessibility; publicpartialclass_Default : System.Web.UI.Page{

{

{
lblInfi.Text = (
}

}

{
lblInfi.Text +=
}

{
lblInfi.Text +=
}

{protectedvoid Page_Load(object sender, EventArgs e)if (!IsPostBack)"<b>This is the 1st time this page has been loaded.</b><br />");privatevoid Page_Init(object sender, EventArgs e)"Page.Init Event Handled.<br />";privatevoid Page_PreRender(object sender,