FAHNZ

MODE

Nov 17 2009

Bubbling UserControl Events

This scenario has bitten me before. User control events fire after page events. This can be a problem if an update within the user control needs to be reflected in the outer page container. The solution is to “bubble” the event from the user control into the page so it can react accordingly. So, as much for my reference as anyone else’s, here’s some quick ‘n dirty code to bubble events from an ASP.NET user control to the page that contains it:

User Control Code:

public partial class SamplePage : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    // "SampleUserControl1" is the ID of the control that has already been placed in the HTML of the page
    SampleUserControl1.BubbleEventHappened += new EventHandler(BubbleEventHappened);
  }

  private void BubbleEventHappened(object sender, EventArgs e)
  {
    // code to react to bubbled event goes here
  }
}

Page (that contains the User Control) Code:

public partial class SamplePage : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    // "SampleUserControl1" is the ID of the control that has already been placed in the HTML of the page
    SampleUserControl1.BubbleEventHappened += new EventHandler(BubbleEventHappened);
  }

  private void BubbleEventHappened(object sender, EventArgs e)
  {
    // code to react to bubbled event goes here
  }
}

There. That wasn’t so bad!


Jul 6 2009

ASP.NET Membership Woes

The problem:

I kept on getting foreign key constraint errors when trying to use ASP.NET (2.0) DeleteUser method:

Membership.DeleteUser(<username>);

All my research on the internet pointed to it being a permissions problem. Like many of the posts I found, this worked fine in my local development database, but in production it would yield this error:

The DELETE statement conflicted with the REFERENCE  constraint
"FK__aspnet_Me__UserI__15502E78". The conflict occurred in  database
"dbname", table "dbo.aspnet_Membership", column 'UserId'.
The  statement has been terminated.

After hours of experimentation with user permissions in the database and research on the problem, the support team at the hosting company I was working with found the solution of setting the references foreign key constraints to cascade upon deletion. I went into SQL Server Management Studio and updated the FKs that were referenced in the errors to cascade upon deletion and voila! Problem solved. Good job CrystalTech support and especially Geoff.

Update: I looked into my local database (that has been working properly) and saw that the foreign keys are not set to cascade deletions, so it still may be some strange permissions issue. Regardless, this seems like an acceptable workaround and it did get those deletions to work (and the usernames freed up).