Novice Developer
Novice Developer

Reputation: 4759

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.

Upvotes: 0

Views: 3318

Answers (2)

Pharabus
Pharabus

Reputation: 6062

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

Upvotes: 1

Agent_9191
Agent_9191

Reputation: 7253

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

Upvotes: 1

Related Questions