Default.aspx
function invokeCastVoteASPX(selectedVote, successFn, errorFn) {
var url = "/mobile/polls/PollProcessorAsync.aspx";
//Create list of parameters in the form:
//{"nam1":"val1","name2":"val2"}
jsonData = "{'selectedVote':'" + selectedVote + "'}";
$.ajax({
type: "POST",
url: url,
contentType: "application/json; charset=utf-8",
data: jsonData,
dataType: "json",
success: successFn,
error: errorFn
});
}
function CastVote() {
// Get selected vote
var selectedVote = GetSelectedVote();
alert(selectedVote);
if (selectedVote != null) {
alert(selectedVote);
// Cast Vote
invokeCastVoteASPX(selectedVote, OnSuccess, OnFailed);
}
// Close floating div
}
function OnSuccess(result) {
alert('CallBack Occured OnSuccess');
// Close floating div
}
function OnFailed(error) {
alert('CallBack Occured OnFailed');
}
function GetSelectedVote() {
var list = document.getElementById('<%= rblPollAnswers.ClientID %>'); //Client ID of the radiolist
var inputs = list.getElementsByTagName("input");
var selected;
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
selected = inputs[i];
break;
}
}
if (selected) {
return selected.value;
}
}
</script>
<asp:Label ID="lblPollQuestion" runat="server"></asp:Label>
<asp:RadioButtonList ID="rblPollAnswers" runat="server" />
<asp:Button ID="btnVote" runat="server" Text="Vote" OnClientClick="CastVote();return false;" />
PollProcessorAsync.aspx
<script runat="server">
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
string input = "";
// Make sure we were posted to
if (Request.RequestType == "POST")
{
try
{
// Get posted input stream and convert to a readable string
StreamReader sr = new StreamReader(Request.InputStream);
input = sr.ReadToEnd();
}
catch
{
input = "";
}
}
RenderOutput(input);
}
protected void RenderOutput(string request)
{
int success = 0;
string errorMessage = "";
string errorType = "application";
if (!String.IsNullOrEmpty(request))
{
try
{
JsonData json = JsonMapper.ToObject(request);
if (json != null && json.Count > 0)
{
string selectedVote = (!json["selectedVote"].IsString) ? "" : json["selectedVote"].ToString();
// Begin persiting vote to database
Guid userID = Guid.NewGuid();
DateTime dateTimeStamp = DateTime.Now;
// connection string
SqlConnection mySqlConnection = CustomerDAL.GetPrimarySqlConnection();
try
{
try
{
mySqlConnection.Open();
}
catch (Exception)
{
mySqlConnection = CustomerDAL.GetSecondarySqlConnection();
mySqlConnection.Open();
}
// Sql Command object initialized with SQL INSERT
SqlCommand mySqlCommand = new SqlCommand("INSERT INTO PollUserResponses (UserID, PollAnswerID, DateTimeStamp) VALUES (@UserID, @PollAnswerID, @DateTimeStamp)", mySqlConnection);
mySqlCommand.Parameters.AddWithValue("@UserID", userID);
mySqlCommand.Parameters.AddWithValue("@PollAnswerID", Convert.ToInt16(selectedVote));
mySqlCommand.Parameters.AddWithValue("@DateTimeStamp", dateTimeStamp);
// Execute
mySqlCommand.ExecuteNonQuery();
success = 1;
errorType = "none";
errorMessage = "";
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
finally
{
// close the Sql Connection
mySqlConnection.Close();
}
}
}
catch (System.Collections.Generic.KeyNotFoundException)
{
errorMessage = "KeyNotFoundException";
}
catch (Exception ex)
{
errorMessage = ex.Message;
}
}
string jsonOutputString = @"
{
'd': {
's':" + success.ToString() + @",
'e': {
't': '" + errorType + @"',
'msg': '" + errorMessage + @"'
}
}
}";
JsonData jsonOutput = JsonMapper.ToObject(jsonOutputString);
string output = jsonOutput.ToJson();
Response.Clear();
Response.ContentType = "text/plain";
Response.Write(output);
Response.End();
}
</script>
Great Resource For Consuming ASPX in ASP.NET using jQuery