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
 

6 Comments

  • Thats really useful. Will try that tomorrow. THx.

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

  • Thanks Brandon,
    I'm happy because it helped .

  • Thanks a great deal. It works.

    Thanks once again, Gracia mon ami

    Afrika
    :-)

  • do you have this in VB

  • 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:





    .style16
    {
    width: 80%;
    }
    .style15
    {
    width: 300px;
    }
    .style17
    {
    color: #FF0000;
    font-weight: bold;
    text-decoration: underline;
    }




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

    SelectCommand="SELECT * FROM [Project] WHERE ([Project Supervisor] =@UserName)"
    OldValuesParameterFormatString="original_{0}">








    This List will only display available projects


    &nbsp;







































    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

Comments have been disabled for this content.