Add common JavaScript operations to the ASP. NET Control

Source: Internet
Author: User
1. Add confirmation for the button control

To add client events to server controls, you must use the attributes attribute. The attributes attribute is an attribute of all server controls. It is used to add custom tags to the final HTML. Assume that there is a Save button btnsave on the web form, and you want to prompt the user whether to save the button (for example, once saved, it cannot be restored) When you click this button ), add the following in the page_load event:Code:
1btnsave. Attributes. Add ("On Click ","; ")

note that 'return' is not a province. Otherwise, the data is saved even if the user clicks cancel.

2. add javas upload event

the child control in the DataGrid cannot be directly accessed. To achieve the above effect, we need to use the on itemdatabound event. On the itemdatabound event occurs after each row of data in the DataGrid is bound to the DataGrid (that is, one row is fired ). First, add the on itemdatabound attribute to the DataGrid declaration,

On when the itemdatabound event occurs, call the itemdatabound method. Add the definition of this method to the Code Post File:

Sender, system. Web. UI. webcontrols. datagriditemeventargs E)
If (E. Item. itemtype! = Listitemtype. Header & E. Item. itemtype! =
= (Linkbutton) E. Item. cells [2]. controls [0
Btnsave. Attributes. Add ("On Click ",";"


because the row of the title and footer of the DataGrid will also trigger this event, first determine that the row that inspires this event is not the row of the title and footer. Assume that the btnsave button is in column 3rd of the DataGrid (the first column is 0 ).

3. in javas events that trigger server-side controls

let's consider the first instance. we added the confirmation function for the Save button, the Save operation is performed only after the user confirms the operation. If the user does not confirm the operation, the operation is not executed. What if we want the user to execute another operation when the user presses "cancel? This requires the use of JS back-and-forth (PostBack) server-side controls to complete the operation.

There Is A dropdownlist control ddltest on the current page, and the button is btnsave. when ddltest is selected, the On change event is triggered, save the current selected value. The operator is asked to confirm before saving, and the user confirms to save the selected value; otherwise, the system changes to default. ASPX page.

Add the following code to the page_load event:

1 string strcmd = page. getpostbackclienthyperlink (btnsave ,"");
2 string script = @ "" EV Al_message "");";
3 = script. Replace ("EV Al_message ", strcmd );
4 "on Change ", script );

The Select Control generated after this code execution is as follows:

1 select name = "ddltest" id = "ddltest" on Change = ");"> """);"〉

The confirmupdate function is as follows:
Confirmupdate (CMD ){
3if (confirm ("are you sure to update? "
8 9 = "default. aspx"
10 javas is used here Cript EV To call the commands contained in a string. Note that strings containing commands cannot be enclosed by single quotation marks. Because the automatically generated script contains single quotation marks, two double quotation marks are used to represent the double quotation marks of the string.
------------------------------------------------------------- Gorgeous split line ------------------------------------------------------------- I remember that JavaScript is often used when ASP is used. Such as the verification form and deletion confirmation. Asp.net is a server, and our JavaScript has lost the space for applications. However, after some exploration, Asp.net is actually very simple to use JavaScript.

First, we will introduce the webbutes attribute of webcontrol. (Htmlcontrol is similar and does not repeat). attributes is a set of arbitrary features (only for rendering) that do not correspond to the properties of the control. For example, a common form Control
<Input type = "button" name = "button" value = "button" onclick = "clickevent ()">
Here, the type, name, and button are all the attributes of the button, and the event after The onclick button is clicked. These can be set in Asp.net through the attributes attribute set.

The following is an example of Logon form verification.
<% @ Page Language = "C #" %>
<SCRIPT runat = "server">
Void page_load (Object sender, eventargs E)
{
// Add the client onclick event
Button1.attributes ["onclick"] = "Return checkform ()";
}
</SCRIPT>

<HTML>
<Head>
<Script language = "JavaScript">
Function checkform ()
{
// Verify the javascipt function of the form.
If (document. All. username. value = "")
{
Alert ("Enter the user name ");
Return false;
}
Else if (document. All. Password. value = "")
{
Alert ("enter the password ");
Return false;
}
Return true;
}
</SCRIPT>
</Head>
<Body>
<Form runat = "server">
<Br/>
<Table Height = "150" width = "300">
<Tbody>
<Tr>
<TD>
User name </TD>
<TD>
<Asp: textbox id = "username" runat = "server"> </ASP: textbox>
</TD>
</Tr>
<Tr>
<TD>
Password </TD>
<TD>
<Asp: textbox id = "password" runat = "server" textmode = "password"> </ASP: textbox>
</TD>
</Tr>
<Tr>
<TD>
</TD>
<TD>
<Asp: button id = "button1" runat = "server" text = "login"> </ASP: button>
</TD>
</Tr>
</Tbody>
</Table>
</Form>
</Body>
</Html>

Is it back to the familiar method :)
----------------------------------------------------------------------- Gorgeous split line ----------------------------------------------------------------------- Server controls: The onclientclick attribute is the easiest way to call the static PAGE method. Use onclick to call server events

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.