Creating Custom Parameter Types for DataSource Controls in ASP.NET 2.0

I've posted in the past about how to create your own custom datasource controls using ASP.NET 2.0.   One of the other cool new ASP.NET 2.0 extensibility points is the ability to create your own custom datasource parameter objects.  These can then be used with any datasource control (including built-in ones like ObjectDataSource or SqlDataSource -- or any other custom ones).  That way, you could write code like this (where I have a sortable, pagable GridView binding against a business object or DAL like this):

<asp:GridView ID="GridView1" runat="server" DataSourceID="DS1" AllowPaging="True" AllowSorting="True">

    <Columns>

        <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />

        <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />

    </Columns>

</asp:GridView>

 

<asp:ObjectDataSource ID="DS1" runat="server" SelectMethod="GetAllCategories" TypeName="NorthwindTableAdapters.CategoriesTableAdapter"

 

    <SelectParameters>

        <asp:QueryStringParameter Name="CategoryName" QueryStringFiled="CategoryName" />

        <scottgu:MyCustomParam Name="Param2" SomeAttribute="SomeValue" />

    </SelectParameters>

 

</asp:ObjectDataSource>

Note the "scottgu:MyCustomParam" parameter above in the <SelectParameters> collection -- this could implement whatever logic we want to generate the "Param2" value (for example: it could access a property on a page, access the user.identity.name value, or execute any code we want).  This can be very useful when you have a lot of parameter types throughout projects that you want to re-use.

Fredrik Normen (who has a great ASP.NET blog!) has published a cool posting that shows how to implement parameters like the one above.  You can read all about it here.

Update: Eilon Lipton (who is the ASP.NET developer who built many of the data controls) just posted a great blog entry on how to-do this here as well.

Hope this helps -- and special thanks to Fredrik for writing this entry.

Scott

 

4 Comments

  • Great feature! I didn't know this.

  • Hi Scott,



    This is killing me :-(



    I tried the above, but cannot get it to work, at least not using a web site, with the parameter defined in App_Code.



    In both your two post, and Fredrik's....neither of you included the code you need to place in the ASPX file, so I do not have a complete example (MSDN docs, while they include a demo of extending from Parameter, also do not include a complete example).



    I tried creating my own, but of course ASP.NET returns a compile error because it does not know what &lt;Dav:MyParam /&gt; is....So then I tried including a &lt;%@ Register TagPrefix=&quot;Dav&quot; TagName=&quot;MyParam&quot; %&gt;, but ran into the next problem that I need to specify either a Src or Assembly attribute. When I tried to specify the Src attribute, I got an error that it must reference a user control. Because I am placing the code in App_Code, I am pretty sure you do not provide a *placeholder* string that is substituted with the real assembly name at compile time....So I do not think I can reference the generated App_Code assembly.



    Am I correct that this technique will only work if you create a separate project to include the Parameter code, and reference the output assembly so it is in the bin directory?



    If that is true, this feature is sadly of less use of me.....I am working hard on a project to *simplify* and dumb down our architecture using the nice new features of ASP.NET 2, but having to create a separate project/assembly just for those 5 lines of code misses the point entirely.



    Is there any way of getting this to work with a simple web site, just using App_Code for the Parameter class?



    Thanks.

  • Hi Scott,



    Don't waste your time responding to my previous comment as I figured it out myself. My problem was that I did not have my derived parameter class within a namespace, and it appears to work when I do the following:



    Place this in App_Code (you *must* include a namespace):

    namespace MyNamespace

    {

    public class MyParameter : System.Web.UI.WebControls.Parameter

    {

    protected override object Evaluate(HttpContext context, Control control)

    {

    return &quot;Hello&quot;;

    }

    }

    }



    This directive must be included at the top of the ASPX page:

    &lt;%@ Register Namespace=&quot;MyNamespace&quot; TagPrefix=&quot;MyPrefix&quot; %&gt;



    This is an example datasource using the param:

    &lt;asp:ObjectDataSource ID=&quot;ods1&quot; runat=&quot;server&quot; SelectMethod=&quot;GetItems&quot; TypeName=&quot;MyAdapter&quot;&gt;

    &lt;SelectParameters&gt;

    &lt;MyPrefix:MyParameter Name=&quot;param&quot; /&gt;

    &lt;/SelectParameters&gt;

    &lt;/asp:ObjectDataSource&gt;



    This all works perfectly, so thanks for your help :-)



    David

  • Thank you Scott!

    Let me know. Why wheni create the custom handler of event we xcan show it only in SOURCE mode into VWD, but into DISIGRER mode we at all not have any EVENT?

    Really, it is big breack for fast programming. I hope that it is only timely bug and your next issue will fix that problem.

    SIncerely, LukCAD

Comments have been disabled for this content.