I’ve got the flow all worked out thanks to balexandre and rtiq. My .ashx file is being called so I know a portion of the code is working and it is alerting me to an error. When I trace the .NET, the variables pulled in via context.Request[“email”] and context.Request[“optin”] are NULL.
I know there’s something wrong but I can’t see it. I’ve re-edited this post to have the latest code.
jQuery in HEAD
<script type="text/javascript">
$(document).ready(function () {
$(".submitConnectButton").click(function (evt) {
evt.preventDefault();
alert("hello click");
alert($(".emailConnectTextBox").val());
$.ajax({
type: "POST",
url: "/asynchronous/insertEmail.ashx",
data: "{email: '" + $(".emailConnectTextBox").val() + "',optin: '" + $(".connectCheckbox").val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) { alert(msg.d); },
error: function (msg) { alert('Error:' + msg); }
});
});
});
</script>
HTML
<div class="emailConnect">
<asp:TextBox runat="server" ID="TextBox1" CssClass="emailConnectTextBox" BorderStyle="Solid"></asp:TextBox>
<asp:ImageButton id="connectButton" CssClass="submitConnectButton" runat="server" ImageUrl="~/Images/submit_btn.png" /><br />
<asp:CheckBox Checked="true" id="checkbox1" runat="server" CssClass="connectCheckbox" />
</div>
CodeBehind in a .ashx
public class insertEmail : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string strConnection = System.Configuration.ConfigurationManager.AppSettings["SQLConnectString"].ToString();
string email = context.Request["email"],
optin = context.Request["optin"];
string strSQL = "INSERT INTO Emails (emailAddress,optIn) VALUES('" + email.ToString() + "','" + optin.ToString() + "')";
SqlConnection Conn = new SqlConnection(strConnection);
SqlCommand Command = new SqlCommand(strSQL, Conn);
Conn.Open();
Command.ExecuteNonQuery();
Conn.Close();
context.Response.ContentType = "text/plain";
context.Response.Write("email inserted");
}
public bool IsReusable
{
get
{
return false;
}
}
}
The form and elements are acting properly. We are just getting this NULL values and not being able to insert. The ajax is calling the .ashx file properly and the file is compiling, the requested variables are null.. The previous help was awesome, if anyone could help me get this last kink out, you would get a gold star for the day! 🙂
After some searching offline in books, this finally worked for me in concjunction with balexandres .aspx method:
SOLUTION
$.post("/asynchronous/addEmail.aspx", {email: $(".emailConnectTextBox").val(),optin: $(".connectCheckbox").is(':checked')}, function(data) { alert('Successful Submission');});
asynchronousaspxpage calledaddEmail.aspxand delete all HTML except the 1st lineaddEmail.aspxyou place your code behind, like:.
in your main page that has the
.ajax()call change theurlproperty tourl: "/asynchronous/insertEmail.aspx",You will have in your
msginsuccess: function (msg) {}the stringemail insertedThis is what I always do, though, instead of creating an ASPX Page, I use ASHX (Generic Handler) page that does not contain any ASP.NET Page Cycle (faster to load) and it’s a simple page.
if you want to use a
Generic Handlerinstead, create insideasynchronousfolder a file calledinserEmail.ashxand the full code would be:and, remember to change your
urlproperty tourl: "/asynchronous/insertEmail.ashx",from your comment I realized that your
dataproperty was also not correct.the correct is:
your full ajax call should be:
and your
Response.Writein the generic handler should pass a JSON string as wellso, change tgis
context.Response.Write("email inserted");intocontext.Response.Write("{d:'email inserted'});that’s all.