Call a Javascript function from C#

  • Thread starter Thread starter Praveen
  • Start date Start date
P

Praveen

Hello,

I have a web form(mainform.aspx) which contains a
javascript function (draw). How can i call this function
(on button click event) from Mainform.aspx.cs file.
If anything is unclear please let me know.
Any help would be really appreciated.

Regards
Praveen Manne
 
Hi Praveen,

You can achieve this by using somethig similar to the code below:

<form runat="server">
<asp:button id="button1" onclick="button1_click" runat="server"
Text="Click Me!"></asp:button>
</form>

HTH,

Telmo Sampaio
MCT
 
adding the onclick directly to the html will cause a server event to
fire. the best way is to do add this code to the Page_Load

button1.Attributes.Add("onClick", "MyJS('a','b');");

bk
 
Hi,

One more small query ..

The JavaScript function I'm using dishes out some values
and is there any way that I can use those variables/values
in my C# code.

Any help would be really appreciated.

Regards
Praveen ..
 
i don't know of a way to pass values from JS to code behind, except
setting the values to hidden fields, then accessing the hidden fields
in the code behind. (make sure runat="server" is in the hidden fields.

why not generate those values in the code behind then pass those values
to the JS func?
 
Hi..
i also did the same.. but still unable to get the solution..

default.aspx
<asp:Button ID="Button11" runat="server" Text="Upload111" style="display:none"/>

default.aspx.cs
protected void buttonPicUpload_onclick(object sender, EventArgs e)

{

Button11.Attributes.Add("onclick", "buttonPicUpload_onclickJS();");



}



js function is:
function buttonPicUpload_onclickJS()

{

alert("ghfcjhgfjhe");

}

from cs i am unable to go to js...
please help me do so..
thanks in advance..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top