BlogEngine.Net Control Parameters

May 18, 2008
by:   Tim Stanley

How does one add or pass parameter to a user control in BlogEngine.Net?  This may be obvious to others already, but I thought I'd share a quick note on what I found. 

Create The Parameter

First, define the parameter value in the user control.

   1: private String _Name = null;
   2: public String Name
   3: {
   4:     get { return _Name; }
   5:     set { _Name = value; }
   6: }

The parameter now can be set either by embedding the control on another page or user control, or by passing a parameter in the [ usercontrol:~\Control.apx Name=xxx;] format.

Passing Parameters In Pages Or Controls

   1: Register src="../../UserCustom/PageInclude.ascx" mce_src="../../UserCustom/PageInclude.ascx" tagname="PageInclude" tagprefix="uc6"
   2:  
   3: <uc6:PageInclude ID="PageInclude1" runat="server" runat="server" Name="P Test" />

By adding a reference to the user control to a page or other user control, then the values for the parameters can be set. Without the runat="server" value, values will not be passed to the control.

In this particular example, the Name="Some Page" sets the earlier defined _Name value to "Some Page".

Passing Parameters In BlogEngine.Net

BlogEngine.Net behaves very similarly.  All parameters must be separated by a ";" semi-colon character.  If parameters are passed that are not supported, BlogEngine.Net 1.3 code will not render the control and an error message will be displayed.  The example below has spaces added after the bracket.  These need to be removed in the actual page or posting that sets these parameters.

   1: [ usercontrol:~\UserCustom\PageInclude.ascx Name=SomeName; ] 

 

Multiple parameter example:

   1: [ usercontrol:~\UserCustom\PageInclude.ascx Name=Some Name; Value=Some Value; ]
Add to favorites Send to a friend Digg It! DZone It! StumbleUpon Technorati Reddit Del.icio.us NewsVine Furl BlinkList