C# DropDownList SelectedValue不工作
在asp:DropDownList中获取所选值时遇到问题。我正在使用AJAX,希望动态更新Default.aspx页面。这是可行的,但是,从DropDownList中检索的值没有正确地传递给ASP函数 ASP函数返回的是一些内容,而不是字符串。我这样做对吗 Default.aspx:C# DropDownList SelectedValue不工作,c#,javascript,asp.net,ajax,C#,Javascript,Asp.net,Ajax,在asp:DropDownList中获取所选值时遇到问题。我正在使用AJAX,希望动态更新Default.aspx页面。这是可行的,但是,从DropDownList中检索的值没有正确地传递给ASP函数 ASP函数返回的是一些内容,而不是字符串。我这样做对吗 Default.aspx: <script type="text/javascript"> // Calls an ASP function, gets user surname function GetData(){
<script type="text/javascript">
// Calls an ASP function, gets user surname
function GetData(){
var response = UserForm.GetData();
alert (response); // returns [object object]
document.getElementById("surnameLabel").innerHTM = response;
}
</script>
<asp:SqlDataSource
id="Users"
runat="server"
connectionstring="<%$ ConnectionStrings:ConnectionString %>"
selectcommand="select username, userid from users"
>
</asp:SqlDataSource>
<asp:DropDownList
id="UserList"
name="UserList"
runat="server"
DataSourceID="Users"
DataTextField="username"
DataValueField="userid"
>
</asp:DropDownList>
<input type="button" onclick="GetData();" value="Get Surname" />
<asp:Label name="surnameLabel" id="surnameLabel" Text="No user selected"></asp:Label>
public partial class UserForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(AwardsForm));
if (!this.IsPostBack) { }
}
// This function is called from the javascript function and looks up the users surname
[Ajax.AjaxMethod(HttpSessionStateRequirement.ReadWrite)]
public string GetData()
{
string userid = UserList.SelectedValue.ToString(); // this value keeps on equalling null
// Do some SQL using this ID to look up user surname
return Surname;
}
}
根据您的代码,您似乎正在使用Ajax.Net。我已经很久没有使用这个库了,但我记得它不支持发回控件值,或者您必须在某个地方调用另一个设置或属性才能使它工作 无论如何,我认为您最好更改调用以在方法调用中包含所选值 您应该能够执行以下操作:
var ddList = document.getElementById("UserList");
var response = UserForm.GetData(ddList.options[ddList.selectedIndex].value);
更新
由于Ajax.net已经过时(codeplex的最后一次更新是在2006年),我建议在页面中使用静态WebMethod来提供此功能
以下是您应该进行的更改:
1) 导入System.Web.Services命名空间
using System.Web.Services;
2) 将GetData方法转换为以userid为参数并使用WebMethod属性修饰的静态方法:
[WebMethod]
public static string GetData(string sUserId)
{
// Do some SQL using this ID to look up user surname
return Surname;
}
3) 将scriptmanager添加到您的页面,或将其更改为以下内容(如果已存在):
<asp:ScriptManager ID="ScriptManager1"
runat="server" EnablePageMethods="true">
</asp:ScriptManager>
这方面的例子很好
请注意,一些示例,包括上面链接中的示例,在脚本管理器声明和javascript调用上有一些变化,但我对这些没有任何经验。根据您的代码,您似乎正在使用Ajax.Net。我已经很久没有使用这个库了,但我记得它不支持发回控件值,或者您必须在某个地方调用另一个设置或属性才能使它工作 无论如何,我认为您最好更改调用以在方法调用中包含所选值 您应该能够执行以下操作:
var ddList = document.getElementById("UserList");
var response = UserForm.GetData(ddList.options[ddList.selectedIndex].value);
更新
由于Ajax.net已经过时(codeplex的最后一次更新是在2006年),我建议在页面中使用静态WebMethod来提供此功能
以下是您应该进行的更改:
1) 导入System.Web.Services命名空间
using System.Web.Services;
2) 将GetData方法转换为以userid为参数并使用WebMethod属性修饰的静态方法:
[WebMethod]
public static string GetData(string sUserId)
{
// Do some SQL using this ID to look up user surname
return Surname;
}
3) 将scriptmanager添加到您的页面,或将其更改为以下内容(如果已存在):
<asp:ScriptManager ID="ScriptManager1"
runat="server" EnablePageMethods="true">
</asp:ScriptManager>
这方面的例子很好
请注意,一些示例,包括上面链接中的示例,在脚本管理器声明和javascript调用上有一个变体,但我对它们没有任何经验。好的,您知道人们是否有一种更新的方法用于这类事情吗?我曾尝试将Javascript变量传递给ASP函数,但由于某些原因,它无法工作。我所需要做的就是动态地进行SQL查找,只要该值返回到aspx而不进行完全刷新?至于最近的方法,我已经更新了一个建议的答案。感谢你的努力,我会尽快测试它,并让你知道我如何去。得到它的工作,我会张贴最终的代码很快。谢谢你,朋友!好的,你知道有没有一种更新的方法可以用来做这类事情吗?我曾尝试将Javascript变量传递给ASP函数,但由于某些原因,它无法工作。我所需要做的就是动态地进行SQL查找,只要该值返回到aspx而不进行完全刷新?至于最近的方法,我已经更新了一个建议的答案。感谢你的努力,我会尽快测试它,并让你知道我如何去。得到它的工作,我会张贴最终的代码很快。谢谢你,朋友!