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>