[英]Call code behind from JavaScript in c#
I have a search page call "companies" when user input keyword and click "search" it will call to some functions to show search results in file "companiesdatagrid". 当用户输入关键字并单击“搜索”时,我有一个搜索页面调用“ companies”,它将调用某些功能以在文件“ companiesdatagrid”中显示搜索结果。
Now, I created a simple dropdownlist in companiesdatagrid.ascx file: 现在,我在companiesdatagrid.ascx文件中创建了一个简单的下拉列表:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" Width="200px" runat="server" DataTextField="ItemTypeName" DataValueField="ItemTypeId"
onchange = "CallServerMethod(this)" ValidationGroup="vgLibItem" >
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
</Triggers>
I want it to call a Java Script function that will call to function 我希望它调用将调用该函数的Java Script函数
public void DropDownList1sel(object sender, EventArgs e)
in the code behind file (CompaniesDatagrid.ascx.cs) However, I will test for a simple case, call for a simple function in code behind : 在文件(CompaniesDatagrid.ascx.cs)后面的代码中,但是,我将测试一个简单的情况,在后面的代码中调用一个简单的函数:
public void add()
{
int a = 1;
int b = 3;
int c = a + b;
}
In the page_load function (file CompanniesDatagrid.ascx.cs) I have: 在page_load函数(文件CompanniesDatagrid.ascx.cs)中,我具有:
protected void Page_Load(object sender, System.EventArgs e)
{
// clear the javascript literal
this.responseLiteral.Text = "";
if (!Page.IsPostBack)
{
if (Request.Form["Method"] == "Add")
{
add();
return;
}
}
And in companiesdatagrid.ascx I have this Java Script: 在companydatagrid.ascx中,我有以下Java脚本:
<script type="text/javascript">
function CallServerMethod() {
alert("changed");
var dataToSend = { Send1: 'Value1', Send2: 'Value2', Method: 'Add' };
var opts =
{
url: 'companiesdatagrid.ascx',
//url: 'companies.ascx',
//url: 'CompaniesDatagrid.ascx.cs',
//url: 'companies.ascx.cs'
//url: 'companiesdatagrid.ascx/add',
//url: 'CompaniesDatagrid.ascx.cs/add',
//url: 'CompaniesDatagrid.aspx/add',
//url: 'DesktopDefault.aspx?tabindex=2&tabid=15/add',
data: dataToSend,
dataType: 'JSON',
type: 'POST',
success: function (response) {
//Do something here if successful
alert('success');
},
error: function () {
alert('failure');
}
}
$.ajax(opts);
alert("changed 2");
}
</script>
It will only show the alert "changed" then "changed 2" then "failure". 它只会显示警报“已更改”,然后“已更改2”,然后显示“失败”。 I never get success alert, which means I never call add() function in the code behind successfully!?
我永远不会收到成功警报,这意味着我永远不会在背后的代码中成功调用add()函数! Note that all the commented at the url I tried and get the same result!
请注意,在我尝试过的网址上的所有评论都得到了相同的结果! How to implement it correctly here ?
如何在这里正确实施? I am really out of my way, please help!
我真的很迷路,请帮忙! Thank you !
谢谢 !
first change url and check it is not reaching at proper resource, then check data is proper not.. 首先更改网址,并检查它是否未到达正确的资源,然后检查数据是否正确。
var dataToSend = { Send1: 'Value1', Send2: 'Value2', Method: 'Add' };
var opts =
$.ajax({
type: "POST",
url: "companiesdatagrid.ascx/methodname to be called",
data: dataToSend ,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert('sucess');
},
error: function() {
alert("error");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.