Use ASP. NET 2.0 findcontrol getelementbyid

Source: Internet
Author: User

A major problem found with ASP. NET 2.0 is that getelementbyid cannot be directly used in JavaScript. The ID on the ASP page cannot be referenced directly.
ASP. NET 2.0 name mangling
1: Use
VaR ctrlcust = Document. getelementbyid (<% ctrl. clientid %> );
2: however, if the control is in formview gridview detailview, The findcontrol is required.
Txbox = (textbox) performanceitemview. findcontrol ("textbox ");
Find the control first. Then txbox. clientid, and JavaScript needs to load in page_load
3: but there is still a problem here. In method 2, it is only suitable for default view. If the view has changed, for example, when readonly is converted to edittemplate, the controls in edittemplate use itemview. find cannot be found. Therefore, another method is required. Load Javascript in item_created
What I implement here is a pop-up window that returns a value
Protected void formviewincluitemcreated (Object sender, eventargs E)
{
If (formview1.currentmode = formviewmode. Edit)
{
Textbox custtextbox = (textbox) formview1.findcontrol ("main_customertextbox ");
If (custtextbox = NULL) return;
String JS = "<script language = 'javascript '> ";
JS = JS + "function findcust (custbutton )";
JS = JS + "{";
JS = JS + "Var ttop = custbutton. offsettop ;";
JS = JS + "Var thei = custbutton. clientheight ;";
JS = JS + "Var tleft = custbutton. offsetleft ;";
JS = JS + "while (custbutton = custbutton. offsetparent) {ttop + = custbutton. offsettop; tleft + = custbutton. offsetleft ;}";

JS = JS + "Var returnvalue = Window. showmodaldialog ('.. /common/commonfindcustomer. aspx ', 'pass paramerter to child Window', 'dialogwidth: 600px; dialogheight: 500px; dialogleft:' + tleft + '; dialogtop:' + ttop + '; resizable: yes '); ";
JS = JS +" If (returnvalue = NULL) return; ";

JS = JS + "Var ctrlcust = Document. getelementbyid ('" + custtextbox. clientid + "');";
JS = JS + "If (ctrlcust = NULL )";
JS = JS + "alert ('not found customer control ');";
JS = JS + "else ";
JS = JS + "ctrlcust. value = returnvalue ;";
JS = JS + "} </SCRIPT> ";

Clientscript. registerstartupscript (this. GetType (), "findcustscript", JS );

}

Refer to the following
How to access control in formview
Http://www.dotnettaxi.com/ViewTopic137362.aspx
Formview-findcontrol only works for default view
Http://www.dotnettaxi.com/ViewTopic138828.aspx
Http://west-wind.com/weblog/posts/5127.aspx

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.