About introducing JS scripts in custom controls

Source: Internet
Author: User

wrote a custom control project, project named TestControls, the contents of the custom control are:

  namespaceTestControls { Public classTestboxcontrol:control {Private stringTextValue ="コピー"; [Description ("ボタンのテキスト")] [browsable (true)]           Public Virtual stringTextValue {Set{TextValue =value;} }          protected Override voidRender (HtmlTextWriter writer) {writer. Write ("<input id=\ "textinput\" type=\ "text\"/>"); Writer. Write ("<input id=\ "copy\" type=\ "button\" value= ""+ TextValue +"' onclick =\ ' copy (); \ "/>"); Writer. Write ("<input id=\ "textwrite\" type=\ "text\" readonly=\ "readonly\"/>"); Base.          Render (writer); }  }

The copy method is the JavaScript scripting method:

function copy () {     var textinputvalue = document.getElementById ("TextInput"). Value;     document.getElementById ("Textwrite"). Value = textinputvalue;}

The script name is Textbox.js and is in the TestControls project.

There is another project test, introduced TestContrls.dll, now to introduce the Testboxcontrol control. But how can the script method copy () be called?

The steps to introduce a JS script are:

1. Write the JS script, and set the JS file's properties as: Embedded resources (as shown)

2. At the beginning of the custom control, write

"[Assembly:System.Web.UI.WebResource (" TestControl.JavaScript.js "," Text/javascript ")]" such as:

Note: TestControl.JavaScript.js in TestControl is the name of the folder where JS is located, because my assembly's default namespace is empty, so here is TestControl.JavaScript.js. If the default named control is not empty, it should be added. The format is: the default named Control. JS is located in the folder name. js file name.

3. Introduction of JS script in OnPreRender in custom control

1 protected Override voidOnPreRender (EventArgs e)2        {3              if( This. Page! =NULL)4              {5                   This. Page.ClientScript.RegisterClientScriptResource (typeof(TestControl),"TestControl.JavaScript.js");6              }7              Base. OnPreRender (e);8}

Note that the JS file name here, you can use reflector to open the currently written DLL view, to ensure the correctness of the JS name.

Finally, how to see whether the introduction of JS is correct?

Launches the page that invokes the current custom control, which you can see in VS

Click here to the reality is the source code. If the script block is displayed here, when clicked, Open is blank, it means that JS introduction failed.

About introducing JS scripts in custom controls

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.