如何使用ajax调用在javascript中调用ascx.cs中的函数? [英] How to call function from ascx.cs in javascript using ajax call?
问题描述
我创建了一个控件,它有一个函数,但我想在任何.aspx页面或母版页上使用ajax调用调用该函数,就像从代码隐藏文件中调用函数一样,对代码隐藏文件很好但是没有调用函数从acsx.cs文件到任何包含/注册该控件的.aspx页面/母版页。
这是我的HTML
I have created one control which has one function but I want to call that function with ajax call on any .aspx page or master page same as like calling function from a code behind file,am fine with codebehind file but stucked with calling function from acsx.cs file to any .aspx page/master page on which am including/registering that control.
this is my HTML
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input type="button" id="Button2" runat="server" value="Button" />
</div>
我的ascx.cs代码
my ascx.cs code
[WebMethod()]
[ScriptMethod()]
public static void myMethod(string text)
{
Label1.text=text;
}
我的ajax调用whn从函数后面调用函数就像
my ajax call whn am calling function from code behind file is like
$(document).ready(function () {
$("#Button2").click(function () {
var text = $("#TextBox1").val();
$.ajax({
type: "POST",
url: "WebForm1.aspx/myMethod",
data: "{ text: '" + text + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: "true",
cache: "false",
success: function (msg) {
alert("success");
},
Error: function (x, e) {
// On Error
}
});
});
});
ajax调用中的
我从codebehind调用函数这就是为什么我要放url: WebForm1.aspx / myMethod,但是在关于从.ascx.cs调用函数时感到很困惑,比如什么是Ur l值,因为我只是在我的页面上使用该控件,如
in ajax call am calling function from codebehind thats why am puting url: "WebForm1.aspx/myMethod", but am confused when about calling the function from .ascx.cs, like what will be a Url value for that ,because am simply using that control on my page like
<UC:userControl id="control1" runat="server" />
我已经在我的页面上包含/注册了
which I have included/registered on my page like
<%@ Register TagPrefix="UC" TagName="userControl " src="~/Controls/Mycontrol.ascx" %>
我的尝试:
我如何解决这个问题?
What I have tried:
how do i resolved this issue?
i tried url value(s) like
<pre> url: "Mycontrol.ascx/myMethod",
还
also
<pre> url: "WebForm1.aspx/Mycontrol.ascx/myMethod",
但是仍然无法得到它
but still am unable to get it
推荐答案
(document).ready(function(){
(document).ready(function () {
(#Button2)。click(function(){
var text =
("#Button2").click(function () { var text =
(#TextBox1)。val();
("#TextBox1").val();
这篇关于如何使用ajax调用在javascript中调用ascx.cs中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!