By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,882 Members | 2,453 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,882 IT Pros & Developers. It's quick & easy.

How to call apsx.cs mthod(C#) from javascript?

P: 17
Any body has any idea on How to call apsx.cs mthod(C#) from javascript?

Thanks
Abdun Nabi Sk
Apr 16 '08 #1
Share this Question
Share on Google+
8 Replies


P: 47
Depends what you are trying to achieve?
Apr 16 '08 #2

P: 17
can u show me some examples?

thanks..
Abdun Nabi Sk
Apr 16 '08 #3

P: 47
This is the standard bit of JScript that is in an aspx page but generally you do not send javascript to interact with methods in the cs file:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. <!--
  3. var theForm = document.forms['aspnetForm'];
  4. if (!theForm) {
  5.     theForm = document.aspnetForm;
  6. }
  7. function __doPostBack(eventTarget, eventArgument) {
  8.     if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
  9.         theForm.__EVENTTARGET.value = eventTarget;
  10.         theForm.__EVENTARGUMENT.value = eventArgument;
  11.         theForm.submit();
  12.     }
  13. }
  14. // -->
  15. </script>
  16.  
Apr 16 '08 #4

P: 17
actually I am having a method name
Expand|Select|Wrap|Line Numbers
  1. public void Check(int n)
  2. {
  3. }
  4.  
in aspx.cs file.

in javascript
Expand|Select|Wrap|Line Numbers
  1. function Test(n)
  2. {
  3.   <%Check(n)%>;
  4. }
  5.  
its not calling Check() method....
There are errors like syntax

and if I dont want to send argument then no error but
every time i have to refresh the page...

can u help me

Thanks
Abdun Nabi SK
Apr 16 '08 #5

P: 47
In your case you should probably have a submit button that runs that code. In order to run cs code you need to post to the server to run that code.

So in the aspx page you should add an:

Expand|Select|Wrap|Line Numbers
  1. <asp:button id="btnSubmit" runat="server" Text="Submit" />
  2. <asp:TextBox id="txtCheck" runat="server" />
  3.  
In the cs file in the Initialise() method you can add the eventhandler for the button like so:

Expand|Select|Wrap|Line Numbers
  1. btnSubmit.Click += new EventHandler(Check);
  2.  
Then in the Check() method you created you can access the text field like so:

Expand|Select|Wrap|Line Numbers
  1. public void Check(object s, EventArgs e)
  2. {
  3.     string strCheck = txtCheck.Text;
  4.  
  5.     // operate on strCheck here
  6. }
  7.  
Hope this example will help get you started.
Apr 16 '08 #6

P: 17
I have button that is calling
javascrip:parent.Test(2)

method.....
anyway I have to call c# method from
javascript method Test().

please help me
thanks

Abdun Nabi Sk
Apr 16 '08 #7

Frinavale
Expert Mod 5K+
P: 9,731
I have button that is calling
javascrip:parent.Test(2)

method.....
anyway I have to call c# method from
javascript method Test().

please help me
thanks

Abdun Nabi Sk
You can't really use JavaScript for this.
You're going to have to use Ajax to call server side code.

Are you using Ajax in your web project currently?
If you use Ajax, the page is "partially" posted back to the server, your request is processed and that section of the page is updated.

You can use pure Ajax to do this if you want to, but .NET now has an Ajax framework that simplifies things.


Instead of putting in Ajax calls to do validation, you should consider using the client side Validator objects available to you (eg RequiredFieldValidator, RegularExpressionValidator ...etc).

You should still validate on the server....I would place a call to your Check() method in your button click event and if it return's true, would allow further processing to take place.

-Frinny
Apr 16 '08 #8

P: 7
I'm agree with what Frinavale said.
But if you really want to do validation via js then you may use PageMethods with ASP.NET Ajax.
Apr 18 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.