在运行时未调用页面方法 [英] Pagemethods not called during runtime
问题描述
海,
在此先感谢您的大力帮助..
我为国家和州添加了2个下拉列表框.基于被选中的国家,我必须载入各个州.
我使用pagemethod调用了绑定状态的服务器端代码.但是,它没有加载..
任何人都可以纠正此问题.如果对此有任何疑问,请询问
我在此附上我的示例代码:
.aspx
-------
Hai,
Thanks in advance for your great help..
I have added 2 dropdownlist boxes for Country and State. Based on the slected country, i have to load the states.
I used pagemethod to call the Serverside code of Binding states. But, its not loading..
Can anyone please rectify this.. If any doubt on this, pls ask
I herewith attached my sample code below:
.aspx
-------
<body onload="fnOnload();>
<script type="text/javascript" language="javascript" src="Scripts/Register.js"></script>
<form runat="server" id="frmMain">
<div id="reg">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="true">
Register.js:
--------------
Register.js:
--------------
function LoadState() {
var CountryId = document.getElementById("ddlICountry").value;alert(CountryId);
PageMethods.ServerLoadState(CountryId, CallSuccess_LoadState, CallFailed_LoadState);
}
function CallSuccess_LoadState(res) {
var StateID = new Array();
var StateName = new Array();
var Collection = new Array();
var Collection_ID = new Array();
var Collection_Text = new Array();
Collection = res.split('~~~');
Collection_ID = Collection[0];
Collection_Text = Collection[1].split('|');
for (var i = 0; i < Collection_Text.length; i++) {
StateID[i] = i;
StateName[i] = Collection_Text[i].split(',');
}
document.getElementById("ddlIState").options.length = 0;
for (var i = 0; i < StateID.length; ++i) {
addOption(document.getElementById("ddlIState"), StateName[i], StateId[i]);
}
document.getElementById("ddlIState").disabled = false;
}
function CallFailed_LoadState(res, destCtrl) {
alert(res.get_message());
}
function addOption(selectbox, text, value) {
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}
.aspx.cs
-----------
.aspx.cs
-----------
[System.Web.Services.WebMethod]
public static string ServerLoadState(int CountryId)
{
SqlConnection con = null;
string Statelist = "";
string connection = ConfigurationManager.AppSettings["SoftProConnect"];
con = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "sp_GetAllStates";
cmd.Parameters.AddWithValue("@CountryId", CountryId);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 500;
cmd.Connection = con;
if (con.State == 0)
{
con.Open();
}
Statelist = cmd.ExecuteScalar().ToString();
return Statelist;
}
调用下拉列表中的方法:
Calling the method in dropdownlist:
ddlICountry.Attributes.Add("onChange", "javascript:LoadState();");
整理好的代码块-OriginalGriff [/edit]
[edit]Code blocks tidied up - OriginalGriff[/edit]
推荐答案
我在ServerLoadState上方看到了[System.Web.Services.WebMethod]标签. ,您需要在计划调用它的aspx页面中添加对asmx的引用.请查看下面的文章,以了解如何从javascript调用webmethod
http://www.a2zdotnet.com/View.aspx?Id=15 [ ^ ]
I see [System.Web.Services.WebMethod] tag above ServerLoadState.As this is a webmethod,you need to add reference of your asmx in aspx page where you are planning to call it.Please have a look at the below article to understand how webmethods can be called from javascript
http://www.a2zdotnet.com/View.aspx?Id=15[^]
这篇关于在运行时未调用页面方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!