See the question and my original answer on StackOverflow

When you start using ASP.NET's DataSourceControls (ObjectDataSource, SqlDataSource, AccessDataSource, etc.) and their counterparts DataBoundControls (DropDownList, DetailsView, ListView, FormView, GridView), you really want to forget about ASP.NET lifecycle (and stop pulling hair), and if you do it well, you can even forget about code-behind code (aspx.cs files) because now the system can be pretty automatic between datasources and databound controls.

In fact this paradigm (that has appeared only starting with .NET 2.0) really helps to focus on declarative HTML-like code.

A DataSourceControl uses a collection of objects of type Parameter as parameters to the methods it uses. For example, the ObjectDataSource's SelectMethod uses the SelectParameters property (of ParameterCollection type).

You can define these parameters declaratively. Let's take an example:

<form id="form1" runat="server">
<div>
    <asp:TextBox ID="MyTextBox" Text="3" runat="server" />
    <asp:Button runat="server" Text="Run" />

    <asp:GridView ID="myGridView" runat="server" DataSourceID="myDataSource" />
    <asp:ObjectDataSource ID="myDataSource" runat="server" 
             SelectMethod="GetSearchResults"
             TypeName="WebApplication1.Code.MyModel">
        <SelectParameters>
            <asp:ControlParameter ControlID="MyTextBox" PropertyName="Text" Name="myCount" />
        </SelectParameters>
    </asp:ObjectDataSource>
</div>
</form>

Here, myDataSource defines a GetSearchResults as the SelectMethod (I've omitted your other parameters but the idea is the same) method on a MyModel class in some class. It also defines a parameter named myCount. This parameter is a ControlParameter (there are others): it will connect to the ASP.NET control MyTextBox which happens to be defined as a TextBox control, and will use the TextBox's Text property as the the value of the myCount parameter.

Here is the code of the object model:

namespace WebApplication1.Code
{
    public class MyModel
    {
        public string Name { get; set; }

        public static IEnumerable GetSearchResults(int myCount)
        {
            for (int i = 0; i < myCount; i++)
            {
                yield return new MyModel { Name = "item " + i };
            }
        }
    }
}

As you see, the method also has a myCount parameter defined (it's case sensitive, and yes, ASP.NET will convert automatically from string to int, it's almost magic, it uses TypeConverters under the hood), so everything will work as expected, without any code-behind code. It's some kind of an MV pattern (M model V view) and DataSourceControl/DataBoundControl do the binding.

So, you have to think this way now, and use parameters. If the provided list of parameters (QueryString, Cookie, Form, Profile, Route, Session) is no enough, you can provide your own. For example I can define a special parameter that will get myCount randomly (it's just an example :-):

I can use it like this, and I can define custom arguments for this parameter:

<%@ Register Namespace="WebApplication1.Code" TagPrefix="my" Assembly="WebApplication1" %>
...
<SelectParameters>
    <my:RandomParameter Name="myCount" Min="10" Max="20" />
</SelectParameters>

The custom parameter type code:

public class RandomParameter : Parameter
{
    protected override object Evaluate(HttpContext context, Control control)
    {
        // you can get to page or environment from here with 'context' and 'control' parameters
        return new Random(Environment.TickCount).Next(Min, Max);
    }

    [DefaultValue(1)]
    public int Min
    {
        get
        {
            object o = ViewState["Min"];
            return o is int ? (int)o : 1;
        }
        set
        {
            if (Min != value)
            {
                ViewState["Min"] = value;
                OnParameterChanged();
            }
        }
    }

    [DefaultValue(10)]
    public int Max
    {
        get
        {
            object o = ViewState["Max"];
            return o is int ? (int)o : 10;
        }
        set
        {
            if (Max != value)
            {
                ViewState["Max"] = value;
                OnParameterChanged();
            }
        }
    }
}