Asp.net| Control | data | data source
Summary:This article is the second in a series of articles that introduce data source controls. In this article, Nikhil highlights how to add support to add parameters to a query for a control.
The data source control needs to use parameter values to specify which data you want to select, or to specify how to modify the data and what data to modify. Typically, a page contains some UI to define the parameters that must be used in a select operation, and the data-bound control provides parameter values for inserts, updates, and deletes. However, in either case, two phenomena may occur at the same time. In part 1th, the data source control exposes the ZipCode property, which can be declaratively set, or set in code to respond to user actions. Parameters are designed to complete this scenario in a declarative (and extensible) manner.
Introduction
The Parameter base class represents a common parameter. Microsoft Visual Studio 2005 provides parameters such as QueryStringParameter to request data from query string parameters to the data source. Another very useful parameter is ControlParameter, which allows you to request data from any of the control properties. If the built-in parameter types do not meet your requirements, you can define your own parameter types. This allows you to make the page irrelevant to the glue code, but to encapsulate the code neatly in the parameter implementation.
In addition to requesting values from different sources, these parameters can also track changes to values and notify the data sources to which the changes belong, triggering a data source change notification, and eventually triggering a data-binding operation in a data-bound control. In short, this is the rationale behind the main declarative detailed scenario when using Controlparameters.
Example
The parameter functionality is added to the WeatherDataSource, and then further elaborated.
public class Weatherdatasource:datasourcecontrol {
public static readonly String zipcodeparametername = "ZipCode";
...
Private ParameterCollection _parameters;
Private ParameterCollection Parameters {
get {
if (_parameters = = null) {
_parameters = new ParameterCollection ();
_parameters. Parameterschanged
+ + new EventHandler (this. onparameterschanged);
if (istrackingviewstate) {
((IStateManager) _parameters). TrackViewState ();
}
}
return _parameters;
}
}
...
public string Getselectedzipcode () {
if (_parameters!= null) {
Parameter zipCodeParameter =
_parameters[zipcodeparametername];
if (zipcodeparameter!= null) {
IOrderedDictionary parametervalues =
_parameters. GetValues (context, this);
Return (string) parametervalues[zipcodeparameter.name];
}
}
return ZipCode;
}
protected override void LoadViewState (object state) {
Object basestate = null;
if (state!= null) {
Pair p = (Pair) state;
Basestate = P.first;
if (P.second!= null) {
((IStateManager) Parameters). LoadViewState (P.second);
}
}
Base. LoadViewState (basestate);
}
protected override void OnInit (EventArgs e) {
Page.loadcomplete + = new EventHandler (this. Onpageloadcomplete);
}
private void Onpageloadcomplete (object sender, EventArgs e) {
if (_parameters!= null) {
_parameters. Updatevalues (context, this);
}
}
private void Onparameterschanged (object sender, EventArgs e) {
Currentconditionsview.raisechangedevent ();
}
protected override Object SaveViewState () {
Object basestate = base. SaveViewState ();
Object parameterstate = null;
if (_parameters!= null) {
Parameterstate = ((IStateManager) _parameters). SaveViewState ();
}
if ((basestate!= null) | | (parameterstate!= null)) {
return new Pair (Basestate, parameterstate);
}
return null;
}
protected override void TrackViewState () {
Base. TrackViewState ();
if (_parameters!= null) {
((IStateManager) _parameters). TrackViewState ();
}
}
}
[1] [2] Next page