Showing posts with label ASPX. Show all posts
Showing posts with label ASPX. Show all posts

Thursday, August 28, 2008

Check if a URL actually exists

0 comments
We have seen many validations for URLs.
But what to do if the requirement is like to check whether a URL is in the NET.
In other words, how can one knows the URL given has not returned a 404 error?

Here's the code



/// <summary>
/// Checks the status of the specified URL.
/// </summary>
/// <param name="strURL">The URL that needs to be checked</param>
/// <returns>True if the URL exists</returns>

public bool IsURLValid(string strURL)
{
string strResponse = "";
try
{
string pattern = @"((http|ftp|https):\/\/w{3}[\d]*.|(http|ftp|https):\/\/|w{3}[\d]*.)([\w\d\._\-#\(\)\[\]\\,;:]+@[\w\d\._\-#\(\)\[\]\\,;:])?([a-z0-9]+.)*[a-z\-0-9]+.([a-z]{2,3})?[a-z]{2,6}(:[0-9]+)?(\/[\/a-z0-9\._\-,]+)*[a-z0-9\-_\.\s\%]+(\?[a-z0-9=%&\.\-,#]+)?";
//validatng url in case if it hasn't been validated earlier
if(Regex.IsMatch(strURL,pattern))
{
WebRequest request = HttpWebRequest.Create(strURL);
request.Timeout = 5000;
strResponse = ((HttpWebResponse)request.GetResponse()).StatusCode.ToString();
}
}
catch(Exception exp)
{
strResponse = "";
}
return (strResponse=="OK")? true: false;
}



Does the work, but not very classy.
Any better ideas?
Read more...

Thursday, July 17, 2008

GridViewRow as Hyperlink

1 comments
This is a simple technique used in the RowDataBound event of a GridView so that its Entire Row acts as a HyperLink.
For Visual effects styles are applied at mouseover and mouseout.
The cursor is also given a hand like appearance to show the row is a hyperlink


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.color='DodgerBlue';this.style.cursor='hand';";
e.Row.Attributes["onmouseout"] = "this.style.color='Black';";
e.Row.Attributes["onclick"] = "window.navigate('NavigatedPage.aspx?id=" + e.Row.RowIndex + "')";
// can send anything as querystring
}
}

Read more...