`
bcyy
  • 浏览: 1831103 次
文章分类
社区版块
存档分类
最新评论

Declarative Parameters(Data Access Tutorials 5)

 
阅读更多

Introduction

In the last tutorial we looked at displaying data with the GridView, DetailsView, and FormView controls bound to an ObjectDataSource control that invoked the GetProducts() method from the ProductsBLL class. The GetProducts() method returns a strongly-typed DataTable populated with all of the records from the Northwind database's Products table. The ProductsBLL class contains additional methods for returning just subsets of the products - GetProductByProductID(productID), GetProductsByCategoryID(categoryID), and GetProductsBySupplierID(supplierID). These three methods expect an input parameter indicating how to filter the returned product information.

The ObjectDataSource can be used to invoke methods that expect input parameters, but in order to do so we must specify where the values for these parameters come from. The parameter values can be hard-coded or can come from a variety of dynamic sources, including: querystring values, Session variables, the property value of a Web control on the page, or others.

For this tutorial let's start by illustrating how to use a parameter set to a hard-coded value. Specifically, we'll look at adding a DetailsView to the page that displays information about a specific product, namely Chef Anton's Gumbo Mix, which has a ProductID of 5. Next, we'll see how to set the parameter value based on a Web control. In particular, we'll use a TextBox to let the user type in a country, after which they can click a Button to see the list of suppliers that reside in that country.

Using a Hard-Coded Parameter Value

For the first example, start by adding a DetailsView control to the DeclarativeParams.aspx page in the BasicReporting folder. From the DetailsView's smart tag, select <New data source> from the drop-down list and choose to add an ObjectDataSource.

Add an ObjectDataSource to the Page

Figure 1: Add an ObjectDataSource to the Page (Click to view full-size image)

This will automatically start the ObjectDataSource control's Choose Data Source wizard. Select the ProductsBLL class from the first screen of the wizard.

Select the ProductsBLL Class

Figure 2: Select the ProductsBLL Class (Click to view full-size image)

Since we want to display information about a particular product we want to use the GetProductByProductID(productID) method.

Choose the GetProductByProductID(productID) method

Figure 3: Choose the GetProductByProductID(productID) method (Click to view full-size image)

Since the method we selected includes a parameter, there's one more screen for the wizard, where we're asked to define the value to be used for the parameter. The list on the left shows all of the parameters for the selected method. For GetProductByProductID(productID) there's only one – productID. On the right we can specify the value for the selected parameter. The parameter source drop-down list enumerates the various possible sources for the parameter value. Since we want to specify a hard-coded value of 5 for the productID parameter, leave the Parameter source as None and enter 5 into the DefaultValue textbox.

A Hard-Coded Parameter Value of 5 Will Be Used for the productID Parameter

Figure 4: A Hard-Coded Parameter Value of 5 Will Be Used for the productID Parameter (Click to view full-size image)

After completing the Configure Data Source wizard, the ObjectDataSource control's declarative markup includes a Parameter object in the SelectParameters collection for each of the input parameters expected by the method defined in the SelectMethod property. Since the method we're using in this example expects just a single input parameter, parameterID, there's only one entry here. The SelectParameters collection can contain any class that derives from the Parameter class in the System.Web.UI.WebControls namespace. For hard-coded parameter values the base Parameter class is used, but for the other parameter source options a derived Parameter class is used; you can also create your own custom parameter types, if needed.

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetProductByProductID" TypeName="ProductsBLL"> <SelectParameters> <asp:Parameter DefaultValue="5" Name="productID" Type="Int32" /> </SelectParameters> </asp:ObjectDataSource>

Note: If you're following along on your own computer the declarative markup you see at this point may include values for the InsertMethod, UpdateMethod, and DeleteMethod properties, as well as DeleteParameters. The ObjectDataSource's Choose Data Source wizard automatically specifies the methods from the ProductBLL to use for inserting, updating, and deleting, so unless you explicitly cleared those out, they'll be included in the markup above.

When visiting this page, the data Web control will invoke the ObjectDataSource's Select method, which will call the ProductsBLL class's GetProductByProductID(productID) method using the hard-coded value of 5 for the productID input parameter. The method will return a strongly-typed ProductDataTable object that contains a single row with information about Chef Anton's Gumbo Mix (the product with ProductID 5).

Information About Chef Anton's Gumbo Mix are Displayed

Figure 5: Information About Chef Anton's Gumbo Mix are Displayed (Click to view full-size image)

Setting the Parameter Value to the Property Value of a Web Control

The ObjectDataSource's parameter values can also be set based on the value of a Web control on the page. To illustrate this, let's have a GridView that lists all of the suppliers that are located in a country specified by the user. To accomplish this start by adding a TextBox to the page into which the user can enter a country name. Set this TextBox control's ID property to CountryName. Also add a Button Web control.

Add a TextBox to the Page with ID CountryName

Figure 6: Add a TextBox to the Page with ID CountryName (Click to view full-size image)

Next, add a GridView to the page and, from the smart tag, choose to add a new ObjectDataSource. Since we want to display supplier information select the SuppliersBLL class from the wizard's first screen. From the second screen, pick the GetSuppliersByCountry(country) method.

Choose the GetSuppliersByCountry(country) Method

Figure 7: Choose the GetSuppliersByCountry(country) Method (Click to view full-size image)

Since the GetSuppliersByCountry(country) method has an input parameter, the wizard once again includes a final screen for choosing the parameter value. This time, set the Parameter source to Control. This will populate the ControlID drop-down list with the names of the controls on the page; select the CountryName control from the list. When the page is first visited the CountryName TextBox will be blank, so no results are returned and nothing is displayed. If you want to display some results by default, set the DefaultValue textbox accordingly.

Set the Parameter Value to the CountryName Control Value

Figure 8: Set the Parameter Value to the CountryName Control Value (Click to view full-size image)

The ObjectDataSource's declarative markup differs slightly from our first example, using a ControlParameter instead of the standard Parameter object. A ControlParameter has additional properties to specify the ID of the Web control and the property value to use for the parameter (PropertyName). The Configure Data Source wizard was smart enough to determine that, for a TextBox, we'll likely want to use the Text property for the parameter value. If, however, you want to use a different property value from the Web control you can change the PropertyName value here or by clicking the "Show advanced properties" link in the wizard.

<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetSuppliersByCountry" TypeName="SuppliersBLL"> <SelectParameters> <asp:ControlParameter ControlID="CountryName" Name="country" PropertyName="Text" Type="String" /> </SelectParameters> </asp:ObjectDataSource>

When visiting the page for the first time the CountryName TextBox is empty. The ObjectDataSource's Select method is still invoked by the GridView, but a value of null is passed into the GetSuppliersByCountry(country) method. The TableAdapter converts the null into a database NULL value (DBNull.Value), but the query used by the GetSuppliersByCountry(country) method is written such that it doesn't return any values when a NULL value is specified for the @CategoryID parameter. In short, no suppliers are returned.

Once the visitor enters in a country, however, and clicks the Show Suppliers button to cause a postback, the ObjectDataSource's Select method is requeried, passing in the TextBox control's Text value as the country parameter.

Those Suppliers from Canada are Shown

Figure 9: Those Suppliers from Canada are Shown (Click to view full-size image)

Showing All Suppliers By Default

Rather than show none of the suppliers when first viewing the page we may want to show all suppliers at first, allowing the user to pare down the list by entering a country name in the TextBox. When the TextBox is empty, the SuppliersBLL class's GetSuppliersByCountry(country) method is passed in a null value for its country input parameter. This null value is then passed down into the DAL's GetSupplierByCountry(country) method, where it's translated to a database NULL value for the @Country parameter in the following query:

SELECT SupplierID, CompanyName, Address, City, Country, Phone FROM Suppliers WHERE Country = @Country

The expression Country = NULL always returns False, even for records whose Country column has a NULL value; therefore, no records are returned.

To return all suppliers when the country TextBox is empty, we can augment the GetSuppliersByCountry(country) method in the BLL to invoke the GetSuppliers() method when its country parameter is null and to call the DAL's GetSuppliersByCountry(country) method otherwise. This will have the effect of returning all suppliers when no country is specified and the appropriate subset of suppliers when the country parameter is included.

Change the GetSuppliersByCountry(country) method in the SuppliersBLL class to the following:

public Northwind.SuppliersDataTable GetSuppliersByCountry(string country) { if (string.IsNullOrEmpty(country)) return GetSuppliers(); else return Adapter.GetSuppliersByCountry(country); }

With this change the DeclarativeParams.aspx page shows all of the suppliers when first visited (or whenever the CountryName TextBox is empty).

All Suppliers are Now Shown by Default

Figure 10: All Suppliers are Now Shown by Default (Click to view full-size image)

Summary

In order to use methods with input parameters, we need to specify the values for the parameters in the ObjectDataSource's SelectParameters collection. Different types of parameters allow for the parameter value to be obtained from different sources. The default parameter type uses a hard-coded value, but just as easily (and without a line of code) parameter values can be obtained from the querystring, Session variables, cookies, and even user-entered values from Web controls on the page.

The examples we looked at in this tutorial illustrated how to use declarative parameter values. However, there may be times when we need to use a parameter source that's not available, such as the current date and time, or, if our site was using Membership, the User ID of the visitor. For such scenarios we can set the parameter values programmatically prior to the ObjectDataSource invoking its underlying object's method. We'll see how to accomplish this in the next tutorial.

分享到:
评论

相关推荐

    ASPNET_Data_Tutorial_5_CS__Declarative Parameters

    非常優秀的教程..看了它,你再也不會看國內的Asp.Net2.0教程了...可是所有東西都沒有完美的..它是英文的,所以,你要努力啦!

    Learning GraphQL: Declarative Data Fetching for Modern Web Apps

    Why is GraphQL the most innovative technology for fetching data since Ajax? By providing a query language for your APIs and a runtime for fulfilling queries with your data, GraphQL presents a clear ...

    declarative

    declarative services

    Declarative Data Processing With Java in Apache Flink

    ● Introduction to Apache Flink ● The DataSet API ● Runtime Execution ● Data Exchange ● Memory Management

    SwiftUI by Tutorials epub

    SwiftUI by Tutorials is designed to help you learn how to transition from the “old way” of building your app UI with UIKit, to the “new way” of building responsive UI with modern declarative ...

    Spring.Essentials.178398

    Explore Spring's comprehensive transaction support for declarative Transaction Management and its integration with Spring's data access abstractions Investigate Spring Data access mechanisms with ...

    Pure Declarative Programming in Swift, Among Other Things.zip

    Pure Declarative Programming in Swift, Among Other Things.zip,swift中的纯声明式编程

    Qt5Declarative_jll.jl

    Qt5Declarative_jll.jl (v5.15.2 + 0) 这是使用构造的自动生成的包。 原始的脚本可以在社区构建树上找到。 如果您有任何问题,请向Yggdrasil报告。 有关JLL软件包以及如何使用它们的更多详细信息,请参见...

    SwiftUI by Tutorials

    Every developer wants to build the most fluid and engaging declarative UI for their apps with as little code as possible. The latest addition to the Apple toolkit — SwiftUI — will help you do just ...

    Declarative Chrome Extension-crx插件

    1.Declarative Inspector检查选定的DOM元素的声明性视图模型属性(动作,ctx,数据,dataProvider,i18n,消息,onEvent)。 请参见声明性检查器示例的屏幕截图。 2.Declarative Tracer跟踪关键声明事件的执行流程...

    spring-framework-reference 4.3.8.RELEASE

    Framework supports declarative transaction management, remote access to your logic through RMI or web services, and various options for persisting your data. It offers a full-featured MVC framework, ...

    Declarative dependency management for Matlab.zip

    Declarative dependency management for Matlab.zip

    declarative-shadow-dom:自定义元素以声明方式创建Shadow DOM

    &lt;declarative&gt; 自定义元素以声明方式创建Shadow DOM 它应该与给出的建议紧密合作演示版安装使用安装组件:$ bower install declarative-shadow-dom --save 或 。用法如果需要,内置的导入自定义元素可扩展polyfill&...

    SwiftUI_by_Tutorials.zip

    With a declarative Swift syntax that’s easy to read and natural to write, SwiftUI works seamlessly with new Xcode design tools to keep your code and design perfectly in sync. Automatic support for ...

    declarative-parser-0.1.zip

    declarative-parser.zip,将bean定义组成正则表达式的解析器,用于

    SwiftUI_by_Tutorials_v1.0.0.zip (iOS13 & Swift5.1 & Xcode11)

    Build fluid and engaging declarative UI for your apps — using less code — with SwiftUI! Currently updated to: Platform: iOS13; Language: Swift5.1; Editor: Xcode11

    declarative_demo_f5

    描述 这是一个通过Ansible部署F5声明性入职(DO)和应用程序服务3(AS3)扩展以声明方式提供BIG-IP的示例。 输出 ...├── declarative_demo.yaml # Playbook ├── files # Extensions to be inst

    python SQLAlchemy的Mapping与Declarative详解

    主要介绍了python SQLAlchemy的Mapping与Declarative详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

Global site tag (gtag.js) - Google Analytics