JS tips: Get the variable value in the background page in the front-end JS Code

Source: Internet
Author: User

 

Post the code and share it with you:

Background test. aspx. CS code:

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. text;

Public partial class test: system. Web. UI. Page
...{

Protected string strjs; // This variable is to be passed to the front-end JS Code. The position of the variable Declaration must be in the header of the page class and must be declared as protected: Protected

Protected void page_load (Object sender, eventargs E)
...{

}
// Click the button event to dynamically generate a JS Code in the foreground
Protected void button#click (Object sender, eventargs E)
...{
String [] STR =... {"AAA", "BBB", "CCC "};
Stringbuilder sb = new stringbuilder ();
SB. append ("name = '" + STR [0] + "'");
SB. append ("Sex = '" + STR [1] + "'");
SB. append ("age = '" + STR [2] + "'");
Strjs = sb. tostring (); // assign a value to strjs
}
}

Front-End test. aspx code:

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "test. aspx. cs" inherits = "test" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<SCRIPT>
VaR name;
VaR sex;
VaR age; // The three variables are the same as those in strjs passed in the background.
<% = Strjs %> // This sentence is critical, which is equivalent to response. write (strjs) dynamically generates a piece of JS Code. After you click the button, the code is generated dynamically. This JS Code is a value assignment statement, you can click the button to view the source file of the webpage.
Function print ()
...{
Alert (name + ''+ sex +'' + age );
}
</SCRIPT>
</Head>
<Body onload = "Print ()">
<Form ID = "form1" runat = "server">
<Div>
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "button"/> </div>
</Form>
</Body>
</Html>

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.