An array of C#, PHP, and HTML programming articles, tutorials, and resources

Posts Tagged ‘ url ’

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.

Web-based applications involve pages connected by links. Therefore, verifying the authenticity of links becomes a crucial factor. For example, one situation may involve verifyng a link before storing it into a database. Pass to the function a url as a string and it will return true (the link works) or false (the link did not work).

public bool checkUrlLink(string url)
{
   HttpWebRequest request = HttpWebRequest)WebRequest.Create(url);
   request.Proxy = null;
   try
   {
      HttpWebResponse response = (HttpWebResponse)request.GetResponse();
      return true;
   }
   catch
   {
      return false;
   }
}

This code snippet uses HttpWebRequest and HttpWebResponse functions. Therefore, the snippet above requires the following using statement:

using System.Net;

Also note that this code snippet will also check wheather a url is properly formatted. If the format is not correct, it will return false (simply because the url did not receive a response).

How to Redirect a Page in C#

by Victor | March 31, 2008 in C# | 1 Comment

Redirecting a web user to another web page without requiring the user to click a link is a common practice in web development. The built in function of C# is the Redirect function. The redirection function can take one or two input parameters. With one input parameter, simply specify the redirect url.

Response.Redirect("http://www.victorchen.info");

By using only one parameter, code after the Redirect function can still be executed. To prevent this, use the overloaded Redirect function with two input parameters. The first input parameter is still the redirect url, but the second input is the a boolean variable indicating whether remaining code on the page should continue executing or stop when the redirection function is reached.

Response.Redirect("http://www.victorchen.info", false);

Redirect is a function of the HttpResponse class. The Redirect function returns void.