Hi, I wants to ask about how to call the javascript function/method in .aspx file from code behind(or server code)?

for example:
Javascript:

function initCard(userid) {
            //userid, balance, subsidy breakfast, subsidy lunch, subsidy dinner

            var iRet = winCtrl.initCard(userid, 0, 0, 0, 0);
        }

code behind:

Dim sb As New StringBuilder()
   Dim int As Integer = 12
          
            sb.Append("<script type=""text/javascript""> function initCard(" & int & "{")
            sb.Append("var iRet = winCtrl.initCard(" & int & ", 0, 0, 0, 0);} </")
            sb.Append("script>")
         

           Page.ClientScript.RegisterStartupScript(Me.GetType(),"ClientScript",sb.ToString, True)  Page.ClientScript.RegisterClientScriptBlock(Me.GetType(),"ClientScript",sb.ToString,True)

After I run the program, it does not affect anything.Anyone can help?

Hi
This link should help you http://msdn.microsoft.com/en-us/library/ms178207.aspx

It really depends when you want the script to run, In response to an event or automatically after the page loads?


Your code looks like you are trying to do both with the same script....

The RegisterStartupScript creates a Script block at the bottom of the page to run immediately after a page load.

The RegisterClientScriptBlock creates a script block at the top of the page to handle a client side event or be fired by another client side script

The RegisterOnSubmit Statement adds a block that is fired when you page submits i.e. on postback.

Also you have given the two script blocks the same name! How does the page know which to call?

Hi
This link should help you http://msdn.microsoft.com/en-us/library/ms178207.aspx

It really depends when you want the script to run, In response to an event or automatically after the page loads?


Your code looks like you are trying to do both with the same script....

The RegisterStartupScript creates a Script block at the bottom of the page to run immediately after a page load.

The RegisterClientScriptBlock creates a script block at the top of the page to handle a client side event or be fired by another client side script

The RegisterOnSubmit Statement adds a block that is fired when you page submits i.e. on postback.

Also you have given the two script blocks the same name! How does the page know which to call?

Thanks for reply, I just wants to access/initialize the method initCard() which are created in the .aspx file after i click a "Save" button. By the way i just found that the code "RegisterStartupScript" and the "RegisterClientScriptBlock" is created a new script block but not for access the .aspx method ...

Hi,

Sorry for the delay, you either want to use the register on submit statement or alternatively, if you are checking something like form input etc you can create a client side function that returns true or false, register in with Client script block and on the button attributes call the function on the client side click if the function return a false, the submission will stop if true it will go ahead and execute the submission.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.