我有麻煩的ASP獲得選擇的值:DropDownList的。我正在使用AJAX並且想要動態更新Default.aspx頁面。這是行得通的,但是,從DropDownList中檢索的值沒有正確傳遞給ASP函數。的DropDownList的SelectedValue不工作
的ASP函數返回的東西,只是沒有一個字符串。我是否正確地做這件事?
的Default.aspx:
<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>
Default.aspx.cs:
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;
}
}
對,你知道是否有更新的方法,人們用這種東西?我試圖將一個Javascript變量傳遞給ASP函數,但由於某種原因它不起作用。 所有我需要做的是動態的SQL查詢,只要值變回ASPX沒有一個總的刷新。 – 2011-12-15 03:32:54