Tuesday, November 23, 2010

The following module was built either with optimizations enabled or without debug information

I have been getting this error quite a lot in visual studio 2008. To fix it I followed the following:

On the tools/options menu, under debugging, uncheck the 'enable just my code(managed only)'

...you will need to ensure the 'show all settings' checkbox is set.

Hope this helps someone!

Saturday, November 6, 2010

Raising event from webcontrol to host page

A simple way to raise an event from a webcontrol to the host aspx page in c#

1. Ok, in your web control at the top define the bubble event:

public partial class webcontrols_rightnav : System.Web.UI.UserControl
{
#region BubbleEvents
public event EventHandler MyUserControlEvent;
#endregion BubbleEvents

....

2. Next in one of your web control functions etc raise the event:

if (MyUserControlEvent!= null) //i.e. the host page is subscribing to the event!
{
//raise event
MyUserControlEvent(this, e);
}

3. In your host page...

Add the event handler:

rightnav1.MyUserControlEvent += new EventHandler(DoSomething);

4. Add the function to handle the event in the host page

protected void DoSomething(object sender, EventArgs e)
{
//
}


** important - one thing that catches me out is putting the ...+= new EventHandler(DoSomething); code in a !IsPostBack piece of code - this will only then fire on page load, which is probably NOT what you want!

...Hope this helps!

Friday, November 5, 2010

Remove double or single spaces from a string in C#

I have been tidying some data with double spaces in the records. The following function will remove double or triple spaces from a string and replace with just single spaces:

private string RemoveDoubleSpaces(string InputString)
{
string toReturn = InputString;

while (toReturn.Contains("  "))
{
//remove any double , triple spacing
toReturn = toReturn.Replace("  ", " ");
}

return toReturn;
}