BryianTan is right. But based on my experience calling .click() event do postback but doesn't fire click event of button (when you have ScriptManager in Page) . In that situation you can make use
of __doPostBack method like
Its working but its event is firing on load & post back event.
In my scenario- I'm using gridview & one of column have link button, when user click on link of gridview column then it will change into text box, Now user can make any changes in text box data where user outing I mean text box lost focus then this javascript method
called (on onBlur)with return certian parameter. These parameter I need into code behind OR Call code behind function in C# to update database records.
Almost we have reach ...& finally I need your push up to complete may problem.
Thank you in Advance
Anwar
How to call a C# code behind method from a Java Script function
namespace CallfromJScript
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//The Function You Want to call from JScript
[WebMethod]
public static void ShowMe()
{
ucPopUpContainer._strPopUpType = "2";
ucPopUpContainer._strWidth = "325";
ucPopUpContainer._strHeight = "400";
ucPopUpContainer._strUserControlName = "UserControl2";
ucPopUpContainer.InstanceRef.ShowPopUp();
}
}
}
The above scenario work for UserControl tooo. If it is a UserControl use the html content mentioned above in ascx and the code content in the page in which the user control is used.
Be sure that the function is static and EnablePageMethods="true".
Coool
Contributor
4393 Points
687 Posts
Re: How to call a C# code behind method from a Java Script function
Apr 24, 2009 06:56 AM|LINK
BryianTan is right. But based on my experience calling .click() event do postback but doesn't fire click event of button (when you have ScriptManager in Page) . In that situation you can make use of __doPostBack method like
__doPostBack('<ButtonId>','') ;
Parth Patel
Software Engineer
Anwarul.haqu...
Member
8 Points
5 Posts
Re: How to call a C# code behind method from a Java Script function
Jul 11, 2009 06:26 AM|LINK
Hi Sumit,
Its working but its event is firing on load & post back event.
In my scenario- I'm using gridview & one of column have link button, when user click on link of gridview column then it will change into text box, Now user can make any changes in text box data where user outing I mean text box lost focus then this javascript method called (on onBlur)with return certian parameter. These parameter I need into code behind OR Call code behind function in C# to update database records.
Almost we have reach ...& finally I need your push up to complete may problem.
Thank you in Advance
Anwar
How to call a C# code behind method from a Java Script function
Anwarul.haqu...
Member
8 Points
5 Posts
Re: How to call a C# code behind method from a Java Script function
Jul 11, 2009 06:29 AM|LINK
Hi,
How to do __doPostBack in this case?
plz little explain more
Thankks
abhilashtiru...
Member
4 Points
2 Posts
Re: How to call a C# code behind method from a Java Script function
Aug 08, 2009 04:56 AM|LINK
Could you please explain your solution.
akhi4akhil
Member
30 Points
16 Posts
Re: How to call a C# code behind method from a Java Script function
Sep 16, 2009 08:25 AM|LINK
aspx Designer:
<html>
<head>
<title></title>
</head>
<body>
<script language="javascript" type="text/javascript">
function NewPop(Src) {
PageMethods.ShowMe();
}
</script>
<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server">
</asp:ScriptManager>
<asp:Button ID="Button1" runat="server" Text="Login" OnClientClick="NewPop(this)" />
</body>
</html>
aspx.cs Code:
using System.Web.Services;
namespace CallfromJScript
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//The Function You Want to call from JScript
[WebMethod]
public static void ShowMe()
{
ucPopUpContainer._strPopUpType = "2";
ucPopUpContainer._strWidth = "325";
ucPopUpContainer._strHeight = "400";
ucPopUpContainer._strUserControlName = "UserControl2";
ucPopUpContainer.InstanceRef.ShowPopUp();
}
}
}
The above scenario work for UserControl tooo. If it is a UserControl use the html content mentioned above in ascx and the code content in the page in which the user control is used.
Be sure that the function is static and EnablePageMethods="true".
Regards
Akhil
Mark as answer if helpfull
Akhil
Mark as Answer if found helpfull.
rahul.bansal
Member
6 Points
7 Posts
Re: How to call a C# code behind method from a Java Script function
Dec 02, 2010 04:18 AM|LINK
<script type="text/javascript">PageMethods.SetLabel(myValue, onSuccess, onFailure);
}
document.getElementById(
}
alert(
}
function Sample() {var myValue = document.getElementById('TextBox1').value;function onSuccess(result) {'Label1').innerHTML = result;function onFailure(error) {'fail');</script>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<div>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
<br />
<br /><asp:Label
ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
{
{
TextBox1.Attributes.Add(
}
}
[System.Web.Services.
{
protected void Page_Load(object sender, EventArgs e)if (!IsPostBack)"onkeyup", "Sample();");WebMethod ]public static string SetLabel(string myValue)//Do your logic here}
return myValue;altafpatel
Member
59 Points
15 Posts
Re: How to call a C# code behind method from a Java Script function
Oct 26, 2012 11:13 AM|LINK
Refer following link:
http://tuvianblog.com/2011/06/10/how-to-call-serverside-function-from-client-side-javascript-in-asp-net/