【发布时间】:2011-09-22 16:33:55
【问题描述】:
我有一个下拉列表,我使用 jquery ajax 来调用 webmethod。我的预期解决方案是使用 ajax 根据下拉选择的索引更新当前页面上的所有数据字段。
function getDBInfo()
{
var mySelectedIndex = $("#<%=dblParameter.ClientID%>").val(); //id name for dropdown list
$.ajax({
type:"POST",
url:"/Manage/Details.aspx?param=",
data:{}
contentType:application/json; charset=utf-8",
dataType:"json"
success: function(data)
{
//this is what I am trying to accomplish but not sure how I should handle the webservice method to do this or if I am even doing it right so far
$("#<%=txtParameter.ClientID%>;").text(data.Parameter);
$("#<%=txtName.ClientID%>;").text(data.Name);
$("#<%=txtSSN.ClientID%>;").text(data.SSN);
//etc....
}
});
}
然后在我的代码后面我有我的页面方法
protected void Page_Load(object sender, EventArgs e)
{
dblParameter.Attributes.Add("onchange","getDBInfo");
}
[WebMethod]
public static DataRowView getDBInfo(string param)
{
ConnectionStringSettings conn = ConfiguationManager.ConnectionStrings["MasterDBConnectionString"];
SQLDataSource Details = new SqlDataSource(conn.ConnectionString, "SELECT * FROM [tblInfo] WHERE ([ParamID] = "+param);
DataRowView db = (DataRowView)Details.Select(DataSourceSelectArguments.Empty);
return db;
}
我做错了什么,因为在我的 javascript 中调用 data.Name 或 data.Parameter 不起作用。应该改为 data["Parameter"] 吗?还是我在这里离基地很远?
编辑1:
我在这里更改了很多代码,这就是我所拥有的,我现在可以工作了
$(document).ready(function(){
$("#<%=dblParameter.ClientID%>").change(function(){
var myparam= $("#<%=dblParameter.ClientID%>").val(); //id name for dropdown list
$.ajax({
type:"POST",
url:"Details.aspx/getDBInfo",
data:'{param:"'+myparam+'"}',
contentType:application/json; charset=utf-8",
success: function(data)
{
alert(data.d)
}
});
});
});
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string getDBInfo(string param)
{
MyMainClass myInit = new MyMainClass();
string target= myInit.GetInfo(param);
return target;
}
【问题讨论】:
标签: c# asp.net ajax web-services