Maximizing.ASP.dot.NET.Real.World.ObjectOriented.Development [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Maximizing.ASP.dot.NET.Real.World.ObjectOriented.Development [Electronic resources] - نسخه متنی

Jeffrey Putz

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید






Data Controls


We've been able to declaratively code data controls in ASP.NET since the start. Using simple templates, we've been able to format DataGrids and Repeaters with ease. The slightly more tricky part has been binding data to these controls. That got easier in v2.0 of ASP.NET with the addition of data source controls.

Consider a typical scenario where we need the name and city of our customers from a database, and we need to bind the resulting data to a DataGrid. Listing 13.1 demonstrates this scenario through the use of traditional means, in code, and declaratively using a SqlDataSource control.


The code listing shows the reference to a connection string using an ASP.NET expression. We'll get to expressions at the end of the chapter. For now, just understand that it references a connection string in web.config.

This is also a good time to point out that, despite having "Sql" in the name, the SqlDataSource control can actually get data from most OleDb data sources, including Microsoft Access, by using the attribute provider="System.Data.OleDb" in the control's declaration.

Listing 13.1. Traditional code vs. declarative code

Traditional code


SqlConnection connection =
new SqlConnection(ConfigurationSettings.ConnectionStrings["MyDatabase"]. ConnectionString);
string sql = "SELECT Name, City FROM Customers";
SqlCommand command = new SqlCommand(sql, connection);
SqlDataReader reader = command.ExecuteReader();
CustomerGrid.DataSource = reader;
CustomerGrid.DataBind();
reader.Close();
connection.Close();

Declarative code


<asp:SqlDataSource id="SqlDataSource1" runat="server"
dataSourceMode="DataReader"
connectionString="<%$ ConnectionStrings:MyDatabase %>"
selectCommand="SELECT ID, Name, City FROM Customers" />
<asp:DataGrid id="CustomerGrid" runat="server" dataSourceID="SqlDataSource1" />

Although one could certainly argue that this declarative approach to data binding doesn't represent a "best practice" of separating your application into discrete layers, it's hard to deny that this programming method saves time and is perfectly suited for a small application. It might also seem odd to put a control on the page like this when it doesn't actually render anything to the browser, but this is what declarative programming is all about! Instead of writing an extensive block of code in a traditional class, we simply declare it right in the HTML.

Perhaps a more realistic use of SqlDataSource is to pass in some kind of parameter. Imagine you have a form that enables you to search for customers by name. You can feed in a parameter to the SQL statement by providing the ID of the other control on the page, and that control's property to use as a parameter. This is shown in Listing 13.2, between the SelectParameters elements.

Listing 13.2. Passing in parameters to the SqlDataSource control

[View full width]


<asp:TextBox id="NameTextBox" runat="server" />
<asp:SqlDataSource id="SqlDataSource1" runat="server"
dataSourceMode="DataReader"
connectionString="<%$ ConnectionStrings:MyDatabase %>" selectCommand="SELECT ID, Name,
City FROM Customers WHERE Name = @Name">
<SelectParameters>
<asp:ControlParameter Name="Name" ControlID="NameTextBox" PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DataGrid id="CustomerGrid" runat="server" dataSourceID="SqlDataSource1" />

This code isn't that different from the code in Listing 13.1, except that the SQL statement has a parameter in its WHERE clause, and we've added the SelectParameters section to the control. There still isn't any "normal" code to go along with this; it all works with these simple declarations. The ControlParameter is one of several that we can use to feed data into the select command, including CookieParameter, FormParameter, Parameter, ProfileParameter, QueryStringParameter, and SessionParameter. That's a lot of flexibility because it lets you feed parameters in from virtually anywhere.

One of the examples frequently given to demonstrate data binding and updating data involves the combination of an editable DataGrid and a DataSet. The DataSet is populated in code, and event handlers are tied to the DataGrid to make it all work. Although this task is much easier than it might be on other platforms, it still requires a lot of work. The good news is that you can do it declaratively in ASP.NET v2.0.

Listing 13.3 shows an example using GridView and SqlDataSource controls together to make data editable. Figure 13.1 shows how the grid is rendered in the browser after one of the row's "edit" buttons has been clicked.

Listing 13.3. Using GridView and SqlDataSource together to declaratively create an editable table

[View full width]


<asp:SqlDataSource ID="SqlDataSource1" Runat="server"
DataSourceMode="DataReader"
ConnectionString="<%$ ConnectionStrings:MyDatabase %>"
SelectCommand="SELECT CustomerID, Name, City FROM Customers"
UpdateCommand="UPDATE Customers SET Name = @Name, City = @City WHERE CustomerID =
@original_CustomerID"
DeleteCommand="DELETE FROM Customers WHERE CustomerID =
@original_CustomerID">
</asp:SqlDataSource>
<asp:GridView ID="MyGrid" Runat="Server" DataSourceID="SqlDataSource1"
AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true" DataKeyNames="CustomerID" />

Figure 13.1. The GridView rendered in the browser

By adding the UpdateCommand and DeleteCommand attributes to the SqlDataSource control and adding the list of attributes to the GridView, including DataKeyNames to indicate our primary key, we can edit our data with ease. Gone are the many lines of plumbing code we would ordinarily have to write to achieve this functionality.

This discussion only scratches the surface of what you can do with declarative data controls, and I'm sure that countless books will be dedicated to the subject. ASP.NET v2.0 adds these new controls (including GridView and DetailsView) to make data manipulation even easier in general and significantly easier for a less experienced programmer.


/ 146