Parameters for using data source controls in ASP. NET2.0

Source: Internet
Author: User

The data source control requires parameter values to specify the data to be selected, or how to modify and modify the data. Generally, a page contains some UIS to define the parameters that must be used in the selection operation. The data binding control provides parameter values for insertion, update, and deletion. However, in either case, two phenomena may occur simultaneously.

The Data Source Control reveals the ZipCode attribute, which can be declared or encoded to respond to user operations. Parameters are designed to be declarative and scalable.
Introduction
The Parameter base class represents a common Parameter. Microsoft Visual Studio 2005 provides parameters such as QueryStringParameter to request data from the query string parameter to the data source. Another very useful parameter is ControlParameter, which allows you to request data from any control property. If the built-in parameter type does not meet your requirements, you can define your own parameter type. In this way, the page is unrelated to the bonding code, but the code is neatly encapsulated in the parameter implementation.
In addition to request values from different sources, these parameters can also track changes to values and notify the data source to which these changes belong, which in turn triggers a data source change notification, finally, the data binding operation is triggered in the data binding control. In short, this is the principle that the main declarative details are based on when ControlParameters is used.
Example
The parameter function will be added to Weather Data Source for further explanation.

Public class WeatherDataSource: performancecontrol {
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 ();
}
}
}

Microsoft ASP. NET provides ParameterCollection, which can be used as is. It also includes the change tracking and status management functions. You only need to call the APIs of the set to merge these functions. In addition, you can reveal the set as a property outside the control. In the above Code, the key points to note are:
• The data source control reveals a ParameterCollection type property so that developers can add parameters that represent the zip code value to be used. If a parameter has been set, this parameter is used. Otherwise, the ZipCode attribute value is used.
• This control replaces the State management-related methods to request the built-in state management function in ParameterCollection.
• The control uses the new LoadComplete event of the page lifecycle to update parameter values. It registers these values by replacing OnInit. If any parameter value is changed during initialization, sending back, or page encoding when LoadComplete is triggered, the data source control also registers the ParametersChanged event caused by ParameterCollection. As in the above case, if the ZipCode attribute is set, a change notification will be triggered, indicating to the data binding control that it needs to re-execute the Data Binding operation, which will happen during the PreRender ).
• The need to participate in the lifecycle is one reason why data sources are implemented as controls, even non-visual controls. Another reason is to enable the data binding control to use FindControl by using its performanceid attribute, and to obtain the benefits of the INamingContainer-based hierarchical Name field so as to implement nested data solutions, the method is to place a data source control in the template and repeat it once in each row ). The fact that data sources are controls has long been the focus of debate-I hope this can illustrate some arguments for this problem.
In this performanceview, you only need to call GetSelectedZipCode, instead of directly using the ZipCode attribute. In addition, the data source view code is changed to return null without ZipCode being selected, rather than throwing an exception), which causes the data binding control to display the "null" view. This is usually a convention, but in retrospect, it should be an indispensable aspect of the data source control semantics.
Private sealed class weatherperformanceview: performanceview {
...
Internal Weather GetWeather (){
String zipCode = _ owner. GetSelectedZipCode ();
If (zipCode. Length = 0 ){
Return null;
}
WeatherService weatherService = new WeatherService (zipCode );
Return weatherService. GetWeather ();
}
}

The complete code looks like this. The following is an updated usage example. This example is declarative.
Zip Code: <asp: TextBox runat = "server" id = "zipCodeTextBox"/>
<Asp: Button runat = "server" Text = ""/>
<Hr/>
<Asp: FormView runat = "server" performanceid = "weatherDS">
<ItemTemplate>
<Asp: Label runat = "server"
Text = '<% # Eval ("Temperature ",
"The current temperature is {0 }. ") %> '/>
</ItemTemplate>
</Asp: FormView>
<Nk: WeatherDataSource runat = "server" id = "weatherDS">
<Parameters>
<Asp: ControlParameter Name = "ZipCode" ControlID = "zipCodeTextBox"/>
</Parameters>
</Nk: WeatherDataSource>

Note that Text is not specified in the tag as the property to be searched on the ControlParameter tag. ControlParameter automatically calculates the default attribute to be used when no attribute is specified. It performs this by checking ControlValueAttribute in the class. TextBox defines Text as an attribute containing its "control value. In addition to traditional input controls, this concept also applies to multiple controls. For example, GridView reveals its SelectedDataKey as a "control value ". This is a new thing. Control developers should consider it from now on to better integrate with ControlParameter.

Next article: asynchronous data access to data source controls in ASP. NET2.0

(

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.