简体   繁体   中英

add client script to asp.net page dynamically

I want to add javascript to asp.net page dynamically. can anybody point me towards working example? i know it can be done by using Page.ClientScript.RegisterClientScriptBlock but i have no idea to use it.

An example to move the value of a Drop Down List to text field. The ID parameters are the Object.ClientID properties for the drop down list and text box.

Private Sub RegisterClientDropDownToTextBox(ByVal functionName As String, ByVal dropDownId As String, ByVal textBoxId As String)
    Dim javascriptFunction As String = "function " & functionName & "() {" & _
                                       "document.getElementById('" & textBoxId & "').value = document.getElementById('" & dropDownId & "').value;" & _
                                       "}"
    Dim javascriptWireEvent As String = "document.getElementById('" & dropDownId & "').onclick = " & functionName & ";"
    Me.ClientScript.RegisterClientScriptBlock(Me.GetType(), functionName & "_ScriptBlock", javascriptFunction, True)
    Me.ClientScript.RegisterStartupScript(Me.GetType(), functionName & "_Startup", javascriptWireEvent, True)
End Sub

MSDN

This is the MSDN link

if (!this.Page.ClientScript.IsClientScriptBlockRegistered(typeof(Page), "Utils"))
        {
            string UtilsScript = ResourceHelper.GetEmbeddedAssemblyResource("Utils.js");

            this.Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Utils", UtilsScript, true);
        }

I added the above example to help,

Here we test if the script is already registered (using the type an dkey we register against) get the script as a string from an embedded resource, then register (the last parameter of true tells the code to render Script tags).

hope this helps

P

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM