ASP. NET Programming Model: registerstartupscript registers a script with the page

Source: Internet
Author: User
<% @ Page Language = " C # " Autoeventwireup = " True " Codefile = " Default3.aspx. CS " Inherits = " Webparts_default3 "   %>

<% @ Register SRC = " Selectpro. ascx " Tagname = " Selectpro " Tagprefix = " Uc1 "   %>
<! 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 >
</ Head >
< Body >
< Form ID = "Form1" Runat = "Server" >
< Div Onclick = 'Test ()' >
< ASP: textbox ID = "Textbox1" Runat = "Server" > </ ASP: textbox >
< ASP: textbox ID = "Textbox2" Runat = "Server" > </ ASP: textbox >
< ASP: textbox ID = "Textbox3" Runat = "Server" > </ ASP: textbox >
< ASP: textbox ID = "Textbox4" Runat = "Server" > </ ASP: textbox >
< ASP: textbox ID = "Textbox5" Runat = "Server" > </ ASP: textbox >
</ Div >

< Script Type = "Text/JavaScript" >
Function Test ()
{
Alert ( ' <% = Textbox1.clientid %> ' );

}


</ Script >

</Form>
</Body>
</Html>

 

Using System;
Using System. collections;
Using System. configuration;
Using System. Data;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. htmlcontrols;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;

Public Partial ClassWebparts_default3: system. Web. UI. Page
{
Protected VoidPage_load (ObjectSender, eventargs E)
{
Set1 ();//Or set2 ();
}

Public   Void Set1 ()
{
String Script =   "" ;
For ( Int I =   0 ; I < Page. Form. Controls. Count; I ++ )
{
If (Page. Form. controls [I] Is Textbox)
{
Textbox txt = Page. Form. controls [I] As Textbox;
TXT. Attributes. Add ( " Onclick " , TXT. clientid +   " _ Show () " );
TXT. style. Add (htmltextwriterstyle. backgroundcolor, " Yellow " );
Script + =   " Function "   + TXT. clientid +   " _ Show () {alert (' "   + TXT. clientid +   " ');}; " ;
}
}
Page. clientscript. registerstartupscript ( This . GetType (), " Script " , " <SCRIPT> "   + Script +   " </SCRIPT> " );
}

Public   Void Set2 ()
{
String Script =   "" ;
For ( Int I =   0 ; I < Page. Form. Controls. Count; I ++ )
{
If (Page. Form. controls [I] Is Textbox)
{
Textbox txt = Page. Form. controls [I] As Textbox;
TXT. Attributes. Add ( " Onclick " , " Show (' "   + TXT. clientid +   " ') " );
TXT. style. Add (htmltextwriterstyle. backgroundcolor, " Red " );

Script =   " Function show (ID) {alert (ID );}; " ;
}
}
Page. clientscript. registerstartupscript ( This . GetType (), " Script " , " <SCRIPT> "   + Script +   " </SCRIPT> " );
}
}

 

 

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.