Obtains the Server Control identifier generated by ASP. NET.

Source: Internet
Author: User


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

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.