Apply JavaScript in ASP.NET

xiaoxiao2021-03-06  63

Dai Zefeng Summary 1. ASP.NET and JavaScript .net is Microsoft's next-generation strategic core, ASP.NET is the specific implementation of .NET strategy in web development. It inherits the simplicity and ease of use of ASP, while overcoming the disadvantage of the structure of ASP program, difficult to read and understand. In particular, the import of server-side controls and event drive patterns makes the development of web applications closer to the development of past desktop programs. In various articles and books of ASP.NET, focus on server controls and .NET Framework SDK, because this is the latest and most revolutionary improvements in ASP.NET; Client scripkes that occupy an important position in the past Web development, JavaScript (including VBScript), which seems to have server-side programs, no need to client script. However, the server-side program requires a browser and web server interaction. For ASP.NET, it is the submission of a page. You need to come back to send a lot of data, and a lot of work, such as entering verification or deleting confirmation, etc. Implement with JavaScript. Therefore, it is also necessary to explore how to use JavaScript in ASP.NET. Second, the application example 1 of JavaScript 1. Adding a server control on the page The ultimate generation of the JavaScript event server control is still ordinary html, such as Generate Input Text. Each HTML control in the form has its own JavaScript event, such as TEXTBOX has an onchange event, button has onclick event, Listbox has an onchange event. To add client events to server controls, you need to use Attributes properties. Attributes property is an attribute that all server controls have, which is used to add some custom tags for the final generated HTML. Suppose there is a save button BTnsave on the web form. I hope that the user is not to save (for example, once saved), you should add the following code in the Page_Load event: if not page.ispostback () THEN BTNSAVE.Attributes.add ("OnClick", "JavaScript: Return Confirm ('Are you sute save?');") end if you have to pay attention to 'return', which is inevitable, otherwise even if the user points Cancel, the data will still be saved. 2. Adding a JavaScript event to each of DataGrid to assume a delete button every line of the DataGrid, I hope that the user is not to delete this record in the user point this button, so that the user is wrong, or only inadvertently Delete button. Whether this delete button is what the name is, it cannot be directly referenced as the last example, because each line has such a button, which is a child control in the DataGrid. In this case, you need to use the onItemDatabase event of DataGrid. OnItemDatabase happens after the DataGrid's data is bound to the DataGrid (ie, one line is excited).

First add the following code in the DataGrid declaration: ... columrid definition here This is called the ItemDatabase, which occurs when the onItemDatabase event occurs. Add this method to the code post file: Sub ItemDatabase (Byval E AS Object, Byval E AS DataGriditeMeventargs) if E.Item.ItemType <> ListItemType.Header and E.Item.ItemType <> ListItemTYPE.FOOTER THEN DIM OdeleteButton as linkbutton = E.Item.cells (5) .controls (0) OdeleteButton.attributes ("OnClick") = "JavaScript: Return Confirm ('Are You Sure You Want To Delete" & DataBinder.eval (E.ITEM. DataITEM, "M_SNAME") & "? ')" End if End Sub) Since the Title row and footnotes of DataGrid are also excited, first determined that the row that excited this event is not the title line and footnotes. Here, it is assumed that the Delete button is located in the sixth column of DataGrid (first column 0), and the DataGrid's DataSource is included in column 3 named "m_sname". Reference The built-in editing feature of the control DataGrid in the DataGrid in the editing state enables an editing method when the field recorded fields. The user does not have to enter a separate page editing record, but the direct point editing button can enable the current line to enter the edit mode. On the other hand, there are some JavaScript programs that require the name of the control. For example, many programs provide a date control when requiring the user to enter the date to ensure a new window for the user to select the date when the user point control icon is available. At this point, you need to provide the ID of the text box of the display date to a new window so that the user selects the date of the date to return to the text box. If it is a normal server text box control, its ID is the same as the ID of the generated HTML input box; however, two IDs are not the same in the editing state of the DataGrid (the truth is the same as the above example), which requires The clientId property of the control is used. Protected Sub ItemEdit (ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Dim sDateCtrl as string sDateCtrl = grd1. Items (e.Item.ItemIndex). Cells (2). FindControl ( "txtDate") CLIENTID END SUB This assumes that the itemEdit method is the Dategrid's OnItem Event Event Handler, which contains a server text box control called TXTDATE at the third column of DataGrid. 4. Reference ASP.NET automatically generated JavaScript so called "Server-side Control" is for developers, and there is no server and client in the generated HTML source program, all of which are standard HTML, DHTML, and JavaScript.

It can respond to the user's input because the event handler of each control ultimately generates a script, and this script re-submits the page to make Web Server respond again and processed. Usually we don't have to know what this script doesn't have to call this script directly, but in some cases, you can simplify a lot of work properly. Please see the following two examples. ● Any location of the DataGrid is selected to select a line DataGrid, which is a built-in selection button, and the current row is selected when this button (you can set the SelectedItemStyle property to make the current row have different appearances). However, users may be more accustomed to anyone any location, and if you complete this feature is quite cumbersome. A good idea is to add a selection button, but make this column hide, call the JavaScript script generated by this button when you click anyone. Sub Item_Bound (ByVal sender As Object, ByVal e As DataGridItemEventArgs) Dim itemType As ListItemType itemType = CType (e.Item.ItemType, ListItemType) If (itemType <> ListItemType.Header) And _ (itemType <> ListItemType.Footer) And _ (itemType <> ListItemType.Separator) Then Dim oSelect As LinkButton = CType (e.Item.Cells (5) .Controls (0), LinkButton) e.Item.Attributes ( "onclick") = Page. GetPostBackClientHyperlink (oSelect, " ") End Sub This assumes that the selection button is located in column 6. E.Item represents a row, from the generated HTML to add an OnClick event in each . Page.GetPostbackClienthyperLink method Returns the client script generated by the LinkButton control in the page, where the first parameter is the LinkButton control, the second parameter is the parameter passed to this control, usually empty. If it is not a LinkButton control, there is a similar function getPostBackClienTevent, and the reader can refer to MSDN. ● The server generated by the server and the server event of the manually added script conflict server control generally corresponds to the corresponding event of the client control, such as the DropDownList's SELECTEDEXCHANGED event corresponding to the HTML

CopyRight © 2020 All Rights Reserved
Processed: 0.053, SQL: 9