Creating Custom Parameters for Data source Controls

Asp.net provide various types of parameters , like session parameter which  get its value from session ,control parameter which get its value from form control , and there is also many other parameters each one used to read the parameter value from different location .

But sometimes , those parameters doesn't fit your needs , for example , if you want a parameter that get its value from the UserName of the current logged in user ( User.Identity.Name ) , in this case you may end up by using the default <asp:Parameter > which requires to set its value manually in the data Source Selecting , Updating , Inserting , or deleting event.

However , you could create a custom parameter that can be used in all places that you want to use the Current logged in UserName as  a parameter in the data source controls  , this can be accomplished by inheriting from System.Web.UI.WebControls.Parameter class , and by overriding it's Evaluate method.

Note: the evaluate method will be called automatically by the framework .

The custom parameter can be placed in App_Code folder or in a separate class library if you want to use it in other projects.

This is the custom UserParameter class:

   11 namespace CustomControls
   12 {
   13     /// <summary>
   14     /// Summary description for UserParameter
   15     /// </summary>
   16     public class UserParameter : System.Web.UI.WebControls.Parameter
   17     {
   18         protected override object Evaluate(HttpContext context, System.Web.UI.Control control)
   19         {
   20             if (context.User != null && !string.IsNullOrEmpty(context.User.Identity.Name))
   21             {
   22                 return context.User.Identity.Name;
   23             }
   24 
   25             return null;
   26         }
   27     }
   28 }

 Now to use the UserName parameter,
 
First thing , you should register the custom control ,
 
<%@ Register Namespace="CustomControls" TagPrefix="cc1" %>

Then you can use it as a parameter for your data source :

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<$ConnectionStrings:DatabaseConnectionString %>"
    SelectCommand="SELECT [ProjectId], [Name], [UserName] FROM [Projects] where [UserName]=@UserName">
     <SelectParameters>
      <cc1:UserParameter Name="UserName" />
    </SelectParameters>
  </asp:SqlDataSource>
 
 
Hope it helps
Anas Ghanem
 
Published Wednesday, October 15, 2008 12:56 AM by anas
Filed under:

Comments

# re: Creating Custom Parameters for Data source Controls

Thursday, October 23, 2008 4:14 PM by fr3dr1k

Thats really useful. Will try that tomorrow. THx.

# re: Creating Custom Parameters for Data source Controls

Wednesday, October 29, 2008 9:04 AM by Brandon

Thank you for this piece of code.  This was the exact thing I spent a lot of time searching for.

# re: Creating Custom Parameters for Data source Controls

Wednesday, October 29, 2008 9:18 AM by anas

Thanks Brandon,

I'm happy because it helped .

# re: Creating Custom Parameters for Data source Controls

Saturday, December 20, 2008 4:35 PM by afrika

Thanks a great deal. It works.

Thanks once again, Gracia mon ami

Afrika

:-)

# re: Creating Custom Parameters for Data source Controls

Friday, January 01, 2010 1:17 AM by crosscreek

do you have this in VB

# re: Creating Custom Parameters for Data source Controls

Wednesday, February 24, 2010 2:40 PM by aaronmetcalfe

have tried implementing this into my code, it doesnt throw up any errors but doesnt do anything. below is my code, anyone got any idea's.

PAGE:

<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Copy of Project List.aspx.vb" Inherits="Logged_IN_Projects_Project_List" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

   <style type="text/css">

       .style16

       {

           width: 80%;

       }

       .style15

       {

           width: 300px;

       }

   .style17

   {

       color: #FF0000;

       font-weight: bold;

       text-decoration: underline;

   }

   </style>

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

   <p>

       <asp:SqlDataSource ID="SqlDataSource1" runat="server"

   ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

   ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"

           SelectCommand="SELECT * FROM [Project] WHERE ([Project Supervisor] =@UserName)"

           OldValuesParameterFormatString="original_{0}">

           <SelectParameters>

               <asp:FormParameter FormField="username" Name="Project_Supervisor"

                   Type="String" />

           </SelectParameters>

</asp:SqlDataSource>

   </p>

<p class="style17">

       This List will only display available projects</p>

<p>

       &nbsp;<table align="center" class="style16">

           <tr>

               <td>

                   <table align="center" class="style15">

                       <tr>

                           <td>

                               <asp:GridView ID="GridView1" runat="server" AllowPaging="True"

                                   AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"

                                   DataKeyNames="Project ID" DataSourceID="SqlDataSource1" ForeColor="#333333"

                                   GridLines="None">

                                   <RowStyle BackColor="#EFF3FB" />

                                   <Columns>

                                       <asp:BoundField DataField="Project ID" HeaderText="Project ID" ReadOnly="True"

                                           SortExpression="Project ID" />

                                       <asp:BoundField DataField="Project Title" HeaderText="Project Title"

                                           SortExpression="Project Title" />

                                       <asp:BoundField DataField="Project Year" HeaderText="Project Year"

                                           SortExpression="Project Year" />

                                       <asp:BoundField DataField="Project Specification"

                                           HeaderText="Project Specification" SortExpression="Project Specification" />

                                       <asp:BoundField DataField="Project Supervisor" HeaderText="Project Supervisor"

                                           SortExpression="Project Supervisor" />

                                       <asp:BoundField DataField="Project Student" HeaderText="Project Student"

                                           SortExpression="Project Student" />

                                       <asp:BoundField DataField="Project Assesor" HeaderText="Project Assesor"

                                           SortExpression="Project Assesor" />

                                       <asp:BoundField DataField="Project Available" HeaderText="Project Available"

                                           SortExpression="Project Available" />

                                   </Columns>

                                   <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

                                   <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

                                   <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

                                   <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

                                   <EditRowStyle BackColor="#2461BF" />

                                   <AlternatingRowStyle BackColor="White" />

                               </asp:GridView>

                           </td>

                       </tr>

                   </table>

               </td>

           </tr>

       </table>

   </p>

   <p>

   </p>

</asp:Content>

SOURCE:

Partial Class Logged_IN_Projects_Project_List

   Inherits System.Web.UI.Page

   Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting

   End Sub

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

   End Sub

End Class

Public Class UserParameter

   Inherits System.Web.UI.WebControls.Parameter

   Protected Overloads Overrides Function Evaluate(ByVal context As HttpContext, ByVal control As System.Web.UI.Control) As Object

       If context.User IsNot Nothing AndAlso Not String.IsNullOrEmpty(context.User.Identity.Name) Then

           Return context.User.Identity.Name

       End If

       Return Nothing

   End Function

End Class

Leave a Comment

(required) 
(required) 
(optional)
(required)