Use Js in Asp.net

Source: Internet
Author: User

Method 1,
Using response. Write, this method will write the JS Code at the top of the page (before <HTML> ):
System. Web. httpcontext. Current. response. Write ("<script language = JavaScript> alert ('js Code'); </SCRIPT> ");
Method 2,
Using registerstartupscript, this method embeds the JS Code at the bottom of the page and at the end of the form (</form>). This method is applicable to the JS Code to run after the page control is loaded:
System. Web. UI. Page page = (system. Web. UI. Page) system. Web. httpcontext. Current. Handler;
If (! Page. clientscript. isstartupscriptregistered (page. GetType (), "clientscript "))
Page. clientscript. registerstartupscript (page. GetType (), "clientscript", "<script language = JavaScript> alert ('js Code'); </SCRIPT> ");
Method 3,
Using registerclientscriptblock, this method embeds the JS Code at the top of the page and at the beginning of the form (<form>). This method is applicable to the js code to be executed before the control is loaded, similar to the response. write method:
System. Web. UI. Page page = (system. Web. UI. Page) system. Web. httpcontext. Current. Handler;
If (! Page. clientscript. isclientscriptblockregistered (page. GetType (), "clientscript "))
Page. clientscript. registerclientscriptblock (page. GetType (), "clientscript", "<script language = JavaScript> alert ('js Code'); </SCRIPT> ");

Column:
/// <Summary>
/// Control click the message confirmation prompt box
/// </Summary>
/// <Param name = "page"> current page environment, usually this </param>
/// <Param name = "MSG"> information to be displayed </param>
/// <Param name = "truedosomething"> if you click "OK", perform the following operations (Javascript script): </param>
/// <Param name = "falsedosomething"> if you click Cancel (Javascript script), </param>
Public static void showconfirm (system. Web. UI. Page, string MSG, string truedosomething, string falsedosomething)
{
Stringbuilder builder = new stringbuilder ();
Builder. append ("<script language = 'javascript 'defer> ");
Builder. append ("If (confirm ('" + MSG + "')){");
Builder. append ("" + truedosomething + "}");
Builder. append ("else {");
Builder. append ("" + falsedosomething + "}");
Builder. append ("</SCRIPT> ");
Page. clientscript. registerstartupscript (page. GetType (), "message", builder. tostring ());
}

 

 

 

 

1. dynamically create select

Function createselect (){

VaR myselect = Document. createelement (select );
Myselect. ID = myselect;
Document. Body. appendchild (myselect );
}

2. Add Option

Function addoption (){

Search for objects by ID,
VaR OBJ = Document. getelementbyid ('myselect ');

Add an option
OBJ. Add (New Option (text, value ));
}

3. Delete All option options

Function removeall (){
VaR OBJ = Document. getelementbyid ('myselect ');

OBJ. Options. Length = 0;

}

4. delete an option Option

Function removeone (){
VaR OBJ = Document. getelementbyid ('myselect ');

Index. The sequence number of the option to be deleted. The sequence number of the selected option is used here.

VaR Index = obj. selectedindex;
OBJ. Options. Remove (INDEX );
}

5. Obtain the option value.

VaR OBJ = Document. getelementbyid ('myselect ');

VaR Index = obj. selectedindex; No., which is the serial number of the currently selected option.

Var val = obj. Options [Index]. value;

6. Obtain the option text

VaR OBJ = Document. getelementbyid ('myselect ');

VaR Index = obj. selectedindex; No., which is the serial number of the currently selected option.

Var val = obj. Options [Index]. text;

7. Modify Option

VaR OBJ = Document. getelementbyid ('myselect ');

VaR Index = obj. selectedindex; No., which is the serial number of the currently selected option.

Var val = obj. Options [Index] = New Option (new text, new value );

8. Delete select

Function removeselect (){
VaR myselect = Document. getelementbyid (myselect );
Myselect. parentnode. removechild (myselect );
}
From http://www.cnblogs.com/rayking/archive/2008/09/03/1282473.html

Related Article

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.