The situation begins with your blog or website and you need to post some code on a particular webpage. In this tutorial, we will assume you want to post some basic HTML sample code (though this method will work with any programming or scripting language) that contains a hierarchy code structure.
Step 1: The Actual Code
You probably already have the first step, and that is obtaining the code you want to display on your website or blog. For this example, we will use the below:
<html>
<head>
<title>Victor's Programming Aid</title>
</head>
<body>
<p>This is a test</p>
</body>
</html>
Step 2: Code Manipulation
The next important step is to convert special characters to their associated HTML representations. Common examples of this are the greater than (>) and less than (<) symbols often used to distinguish tags. A ‘>’ symbol should be represented as ‘>’ and a ‘<’ symbol is represented as ‘<’. If you code with the actual symbol, the tags will be evaluated and therefore not display properly. The example below is what our example looks like with special symbols replaced with their associated html names.
<html>
<head>
<title>Victor's Programming Aid</title>
</head>
<body>
<p>This is a test</p>
</body>
</html>
Step 3: Code Formatting
However, if you just post the above code in your HTML while preserving the spaces and carriage returns (line breaks), you will discover the formatting is not properly displayed. In my experience, I find that using the HTML <pre></pre> tag is the best way to achieve the end goal. The text within the<pre> tag simply displays text pre-formatted. This means that formatting such as spaces and carriage returns are preserved. The example:
<pre><html>
<head>
<title>Victor's Programming Aid</title>
</head>
<body>
<p>This is a test</p>
</body>
</html></pre>
Success!
Now, your code will be properly formatted with the correct characters and symbols as well as correctly spaced. Happy coding! Let’s see some examples of this in action!
The object oriented programming concept is great for not only organizing code, but also easing readability. Creating a class in PHP is easy and we will go through the basics. For starters, creating a PHP class is as easy as:
class myFirstClass {
// CLASS CONTENT GOES HERE
}
The next step is to add variables and functions to the class. One of the first functions every class has is a constructor. A constructor is a function that is run when the class object is created. To create a constructor function for your class, a default constructor function name by “__construct()” is used. See the example below
class myFirstClass {
public function __construct() {
// CONSTRUCTOR CONTENT HERE
}
}
Next, you can add variables to your class. Variables can be defined as public or private. Private variables can be accessed only from functions belonging to the class. Public variables on the other hand can be accessed by functions belonging to the class along with functions outside the class. Once we have defined our variables, we can initialize them in our constructor. Below is an expanded example of a public and private variable.
class myFirstClass {
private $myPrivateVariable;
public $myPublicVariable;
public function __construct() {
$this->myPrivateVariable = "Private Message";
$this->myPublicVariable = "Public Message";
}
}
Lastly, we can add functions to our class. Because the $myPrivateVariable is private, we cannot assign it without a custom function. Therefore, in our next example, we will create a function that assigns a string value to the $myPrivateVariable. This function should be public since we want to be able to access it from outside of the class.
class myFirstClass {
private $myPrivateVariable;
public $myPublicVariable;
public function __construct() {
$this->myPrivateVariable = "Private Message";
$this->myPublicVariable = "Public Message";
}
public function assignMyPrivateVariable($message) {
$this->myPrivateVariable = $message;
}
}
Now, we have completed a very basic PHP class. Follow the same steps to create more variables and more functions to make the class more powerful and useful.
To programmatically setup and use an iframe in C#, copy the below iframe code snippet into your aspx page. You are free to add or edit any of the iframe attributes below, but note that the id and the runat attributes are required!
<iframe id="myIframe" runat="server" scrolling="auto">
</iframe >
Now in the aspx page’s codebehind, you can access the control as myIframe. To set a new URL in the iframe during postback, use the below snippet. The example below will dynamically load http://www.victorchen.info in the iframe window.
myIframe.Attributes["src"] = http://www.victorchen.info
You now you have a working iframe in your C# code and accessible in your codebehind.