Passing data to an SqlDataSource on a user control from code

This code snippet demonstrates how to programmaticaly set a parameter on a user control, and bind that paramter to an SqlDataSource that has been configured at design time.

The key to this functioning is a method in the usercontrol that sets a private field on the user control, and triggerin the DataBind() event on the GridView (causing the SqlDataSource to execute it’s query). The query has the SqlDataSource1_Selecting event set to fill in the paramter value.

This example binds to the customers table from the NorthWind database. The downloadable solution is attached to his page at the end.

Usercontrol markup

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="ControlDemo.WebUserControl1" %>
<hr/>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1">
    <Columns>
        <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
        <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
        <asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
        <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" />
        <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
        <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
        <asp:BoundField DataField="Region" HeaderText="Region" SortExpression="Region" />
        <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />
        <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
        <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
        <asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:ControlDemo.Properties.Settings.constring %>"
    OnSelecting="SqlDataSource1_Selecting"
    SelectCommand="SELECT * FROM [Customers] WHERE ([ContactName] LIKE '%' + @ContactName + '%')">
    <SelectParameters>
        <asp:Parameter Name="ContactName" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

Usercontrol code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace ControlDemo
{
    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
 
        private string _filterValue;
 
        /// <summary>
        /// This method is called from the controlling page, and sets the filter value
        /// and refreshes the gridview causing the query to be executed.
        /// </summary>
        /// <param name="value"></param>
        public void SetValue(string value)
        {
            _filterValue = value;
            GridView1.DataBind();
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
 
        /// <summary>
        /// This event on the sqldatasource replaces the paramter configured at design time
        /// with the runtime value provided by the setter.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.Parameters["@ContactName"].Value = _filterValue;
        }
    }
}