我正在进行在线测验项目。用户将选择问题的数量,问题和选择将在Page_Load方法中随机从db中提取。用户将别致的标记。然后当用户点击提交按钮时,用户的选择将通过AJAX发送到静态页面方法。我是初学者,尽管有一天的工作,我还是无法使用Ajax将数据发送到PageMethod。我需要你的帮助。这是我的代码:
<div id="selections">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<input type="radio" name="secenekler" value="1" id="input1" />
<label class="row qaelement labelClick" for="input1">
<asp:Label ID="TextSecenekA" CssClass="textsecenek" runat="server"></asp:Label>
</label>
<input type="radio" name="secenekler" value="2" id="input2" />
<label class="row qaelement labelClick" for="input2">
<asp:Label ID="TextSecenekB" CssClass="textsecenek" runat="server"></asp:Label>
</label>
<input type="radio" name="secenekler" value="3" id="input3" />
<label class="row qaelement labelClick" for="input3">
<asp:Label ID="TextSecenekC" CssClass="textsecenek" runat="server"></asp:Label>
</label>
<input type="radio" name="secenekler" value="4" id="input4" />
<label class="row qaelement labelClick" for="input4">
<asp:Label ID="TextSecenekD" CssClass="textsecenek" runat="server"></asp:Label>
</label>
<div class="row">
<asp:Button ID="SoruGonder" ClientIDMode="Static" class="btn btn-primary btn-lg offset-md-4 col-md-4" runat="server" Text="My Last Decision!" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
JS:
<script type="text/javascript">
function getRadioValue(theRadioGroup) {
var elements = document.getElementsByName(theRadioGroup);
for (var i = 0, l = elements.length; i < l; i++) {
if (elements[i].checked) {
return elements[i].value;
}
}
return null;
}
$(document).ready(function () {
$('#SoruGonder').click(function () {
var ChoosenChicValue = getRadioValue('secenekler');
if (ChoosenChicValue == null) {
alert("Please Make A Choice");
}
else {
$.ajax({
type: 'POST',
url: '/quiz.aspx/quizpagemethod',
data: ChoosenChicValue,
contentType: 'application/json; charset=utf-8',
datatype: 'json',
success: function (data) {
document.getElementById('<%= TextSoru.ClientID %>').value = data.question;
document.getElementById('<%= TextSecenekA.ClientID %>').value = data.selection1;
document.getElementById('<%= TextSecenekB.ClientID %>').value = data.selection2;
document.getElementById('<%= TextSecenekC.ClientID %>').value = data.selection3;
document.getElementById('<%= TextSecenekD.ClientID %>').value = data.selection4;
},
error: function () {
alert("Ajax Error");
}
});
}
});
});
</script>
public class SoruCevapClass
{
public string question { get; set; }
public string selection1 { get; set; }
public string selection2 { get; set; }
public string selection3 { get; set; }
public string selection4 { get; set; }
}
[System.Web.Services.WebMethod(EnableSession = true)]
protected static string GreetingBtn_Click(string PostedAnswer)
{
System.Diagnostics.Debug.WriteLine("POSTED ANSWER : "+PostedAnswer); //This is not printing on output.
//...
}
答案 0 :(得分:0)
在ScriptManager中添加:EnablePageMethods =“true”