Thông tin tài liệu:
Sử dụng Bound Controls với Web Forms Tôi muốn tạo một Form Web cho phép người dùng của tôi để xem dữ liệu giống như Windows Forms của tôi và có thể sử dụng dữ liệu ràng buộc các điều khiển trên nó. Làm thế nào để sử dụng dữ liệu ràng buộc điều khiển trên một Form Web?
Nội dung trích xuất từ tài liệu:
Use Bound Controls with Web Forms 5.1 Use Bound Controls with Web FormsI want to create a Web Form that allows my users to view data much like my WindowsForms and be able to use data bound controls on it. How do I use data bound controls ona Web Form?TechniqueThe data objects that you used in Chapter 1, Developing Windows Forms Using BoundControls, including OleDbDataAdapter, datasets, and so on, will be used with WebForms as they would with Windows Forms. The main difference between WindowsForms and Web Forms in this case is the extra steps needed to handle round trips to theserver from the client machines. For discussion on OleDBDataAdapters and datasets, seeChapter 1.The IsPostBack PropertyOne property that you will be using when youre developing Web forms is the IsPostBackproperty, which is used in the Load event of the Web Form. Thats right-Web Forms nowhave an event model much like Windows Forms. You can now work with the Web Formproperties and Web server control properties from the Web pages class module.The IsPostBack property is set to True if the load event is fired on a round trip from theserver. Therefore, the first time a Web Form is loaded, IsPostBack is False.Web Server Controls Versus HTML ControlsWithin ASP.NET Web Forms, you now have the ability to use either your classic HTMLcontrols, which are available for compatibility purposes, or the new Web server controls,which, because they run on the server, have the following advantages: • You can access Web server control properties and methods from the Web Forms class module, but not with HTML controls. • Because Web server controls are rendered from the server side, on the client side they come through as pure HTML, and they are compatible with more browsers and earlier versions. • Web server controls generally have more features than their HTML counterparts, and in some cases, they can be bound to data.Note You can change some of the HTML controls to Web server controls by placing the control on the Web Form, right-clicking on the control, and choosing Run as Web Server Control from the pop-up menu. After choosing this menu option, you can see the object in your code behind your page.Note Although you will be dealing with a ListBox Web server control, it has different properties and methods than the Windows Form ListBox control. These are discussed in the following steps.The AutoPostBack PropertyWeb server controls have a property called AutoPostBack. This property tells .NET topost back to the server, which is something you need to do if you want to have an eventfire off, such as the SelectedIndexChanged event of the lstCustomers list box. This is alsotrue for Button controls that are used.The DataBind MethodWhen youre binding Web server controls-in this case, a ListBox control-to data such asDataSet or DataTable objects, you need to invoke the DataBind method of the controlthat is being bound. You need to do this when data changes, as you will see in thefollowing steps.StepsOpen and run the Visual Basic .NET-Chapter 5 solution. From the main page, click onthe hyperlink with the caption How-To 5.1: Using Data Bound Controls with WebForms. When the Web Form loads, you will see a list box filled with customers, and thedetails for the first customer will be displayed in the list (see Figure 5.1). Figure 5.1. Arrange the controls on the form you created to look like this form.To start off, you will be creating a Web Form that is similar to a Windows Form that wascreated in Chapter 1. You will actually be creating the form exactly the way you did inthe first chapter with the Windows Form, with the exception of a few commands in thecode. 1. Create a Web Form. Then place the controls listed in Table 5.1 with the following properties set. You will be using Northwind for the database to connect to. Table 5.1. Label, TextBox, ListBox, and Command Button Control Property Settings Object Property Setting OleDbDataAdapter ID odaCustomerList SelectCommand Select CustomerID, CompanyName From Customers DataSet ID dsCustomerList OleDbDataAdapter ID odaCustomerIndividual SelectCommand Select * From Customers Where Customer ID = ?DataSet ID dsCustomerIndividualListBox ID lstCustomers DataSource dsCustomerList DataTextField CompanyName DataValueField CustomerID AutoPostBack TrueLabel Caption Customer IDLabel Caption Company NameLabel Caption ContactLabel Caption Contact TitleLabel Caption AddressLabel Caption CityLabel Caption RegionLabel Caption CountryLabel Caption PhoneLabel Caption FaxTextBox ID txtCustomerID Text dsCustomerIndividual - Customers.CustomerIDTextBox ID txtCompanyName Text dsCustomerIndividual - Customers.CompanyNameTextBox ID txtContactName Text dsCustomerIndividual - Customers.ContactTextBox ID txtContactTitle Text dsCustomerIndividual - Customers.ContactTitleTextBox ID txtAddress Text dsCustomerIn ...