User control injection: pass in parameters?

Dec 6, 2009 at 12:20 PM

Is there any way to specify parameters for the user control being injected? E.g. I would like to create a generic "transform this XML file using this XSLT file" user control, but in that case, I need to be able to specify the XML and XSLT file names when I inject the user control onto my static page:

[usercontrol:~/User Controls/XmlTransformer.ascx] ....... but how do I specify parameters here??


Any ideas?

Dec 7, 2009 at 4:02 AM

It looks like this is supported.  Looking at the code, it looks like the properties should come after the name of the control, semi-colon delimited.

[usercontrol:~/User Controls/XmlTransformer.ascx property1=value;property2=value]

Dec 11, 2009 at 9:17 AM

However, it doesn't work :-(

I tried this in a new page:

[usercontrol:~/User controls/XslTransform.ascx]

works fine.

Now I tried to specify two parameters, which I know are valid:

[usercontrol:~/User controls/XslTransform.ascx DocumentSource="~/App_Data/currentstats.xml";TransformSource="~/currentstats.xsl"]

and all I get is:

ERROR - UNABLE TO LOAD CONTROL : ~/User controls/XslTransform.ascx DocumentSource="~/App_Data/currentstats.xml";TransformSource="~/currentstats.xsl"



Dec 11, 2009 at 4:49 PM

Remove the quotation marks around the property values.  I just tested it, and the quotation marks get passed in to the properties along with the values themselves.  Without the quotation marks, the property values get treated as-is.

[usercontrol:~/User controls/XslTransform.ascx DocumentSource=~/App_Data/currentstats.xml;TransformSource=~/currentstats.xsl]

Dec 11, 2009 at 7:00 PM

Hi Ben,

I tried your suggestion and removed the quotation marks - still the same result:

ERROR - UNABLE TO LOAD CONTROL : ~/User controls/XslTransform.ascx DocumentSource=~/App_Data/currentstats.xml;TransformSource=~/currentstats.xsl

:-( :-( :-(

Dec 11, 2009 at 8:03 PM


I'm guessing you're trying to do this with a Post, not a Page, right?  Both Posts and Pages support this [usercontrol] syntax.  We can pretty easily modify the source code for Pages to try and get more information about the error occurring.  So if you're trying to put the [usercontrol] into a Post, then for testing purposes, try putting the same [usercontrol] into a Page (on the Pages tab in the control panel).

Once you've done that, there's a file in the root of the blog named page.aspx.cs.  In there is a section named InjectUserControls.  Within that, you'll see this code:

catch (Exception)
    divText.Controls.Add(new LiteralControl("ERROR - UNABLE TO LOAD CONTROL : " + myMatch.Groups[1].Value));

... replace that code with ...

catch (Exception ex)
    divText.Controls.Add(new LiteralControl("ERROR - UNABLE TO LOAD CONTROL : " + myMatch.Groups[1].Value));
    divText.Controls.Add(new LiteralControl(".  Error Message: " + ex.Message));

With this code, in addition to getting the "ERROR - UNABLE TO LOAD CONTROL ...." message, you should also get a more informative error message.  Depending on what error message you see, we might need to try and get more information, or the error message alone might tell us what the problem might be.

Dec 11, 2009 at 8:36 PM

Oh goodness - this is soooooo embarassing.... of course it works! The error is all mine.....

I had this user control XslTransform.ascx which contains an <asp:xml> container that does the actual xslt transformation; the asp:xml container has "DocumentSource" and "TransformSource".

But in my very limited wisdom, I chose to surface those properties on my user control using different property names (XmlDocument and XsltDocument)....... but from my page, I was using the property names .....

Sheeesh! So once I started setting values to the right property names, everything just worked like a charm !

Thanks, Ben, for your patience and persistence! Highly appreciated !



Dec 11, 2009 at 9:49 PM

Great ... glad it's working now!