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