asp.net of the background *.cs and the foreground JS script to call each other several ideas __.net

Source: Internet
Author: User
Tags call back

Some ideas about the asp.net between backstage *.cs and foreground JS script

< type= "Text/javascript" > < type= "Text/javascript" >
Frequently, it is necessary to invoke a script function that has already been defined in the JavaScript script in asp.net "background code *.cs file."

Basically include a few ways:

1. Write script using the Response.Write () method:

For example, after the click of the button, the first operation of the database, after the display has been completed, you can finally want to call the place to write:

Response.Write ("<script type= ' text/javascript ' language= ' JavaScript ' >alert"); ></script> ");

A flaw in this approach is that you cannot invoke custom functions in script files, only internal functions, and specific calls to custom functions can only be written in Response.Write, such as: Response.Write ("<script type=" text/ JavaScript ' >function myfun () {...} </script> ");

2. Dynamically add scripts using the ClientScript class:

Use the following: To add code where you want to invoke a JavaScript script function, be aware that Myfun has already been defined in the script file.

Clientscript.registerstartupscript (Clientscript.gettype (), "MyScript", "<script type=" Text/javascript "> Myfun ();</script> ");

Example: StringBuilder sb = new StringBuilder ();
Sb. Append ("<script language= ' JavaScript ' >");
Sb. Append ("Button2_onclick ('" + Serverpath + ")");
Sb. Append ("</script>");
Clientscript.registerstartupscript (this. GetType (), "Loadpicscript", sb. ToString ());

This method is more convenient than Response.Write, and you can call the custom function in the script file directly.

3. Attributes properties of the normal add control:

For the ordinary button is: Button1.Attributes.Add ("onclick", "myfun ();");

Can only be added in the onload or in an initialization process similar to onload. and the script function is executed first, and the order of execution cannot be changed.

Note that all of the above methods, the background code can not be converted to the current page code, such as redirect, and so on, to put the page code in the script.

4. Define literal controls
Usage is as follows:
Literal lit=new Literal ();
Lit.text= "<script>alert ();</script>";
THIS.PAGE.CONTROLS.ADD (lit);

5.OnClientClick (VS2003 does not support this method)
<asp:button id= "Button1" runat= "Server" text= "button" onclientclick= "Client_click ()" onclick= "Button1_Click"/ >
Client_click () is a way of JavaScript.

Second, of course, sometimes also need in the "front desk JS execution or call back some method function", many times will think of Ajax to solve, recently just summed up a no Ajax but very simple and practical method:

1. Here to delete the row in table as an example, if the label content in the table is removed from the background in the render to the foreground, the Delete button in the row can be written as a JS trigger mode:

View Plaincopy to Clipboardprint?
<a href = ' javascript:onrowdatadelete (ID, groupname, groupid); ' > Delete </a>
<a href = ' javascript:onrowdatadelete (ID, groupname, groupid); ' > Delete </a>


2. The called foreground JS function is:

View Plaincopy to Clipboardprint?
function Onrowdatadelete (ROWID, Gname, GID) {
if (Confirm ("-----------------------------------------\ r \ n User group id:\" "+ gid + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ r \ gname +" \ "\ r \ n--------- --------------------------------\ r \ n-------confirm that you want to delete the above user groups? -------" )) {
document.getElementById ("Delid"). Value = rowID;
document.getElementById ("Delgid"). Value = GID;
document.getElementById ("Del_btn"). Click ();
}
else {
}
}
function Onrowdatadelete (ROWID, Gname, GID) {
if (Confirm ("-----------------------------------------\ r \ n User group id:\" "+ gid + \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ r \ gname +" \ "\ r \ n--------- --------------------------------\ r \ n-------confirm that you want to delete the above user groups? -------" )) {
document.getElementById ("Delid"). Value = rowID;
document.getElementById ("Delgid"). Value = GID;
document.getElementById ("Del_btn"). Click ();
}
else {
}
}


3. The following hidden definitions are added to the foreground *.aspx:

View Plaincopy to Clipboardprint?
< Asp:hiddenfield ID = "delid" runat = "server"/>
< Asp:hiddenfield ID = "Delgid" runat = "server"/>
< Asp:button ID = "DEL_BTN" style = "display:none" runat = "server" OnClick = "Delbtn_click"/>
< Asp:hiddenfield ID = "delid" runat = "server"/>
< Asp:hiddenfield ID = "Delgid" runat = "server"/>
< Asp:button ID = "DEL_BTN" style = "display:none" runat = "server" OnClick = "Delbtn_click"/>


4. The corresponding OnClick event in the background *.cs:

View Plaincopy to Clipboardprint?
protected void Delbtn_click (object sender, EventArgs e) {
You can get the relevant parameters for the delete row directly: Delid. Value, Delgid. Value
Execute your business logic here, like here is the method function of the deletion
}
protected void Delbtn_click (object sender, EventArgs e) {
You can get the relevant parameters for the delete row directly: Delid. Value, Delgid. Value
Execute your business logic here, like here is the method function of the deletion
}


5. Brief notes:

After the deletion of the JS method is fired, the confirm will be executed first, and the incoming parameters will be assigned to the corresponding hidden values in the *.aspx foreground page to facilitate the background value. Finally, start *.aspx the hidden button click event in the page, and begin calling its background click event to perform the backend method function.

(Note that the button in *.aspx cannot use Visible=false when hidden, which makes it impossible to find this control when getElementById)

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.