1
Private void Focus (string sDomID)
{
RegScript ("focus", "document. getElementById (\" "+ sDomID +" \ "). focus ();");
}
2
Public void Select (TextBox txtBox)
{
String sScript = "var dom = document. getElementById (\" "+ txtBox. ClientID +" \ "); if (dom) {dom. select () ;}\ n ";
RegScript ("Select", sScript );
}
Note:
(1) When the motherboard (. MASTER) and user controls (. (ASCX), the user control page JavaScript needs to use the Asp.net Control ID, the master page subpage JavaScript needs to use the Asp.net Control ID
(2) \ "means: it is written in the background. in the js file, C # recognizes "as a string, but discards it when it comes to the JavaScript front-end" and adds a \ escape character, tell the C # editor that this is just ". In this way, it will be translated into" and will not be lost in the JavaScript console.
3
Obtain the Server Control identifier generated by ASP. NET.
Problem 1: The Control ID of Asp.net must be used in the page JavaScript of the user control.
Problem 2: The Asp.net Control ID must be used in the subpage JavaScript of the master page.
Solution:
Document. getElementById ("<% = TextBox1.ClientID %>"). value = "1234567 ";
This code can be used to access server-side controls on the client. Because ASP. NET generates different names for rendering server-side controls on the client, this function is useful.
Note: The Asp.net controls are static controls (controls added by tools)
Question 3: How to obtain the ClientID of the dynamically created control exists: you cannot use "<% = TextBox1.ClientID %>" on the page ". method (the method is not available because the control is dynamically created), and TextBox1.ClientID cannot be used in the page script (the obtained ID is incorrect ).
Solution:
(CitySpecProductScheduledControl. ascx. cs code snippet)
TextBox text = new TextBox ();
Text. MaxLength = 12;
Text. Columns = 12;
Text. Text = DateTime. Now. tow.datestring ();
Text. ID = "dateId ";
Text. Attributes. Add ("onclick", "calendar ()");
Text. Attributes. Add ("class", "text ");
HtmlImage dateimg = new HtmlImage ();
Dateimg. Src = "../CommonImages/cal.gif ";
String js = string. format ("calendar ({0 }_{ 1})", this. clientID, text. clientID); // key code, which is manually generated according to the generation rules of client ClientID.
Dateimg. Attributes. Add ("onclick", js );
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/heling0223/archive/2009/11/04/4765598.aspx