Several ways to interact with. NET and JavaScript scripts

Source: Internet
Author: User

1.asp.net Call JS

Response.Write ("<script language=javascript>");

Response.Write ("alert (' Welcome to Dengfeng ');");

Response.Write ("location.href= ' login.aspx ';");

Response.Write ("</script>");

In this case, you can call the function of the JS script in the page to

2. How the JS script accesses the value of the server control

The interface has a TextBox control, ID Name,js can use the following script to take the value of name

var myvalue=document.all (' Name '). Value;

3, server control how to take the value of the variable in JS

The method is to put a hidden control HtmlInputHidden on the interface and then set to run as a server control so that the value of the control can be accessed in the JS script and in the ASP.

To assign a value to a server control in JS:

var bt=document.all (' Name '). Value;

bt.value= ' name ';

Asp. NET, accessed using Name.value.

4. Calls between foreground and background functions

<title>untitled page</title>

<script type= "Text/javascript" >

function CallServer (ARG) {

var oTb = document.getElementById (' <%=editvalue.clientid%> ');

The variables passed to the server in ARG

arg = Otb.value;

<%=clientscript.getcallbackeventreference (This, "Arg", "Receiveserverresult", NULL, True)%>

}

function Receiveserverresult (Result) {

Here you add the logic that handles the server's return result, which is the result of the server's return

alert (result);

}

</script>

...//Omit part of the code here

<asp:textbox id= "EditValue" runat= "Server"/>

<asp:button id= "btnsubmit" runat= "Server" text= "Submit Data" onclientclick= "CallServer (); return false;"/>

CS file

C # code

The page class inherits the ICallbackEventHandler interface and implements two of these methods

Public partial class _default:system.web.ui.page, ICallbackEventHandler

{

private string m_strresult = "";

#region ICallbackEventHandler Members

public string GetCallbackResult ()

{

Returns server-side processing results to the Receiveserverresult method

return m_strresult;

}

public void RaiseCallbackEvent (String eventargument)

{

Eventargument is a variable from the client that corresponds to the ARG variable

Add server-side processing logic here ...

M_strresult = eventargument;

}

#endregion

}

The functions in the C # code are executed in the 5.javaScript function:

Method One: 1, first set up a button, in the background will be called or processed content written in Button_Click;

2, write a JS function in the foreground, the content is document.getElementById ("Btn1"). Click ();

3, in the foreground or background call JS function, fire click event, equal access to the background C # function;

Method Two: 1, function declaration is public

Background code (change public to protected also can)

public string SS ()

{

Return ("a");

}

2, in the HTML with <%=fucntion ()%> can call

Foreground script

var a = "<%=ss ()%>";

alert (a);

Method Three: 1,

!--

function __doPostBack (Eventtarget, eventargument)

{

var theform = document. Form1; Refers to the form of runat=server

Theform.__eventtarget.value = Eventtarget;

Thefrom.__eventargument.value = eventargument;

Theform.submit ();

}

-->

Method Four:

function Submitkeyclick ()

{

if (Event.keycode = = 13)

{

Event.cancelbubble = true;

Event.returnvalue = false;

Document.all.funname.value= "The name of the function you want to invoke";

Document.form[0].submit ();

}

}

〈! --Used to store the function you want to call--〉

In. cs There are:

Public Page_onload ()

{

if (! Page.ispost ())

{

String strfunname=request.form["Funname"]!=null? request.form["Funname"]: "";

Determines which function to call based on the value returned

Switch (strfunname)

{

Case "Enter ()":

Enter (); Call this function

Break

Case "Other":

Calling other functions

Break

Default

Calling the default function

Break

}

}

}

public void Enter ()

{

...... such as calculating a value

}

6. How do I access C # variables in JavaScript?

Method One: 1, through the page hidden domain access

Method Two: 1, such as the background defines the public STRING N; The format for referencing the variable in the foreground JS is ' <%=n%> ' or ' + <%=n%> + '

Method Three: 1, or you can register a script on the page after the server-side variable is assigned a value

""

TMP is a background variable, and then JS can directly access the temp to get the value.

7. How do I access JavaScript functions in C #?

Execute JavaScript functions in C # code:

Method One: 1, page.registerstartupscript ("GGG", "");

Method Two: Use the literal class, and then

private void Button2_Click (object sender, System.EventArgs e)

{

String str;

str= "

str+= "Selectrange ()";

str+= "";

Literal1.visible=true;

LITERAL1.TEXT=STR;

}

Several ways to interact with. NET and JavaScript scripts

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.