Today, I want to DEMO how to use dynamic controls to add JavaScript Functions.
JavaScript and ASP. NET interact frequently when interacting with webpages.
Today's example shows how to dynamically add JavaScript on the control of our portal,
On the one hand, it also demonstrates how JavaScript interacts with ASP. NET in a basic way. The following example is quite interesting,
Because I don't want to write too many program codes, but the Interaction Effect is usually better for two ASP. NET programs. I thought about the results for a moment.
The following program continues the previous article. Add JavaScript to open another webpage to call itself and pass the input value of the input control. When you reset the JavaScript triggered by the button
Allows it to return the input value to the original webpage.
In short, this program is a one-person interpreter, and has a taste of progressive use. This program also interprets the spirit and advantages of a dynamic control, that is, interactive control.
By setting and inputting external users, You can dynamically set the webpage display and control mode. This can be seen in many programming languages and web programs, but ASP. NET,
That is, you can create web pages in an object-based manner.
The following program is just a simple demonstration, in which the spiritual reader can carefully understand the use of Xiangjia
JS_Demo.aspx
1 <% @ Page Language = "C #" AutoEventWireup = "True" %>
2
3 <script language = "C #" runat = server>
4 public void Page_Init (object sender, System. EventArgs e)
5 {
6 string val = Request. QueryString. Get ("val ");
7
8 Label message = new Label ();
9 message. ID = "Enter your words ";
10 sourceTag. Controls. Add (message );
11
12 TextBox input = new TextBox ();
13 input. ID = "input ";
14 input. Text = val;
15 sourceTag. Controls. Add (input );
16
17 Button btnSayHello = new Button ();
18 btnSayHello. ID = "btnSayHello ";
19 btnSayHello. Text = "SayHello ";
20 if (val! = "" & Val! = Null)
21 btnSayHello. Attributes. Add ("OnClick", "return SetValue (" + input. ClientID + ");");
22 else
23 btnSayHello. Attributes. Add ("OnClick", "return GetOtherValue (" + input. ClientID + ");");
24 sourceTag. Controls. Add (btnSayHello );
25}
26
27 void SubmitBtn_Click (Object sender, EventArgs e)
28 {
29 TextBox input = (TextBox) sourceTag. FindControl ("input ");
30 LiteralControl lc;
31 lc = new LiteralControl ("<H3>" + input. Text + "</H3> ");
32 sourceTag. Controls. Add (lc );
33}
34
35 </script>
36
37
38 <meta http-equiv = "content-type" content = "text/html; charset = UTF-8"/>
39
40 <body>
41 <script type = "text/javascript">
42 var myInput;
43 function GetOtherValue (ctrl ){
44 myInput = ctrl;
45 var hWnd = window. open ("JS_Demo.aspx? Val = "+ ctrl. value," _ blank ");
46 if (document. window! = Null )&&(! HWnd. opener ))
47 hWnd. opener = document. window;
48 return false;
49}
50 function SetValue (ctrl ){
51 window. opener. myInput. value = "Re:" + ctrl. value;
52 return false;
53}
54 </script>
55 <form runat = "server">
56
57
58
59 <p/>
60 <div id = "sourceTag" runat = "server">
61 </div>
62 <p/>
63 Xiaolong
64 </form>
65 </body>
66
~~~ A dragon (babydragoner )~~~