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関数に渡そうとしましたが、何らかの理由で動作しません。 私が行う必要があるのは、値が完全リフレッシュなしでaspxに戻っている限り、SQLルックアップを動的に行うことだけです。 –
まず、GetDataメソッドを更新してオブジェクトパラメータとして値を受け入れることを前提としていますか?もっと最近の方法については、私は推奨で答えを更新しました。 –
努力の仲間のおかげで、ありがとう、私はできるだけ早くそれを試し、私はどうやって行くのか教えてあげます。 –