Tài liệu đào tạo ASP.NET

Chia sẻ: Ngo Ben | Ngày: | Loại File: DOC | Số trang:54

2
340
lượt xem
203
download

Tài liệu đào tạo ASP.NET

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Tham khảo tài liệu 'tài liệu đào tạo asp.net', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Chủ đề:
Lưu

Nội dung Text: Tài liệu đào tạo ASP.NET

  1. CÔNG TY CỔ PHẦN PHÁT TRIỂN ĐẦU TƯ CÔNG NGHỆ FPT Công ty Hệ thống thông tin FPT TÀI LIỆU ĐÀO TẠO ASP.NET
  2. Tài liệu đào tạo asp.net v1.0 Hà nội, 09/2009 Mục lục I. Nền tảng lập trình web....................................................................................................................1 Web Programming...............................................................................................................................2 II. ASP.NET............................................................................................................................................3 III. Using JavaScript Along with ASP.NET.....................................................................................15 III.1 Adding JavaScript to a Server Control......................................................................................15 III.2 Performing a Simple Button-rollover........................................................................................17 III.3 Setting Control Focus.................................................................................................................18 III.4 Changing the Control Focus Dynamically................................................................................18 III.5 Using Larger JavaScript Functions............................................................................................18 III.6 Keeping JavaScript in a Separate File (.js)................................................................................23 IV. Data Access:...................................................................................................................................23 V. Microsoft Data Access Application Block...................................................................................28 V.1 Introduction:................................................................................................................................28 V.2 Using Microsoft .net Data Access Application Block:..............................................................28 V.3 Accessing data without Data Access Application Block:..........................................................28 V.4 Retrieving Multiple Rows using SqlDataReader and Data Access Application Block:...........28 V.5 Retrieving Multiple Rows using DataSet:..................................................................................28 V.6 Retrieving a Single Row.............................................................................................................28 V.7 Explanation of the code:..............................................................................................................28 V.8 Retrieving XML Data:.................................................................................................................28 V.9 Explanation of the Code:...........................................................................................................................28 VI. Directory and File Access.............................................................................................................29 2/54
  3. I. Nền tảng lập trình web
  4. Tài liệu đào tạo asp.net v1.0 Web Programming Modern Information Processing 2/54
  5. Tài liệu đào tạo asp.net v1.0 Components of a Web Page II. ASP.NET ASP.NET Environment • ASP.NET has a rich set of objects to work with in an object-oriented and compiled programming environment • The programming environment supports more than 25 .NET languages, including built-in support for VB.NET, C#, and JScript.NET • .Net frame work 3/54
  6. Tài liệu đào tạo asp.net v1.0 Page Structure Server Controls and Script Processing Form Submission The Control: Web form controls are enclosed within a single tag in the format shown below: ...server controls and HTML code Input Controls Web Form Control Equivalent HTML Tag ... 4/54
  7. Tài liệu đào tạo asp.net v1.0 ... CheckBoxList: public static string LoadCheckBoxList(CheckBoxList pCtrl,string pCommandText,string pValueField,string pTextField) { try { DataTable dt = new DataTable(); dt = GetDataTable(pCommandText); pCtrl.DataSource = dt; pCtrl.DataValueField = pValueField; pCtrl.DataTextField = pTextField; pCtrl.DataBind(); return ""; } catch(Exception ex) { return ex.Message; } } DropDownListControl: public static string BindDropDownListControl(DropDownList pCtrl,string pCommandText,string pValueField,string pTextField,bool pRowBlank) { try { DataTable dt=clsCommon.GetDataTable(pCommandText); pCtrl.DataSource=dt; pCtrl.DataTextField=pTextField; pCtrl.DataValueField=pValueField; pCtrl.DataBind(); pCtrl.Items.Insert(0,""); dt=null; 5/54
  8. Tài liệu đào tạo asp.net v1.0 return ""; } catch(Exception ex) { return ex.Message; } } Output Controls Web Form Equivalent HTML Tag Control ... ... Script Activation Controls Scripts can be activated by user events surrounding the Web page. Certain controls, then, are provided for trapping those events and taking action on them. The primary example of a script activation control is the control. The user clicks the button; a subroutine is called in response. Script activation controls come packaged with event handlers to trap and take action on user events. The most common event is a mouse click on a button; the most common event handler is the OnClick handler associated with the button. For example, the following button definition, displays a button control with the label "Submit." It includes an OnClick event handler which makes the button sensitive to a mouse click. When the button is clicked, the event handler calls the Display_Output subprogram and that script is run. There are a number of server controls designed for the purpose of trapping user events and responding to them. The following controls are discussed in these tutorials: Web Form Control Equivalent HTML Tag Script Activation Events 6/54
  9. Tài liệu đào tạo asp.net v1.0 Scripts can be run in response to Web page events as well as to user events. A key page event is the load event, which occurs when the page is first retrieved by the server in response to a URL request. At the same time, there are two conditions under which a page is loaded. On the one hand, it can be an initial page-load event -- the first time the page is retrieved in response to a URL request; or, it can be a post- back event -- a reloading of the same page on form submission. These two page- load events can be, and often are, programmed separately. Sub Page_Load() If Not Page.IsPostBack Then --do this on initial page load End If --do this on every page load End Sub In this example the Page_Load subroutine (this subprogram name is required) is run every time the page is loaded. It is run when the page is initially accessed through a URL request; it is run when the user clicks a control to call a subroutine. However, that portion of the script enclosed within the If Not Page.IsPostBack condition is run only the first time the page is loaded, not when responding to a user event to reload the page, say when a button is clicked to submit a form. Many of the scripts in these tutorials differentiate between initial page load and post-back load events. Information Display Controls There are three special controls that have no equivalence among standard form tags. These controls are unique to ASP.NET and are designed to ease and to automate the display of complex information. Most often these controls are used to display tables of data from databases. They are designed so that minimal coding is required to extract information from those tables and format it for display. We'll leave discussion of these controls for later. Web Form Control Equivalent HTML Tag (none) (none) (none) The Page ViewState This repopulation of controls with submitted values occurs through the page's View State. The View State is the status of the page when it is submitted to the server. ASP.NET maintains this status through a hidden field that it places on pages containing form controls. If you take a look at the browser's source listing you will see this hidden field, named "__VIEWSTATE", and its encoded value that looks something like the following: ... Maintaining Variables on PostBack 7/54
  10. Tài liệu đào tạo asp.net v1.0 Dim Counter As Integer Sub Page_Load If Not Page.IsPostBack Then Counter = 1 CounterOut.Text = Counter End If End Sub Sub Add_To_Counter (Src As Object, Args As EventArgs) Counter += 1 CounterOut.Text = Counter End Sub Session Variables There is a second method of retaining the values of variables between page postings. This method also maintains values between different pages. A user Session is created by ASP.NET whenever a visitor arrives at any Web page located in your root application directory. A Session Object maintains identification information about the visitor and allows ASP.NET to differentiate between visitors and their browsing status as they navigate the Web site. Your visit to this tutorial site, for instance, is identified by the SessionID value. ArrayList/Datasource An ArrayList can be bound to any of the list controls. In the following examples the previous ColorList array is bound to CheckBoxList, DropDownList, and ListBox controls simply by setting their DataSource properties and calling their DataBind() methods. asp:CheckBoxList asp:DropDownList asp:ListBox Red Green Blue CheckBoxes.DataSource = ColorList DropDownList.DataSource = ColorList ListBox.DataSource = ColorList CheckBoxes.DataBind() DropDownList.DataBind() ListBox.DataBind() Binding a DataSet to Controls A DataSet, like a HashTable and SortedList, can provide the text labels and values that are bound to list controls. In the following example, the four list controls are defined without their asp:ListItem entries. This code is identical to that used to bind to previous list controls. 8/54
  11. Tài liệu đào tạo asp.net v1.0 asp:RadioButtonList asp:CheckBoxList asp:DropDownList asp:ListBox AltaVista AltaVista Excite Excite Google Google Lycos Lycos MSN MSN Yahoo Yahoo Binding a Hashtable to Controls A HashTable can be used to provide the text labels and values that are automatically assigned to list controls: asp:RadioButtonList, asp:CheckBoxList, asp:DropDownList, and asp:Listbox. In the following example, these four list controls are defined without their asp:ListItem entries, and buttons are provided for scripting page transfers to the selected URLs: asp:RadioButtonList asp:CheckBoxList asp:DropDownList asp:ListBox Lycos Lycos MSN MSN Yahoo Yahoo AltaVista AltaVista Excite Excite Google Google Information Display Controls Repeater Control No. Name Price Qty. Amount 1111 Adobe Photoshop 345.95 10 3,459.50 1 22222 Adobe Illustrator 249.95 5 1,249.75 9/54
  12. Tài liệu đào tạo asp.net v1.0 33333 Microsoft XP Upgrade 99.95 14 1,399.30 4444 Microsoft Office XP 699.99 9 6,299.91 4 55555 MacroMedia DreamWeaver 145.95 18 2,627.10 Total Value: $15,035.56 Product List Picture Item Information Source: Products.xml file Product List Picture Item Information 11111 Adobe Photoshop 10/54
  13. Tài liệu đào tạo asp.net v1.0 345.95 22222 Adobe Illustrator 249.95 33333 Microsoft XP Upgrade 99.95 44444 Microsoft Office XP 699.99 55555 MacroMedia DreamWeaver 145.95 Source: Products.xml file DataList Control $ Source: Products Database 11/54
  14. Tài liệu đào tạo asp.net v1.0 Product List 11111 22222 33333 Adobe Photoshop Adobe Illustrator Microsoft XP Upgrade $345.95 $249.95 $99.95 44444 55555 Microsoft Office XP MacroMedia DreamWeaver $699.99 $145.95 Source: Products Database DataGrid Control Product Name Product Price* Availability Adobe Photoshop 345.95 In Stock Adobe Illustrator 249.95 In Stock Microsoft XP Upgrade 99.95 In Stock Microsoft Office XP 699.99 In Stock MacroMedia DreamWeaver 145.95 In Stock *Average Street Price As of 09-23-2009 Displaying Calculated Values Calculations for a Repeater Control No. Name Price Qty. 1111 Adobe Photoshop 345.95 10 1 22222 Adobe Illustrator 249.95 5 33333 Microsoft XP Upgrade 99.95 14 4444 Microsoft Office XP 699.99 9 4 55555 MacroMedia DreamWeaver 145.95 18 No. Name 12/54
  15. Tài liệu đào tạo asp.net v1.0 Price Qty. No. Name Price Qty. Amount 1111 Adobe Photoshop 345.95 10 3,459.50 1 22222 Adobe Illustrator 249.95 5 1,249.75 33333 Microsoft XP Upgrade 99.95 14 1,399.30 4444 Microsoft Office XP 699.99 9 6,299.91 4 55555 MacroMedia DreamWeaver 145.95 18 2,627.10 Summations for a Repeater Control No. Name Price Qty. Amount 1111 Adobe Photoshop 345.95 10 3,459.50 1 22222 Adobe Illustrator 249.95 5 1,249.75 33333 Microsoft XP Upgrade 99.95 14 1,399.30 4444 Microsoft Office XP 699.99 9 6,299.91 4 55555 MacroMedia DreamWeaver 145.95 18 2,627.10 Total Value: $15,035.56 Control Reporting No. Name Price Qty. Amount Order Status 1111 Adobe Photoshop 345.95 10 3,459.50 1 22222 Adobe Illustrator 249.95 5 1,249.75 On Order 33333 Microsoft XP Upgrade 99.95 14 1,399.30 13/54
  16. Tài liệu đào tạo asp.net v1.0 4444 Microsoft Office XP 699.99 9 6,299.91 On Order 4 55555 MacroMedia DreamWeaver 145.95 18 2,627.10 $15,035.5 Total Value: 6 Calculations for a DataGrid No. Name Price Qty. Amount Order Status 1111 Adobe Photoshop 345.95 10 3,459.50 1 22222 Adobe Illustrator 249.95 5 1,249.75 On Order 33333 Microsoft XP Upgrade 99.95 14 1,399.30 4444 Microsoft Office XP 699.99 9 6,299.91 On Order 4 55555 MacroMedia DreamWeaver 145.95 18 2,627.10 Total: $30,071.12 Calculations for a DataList No. 11111 No. 22222 Name Adobe Photoshop Name Adobe Illustrator Price 345.95 Price 249.95 Qty. 10 Qty. 5 Amount 3,459.50 Amount 1,249.75 Order Status Order Status On Order No. 33333 No. 44444 Name Microsoft XP Upgrade Name Microsoft Office XP Price 99.95 Price 699.99 Qty. 14 Qty. 9 Amount 1,399.30 Amount 6,299.91 Order Status Order Status On Order No. 55555 Name MacroMedia DreamWeaver Price 145.95 Qty. 18 Amount 2,627.10 Order Status 14/54
  17. Tài liệu đào tạo asp.net v1.0 Inventory Total: $45,106.68 III. Using JavaScript Along with ASP.NET III.1 Adding JavaScript to a Server Control It is quite easy to add JavaScript to a specific server control that resides on an  ASP.NET page. Let's take a look at the button server control as an example. If  you drag and drop a Button HTML server control (HtmlInputButton Class) onto  a page using either Microsoft Visual Studio® .NET or the ASP.NET Web  Matrix and run it as a server control, it should have the following construction: This is a normal button that can be programmatically manipulated in the code­ behind or server­side script of an ASP.NET page. For example, to assign the  button text when the page is generated, simply use the value property of the  button after this element is turned into an HTML server control (right­click on  the control and select Run As Server Control). Visual C# .NET void Page_Load(object sender, EventArgs e) { Button1.Value = DateTime.Now.ToString(); } This simply provides a button on the page that shows a date and time as the text of the button. Figure 1. Showing the date and time on a button It is important to note that the ASP.NET page here gets the time from the server that generated the page. So if the  Web server sits somewhere in the Central Time Zone of the United States (CST ­6 GMT), then everyone who  requests this page will get the same time no matter where they reside in the world. What if you wanted the button to show the time of the person viewing the page? The easiest way of accomplishing  this task would be to do this using JavaScript on the client­side. For an example of this, we will place the end user's (the viewer of the web page) computer time on a button Web  server control. The following code shows how to accomplish this task: Visual C# .NET void Button1_Click(object sender, EventArgs e) { Response.Write("Postback!"); } 15/54
  18. Tài liệu đào tạo asp.net v1.0 In this bit of code, notice how some of the button's attributes are assigned server side before being sent down to the  client's browser. In this case, the font of the text on the button is changed to Verdana as well as to a bold font­type of  a specific size. Once the button's HTML code is received on the client, the client­side JavaScript changes the text of  the button to the current time on the end user's computer. The HTML code generated for the entire page will then  appear as such: Clicking on the button will still give you a postback (observed through the Response.Write command) and a  new time on the button control as the page is re­rendered. The result is shown in Figure 2. Figure 2. Clicking on the date button In this case, we placed some JavaScript directly in the  element of the page using the onload attribute.  For the value of the onload attribute, we specifically pointed to the HTML element with the name Button1 that is  in the first  section (as it is possible to have multiple forms in HTML). 16/54
  19. Tài liệu đào tạo asp.net v1.0 This was an easy way to add some JavaScript to work with an ASP.NET Web server control. Though, we could have  also just as easily added a JavaScript command to the button itself as shown here in the following partial code  example: Visual C# .NET void Page_Load(object sender, EventArgs e) { Button1.Attributes.Add("onclick", "javascript:alert('ALERT ALERT!!!')"); } Using a server control's attribute property is a great way to add additional JavaScript to a control that is control  specific. In this case, the JavaScript was added using the Attribute.Add property along with the key of the script as  well as the script itself (both represented as string values). III.2 Performing a Simple Button­rollover When it comes to buttons on a Web page, one of the more common functionalities that Web developers want to give  their buttons is a rollover effect. The rollover effect experience is when the end user hovers their mouse over a button  on a Web page (without clicking the button) and the button itself changes color or shape. This can be especially  useful for Web pages that have multiple buttons, and it would be beneficial from a usability standpoint to notify the  end user of the button they would be clicking prior to clicking it. This was fairly easy to do before server controls came along and it isn't that difficult now with server controls. The  code for performing such an operation is as follows: Visual C# .NET void ImageButton1_Click(object sender, ImageClickEventArgs e) { Label1.Text = "Postback!"; } 17/54
  20. Tài liệu đào tạo asp.net v1.0 Instead of assigning the JavaScript to a server control through the  element, this time we used the  onmouseover and onmouseout events of the control. For each of these events, we assigned a JavaScript value.  The onmouseover event is when the end user hovers their mouse over the control and the onmouseout is for  actions when the end user removes their mouse from hovering over the control. In our case, we want to show one  image while the mouse hovers over the button and then show the original image from when the page was loaded  when the mouse moves away from the button. If you are working directly in the control such as this, instead of specifying the control in the form as we did when  working with JavaScript in the  element, you can use the this keyword followed by the property you are  trying to change. III.3 Setting Control Focus In the construction of Web Forms, notice that no property is enabled to set the focus of a Web server control (this will  be an added feature in ASP.NET 2.0 Whidbey). Therefore when using the .NET Framework 1.0 or 1.1, you need to  employ different methods to accomplish this task. You can do it just as you did before ASP.NET came along—using  JavaScript. For example, if your ASP.NET page has multiple text boxes on it, focus can be set to the first TextBox control when  the page is loaded by employing the following code in the  tag of the page. Using this construct, when the page is loaded, the element that contains the ID  TextBox1 will employ the focus,  and this enables the end user to start entering text directly without the need to use the mouse to position the focus. III.4 Changing the Control Focus Dynamically To change the focus on the ASP.NET page dynamically, turn the  tag into a HTML server control. Next, you can change the focus by placing the following code constructs in your ASP.NET server­side events. Visual C# .NET Body1.Attributes["onload"] = "document.forms[0] ['TextBox2'].focus();"; Using this technique, you can assign the focus to other elements in the form. You might find this useful when using  the OnTextChanged event if there are multiple elements in the form. If you don't do this, the end user must refocus  on the form again using their mouse after an OnTextChanged event is fired due to the page postback. III.5 Using Larger JavaScript Functions Now that we can place pieces of JavaScript within HTML elements and even work with JavaScript and Web server  controls in a dynamic fashion, how do you go about putting entire JavaScript functions in your code? There are a couple of ways to accomplish this task and we will take a look at some of the more common methods  that you can employ in your ASP.NET code. For this article, we will look at the RegisterStartupScript and the  RegisterClientScriptBlock methods. Note   If you are a Visual Basic .NET developer using Visual Studio .NET 2002 or 2003, then you will have  to go into your options to turn on the ability to see these advanced methods. To do this, when in Visual Studio  18/54
Đồng bộ tài khoản