I.CodeTo dynamically generate JavaScript code:
Previously, the JavaScript code generated in the suffix code always uses response. write to generate. Today we see two functions that allow the client to generate JavaScript code. One is registerclientscriptblock and the other is registerstartupscript. The difference between the two is that the generated position is different, they are all between <form> and </form>, but the first is after <form> and the second is before </form>. The two functions can be used with the isclientscriptblockregistered and isstartupscriptregistered functions to check whether the code block has been registered and used. Example:
Private Void Page_load ( Object Sender, system. eventargs E)
{
Stringbuilder scriptstring = New Stringbuilder ();
Scriptstring. append ( " <Script language = JavaScript> " );
Scriptstring. append ( " Function setfirstfocus (){ " );
Scriptstring. append ( " Document. All. Name. Focus ();} " );
Scriptstring. append ( " </SCRIPT> " );
String JS = Scriptstring. tostring ();
Page. registerclientscriptblock ("Setfirstfocus", JS );
// Page. registerstartupscript ("setfirstfocus", JS );
}
2. Some knowledge points about viewstate:
See http://www.microsoft.com/china/msdn/archives/library/dnaspnet/html/Asp11222001.asp for details
3. Obtain the JavaScript code of the buttons on the client:
< Script Language = " Javascript " >
Function keydown ()
{< br> var keycode = event . keycode;
var keychar = string. fromcharcode (keycode);
alert ( ' ASCII = ' + keycode + ' \ nkeychar = ' + keychar);
}
Document. onkeydown = Keydown;
</ Script >
4. Use enter to replace table key's JavaScript code: < Script Language = " Javascript " >
Function keydown ()
{
VaR keycode=Event. Keycode;
VaR keychar=String. fromcharcode (keycode );
If(Keycode=13)
Event. Keycode=9;
} ;
Document. onkeydown = Keydown;
</ Script >
5. dynamically create controls:
Label LBL = New Label ();
LBL. Text = " Label " + (I). tostring ();
LBL. ID = " Label " + (I). tostring ();
Panel1.controls. Add (LBL );
Panel1.controls. Add ( New Literalcontrol ( " <Br> " ));
Literalcontrol can generate HTML code of the page according to the string
6. dynamically change the properties of the Web Control:
You can add style attributes to a widget just like adding a script event, for example:
Button1.attributes. Add ("style", "background-color: #008888 ");
7. Add it in the javscript code of the page. net Code
for example, the ASPX page has the following controls
I want to get the literal1.text text in the following JS Code, in the following ways: