Some ASP. NET knowledge points (about how. Net interacts with JS)

Source: Internet
Author: User

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:

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.