Tuesday 29 July 2008

Regex to find images in HTML

The other day I wanted to find a way to extract the image urls from some HTML, so I found the following to work quite nicely:


Regex rgxFiles = new Regex(@"[\w/:.]+\.(?:jpg|bmp|gif|png)");
MatchCollection mtcFiles = Regex.Matches(strSourceString,@"[\w/:.]+\.(?:jpg|bmp|gif|png)",RegexOptions.IgnoreCase);

foreach (Match mFile in mtcFiles)
{

//mFile.Value will contain the image address that we can work with

}

The HTTP verb POST used to access path ''Path' is not allowed

This is probably one of the most stupid mistakes I have made in months. I kept this message when transferring data from one website page to another, and I couldn't understand why.....


Hmmmm.....


It helped when I gave the webpage an aspx extension!!!


One problem here, if you try and post data to a page that is not asp or aspx, IIS and .NET does NOT like it!

Hope this helps!

Thursday 24 July 2008

REGEX to remove HTML formatting

I've seen a few ways of using Regex to remove HTML formatting, but the following seems to work fine for me!:

public string Strip(string text)
{
string sReturn = System.Text.RegularExpressions.Regex.Replace(text, @"<(.|\n)*?>", string.Empty);
sReturn = System.Text.RegularExpressions.Regex.Replace(text, @"(&\w*;)", string.Empty);
return sReturn;
}

Friday 11 July 2008

ModalPopupExtender for deleting DataGridView rows

In the words of Little Britain, "What a caffuffle"!

How difficult does it have to be to delete a row from a datagrid, and use an AJAX ModalPopupExtender to provide a nice UI for it? The answer, very!

For all those who are trying to do it, i'm not going to re-invent the wheel, simply take yourself over to Matt Berseths' blog, where he has managed to do it (and now have I!)

http://mattberseth.com/blog/2007/07/confirm_gridview_deletes_with.html

Thursday 10 July 2008

Seekafile Server - not removing indexed documents

I've noticed that on a particular Windows XP machine, that when seekafile server is running, it does detect that a file has changed, but it does not remove the old one from the index.

So, what I am having to do, is modify the crawler application to delete the folder containing the site files, to force seekafile to remove the folder (and it's contents) from the index.

When the crawler then downloads the files from the site again, it create a new index - in fact, this way is better as it ensures that the site data is correct!

Chaning the ASP.NET Name attribute

This had be frustrated for days - you can change the ID property of a dynamically created control, but you can't change the NAME attribute!

Why do I care? Simple, I wanted to use the ID property for use in JavaScript calls, and the NAME attribute for when I was posting the form data - for example, I wanted two textboxes, one with an ID of 12, but a name of "Name", and another with an ID of 13, but a name of "Age", so that when I submitted the form data, I could get a friendly name for it.

(Before anyone harps on about "the .NET framework uses the NAME property in postbacks" - I do know this, but it is not required in this scenario.)

So, how did I do it? I created a class that inherits from TextBox, and then created an property within it called Name. I then overrided the render method to render the box as I needed it to be done in HTML:


public class MTextBox : TextBox
{
private string _mName;
public string Name
{
get { return _mName; }
set { _mName = value; }
}
//overriden to print name attribute correctly
protected override void Render(HtmlTextWriter writer)
{
string sOutput = "";
//Here you can squirt out your HTML in whatever format you want, e.g. you can
//squirt out the HTML of Name=\" + __mName + "\" etc
writer.Write(sOutput);
}
}


Sorry I can't give a more complete example, but blogger treats the code sample as HTML!!! Contact me if you want the code.

In my application, I can then just do this:

MTextBox textBox = new MTextBox();
textBox.Name = "Something I want it to be!!!";