Reputation: 11
After sucessfull sending mail with asp.net I want to execute jQuery script
.NET Code:
void btnSubmit_Click(Object sender, EventArgs e) {
MailMessage objEmail = new MailMessage();
objEmail.To = "mymail@com";
objEmail.From = txtFrom.Text;
objEmail.Cc = txtCc.Text;
objEmail.Subject = "Contact";
objEmail.Body = "note: " + txtComments.Text + " <br> " + "name:" + txtName.Text;
objEmail.Priority = MailPriority.High;
objEmail.BodyFormat = MailFormat.Html;
SmtpMail.SmtpServer = "localhost";
try{
SmtpMail.Send(objEmail);
Response.Write("Email send");
}
catch (Exception exc){
Response.Write("");
}
}
jQuery:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function() {
var par = $('.x');
$(par).hide();
$("button").click(function(){
$(".x").toggle();
});
});
</script>
Both scripts works perfecty, but I have no idea how to combine them in order to close contact form after successful sending mail.
Here is whole contact form:
<div align="center"><button class="button">Napisz do nas!</button></div>
<div class="x">
<form runat="server">
<div align="center">
<table border="0" width="350">
<tr>
<td valign="top"><font face="Verdana" size="2">Imię i Nazwisko:</font></td>
<td height="20"> <asp:TextBox runat="server" Height="21px" Width="215px" ID="txtName"></asp:TextBox>
<br>
<asp:RequiredFieldValidator ID = "req1" ControlToValidate = "txtFrom" Runat = "server" ErrorMessage = "Proszę podać Imię i Nazwisko "></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td valign="top"><font face="Verdana" size="2">E-mail:</font></td>
<td height="24"> <asp:TextBox runat="server" Height="22px" Width="213px" ID="txtFrom"></asp:TextBox>
<br>
<asp:RegularExpressionValidator ID = "reg1" ControlToValidate = "txtFrom" Runat = "server" ErrorMessage = "Invalid Email" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator> <asp:RequiredFieldValidator ID = "req3" ControlToValidate = "txtFrom" Runat = "server" ErrorMessage = "Nieprawidłowy Email" ></asp:RequiredFieldValidator></td>
</tr>
<!--<tr>
<td valign="top"><font face="Verdana" size="2">To</font></td>
<td height="24" valign="top">
<asp:TextBox runat="server" Height="22px" Width="212px" ID="txtTo"></asp:TextBox>
<br>
<asp:RegularExpressionValidator ID = "reg2" ControlToValidate = "txtTo" Runat = "server" ErrorMessage = "Invalid Email" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID = "req4" ControlToValidate = "txtTo" Runat = "server" ErrorMessage = "Please enter recipients E-mail" ></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td valign="top"><font face="Verdana" size="2">Cc</font></td>
<td height="24" valign="top">
<asp:TextBox runat="server" Height="22px" Width="210px" ID="txtCc"></asp:TextBox>
<br>
<asp:RegularExpressionValidator ID = "reg3" ControlToValidate = "txtCc" Runat = "server" ErrorMessage = "Invalid Email" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>-->
<tr>
<td valign="top"><font face="Verdana" size="2">Wiadomość:</font></td>
<td height="80"> <asp:TextBox runat="server" Height="80px" TextMode="Multiline" rows="4" Width="258px" ID="txtComments"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" valign="top" height="10" align="center">
<asp:Button Runat = server ID = btnSubmit OnClick = btnSubmit_Click Text = "Wyślij"></asp:Button>
<input type = "reset" runat = "server" value = "Wyczyść"></td>
</tr>
</table>
</div>
</form>
</div>
Upvotes: 1
Views: 202
Reputation: 767
<div id="x" runat="server">
<!-- your html -->
</div>
In case of successful send of email of the btnSubmit_Click:
x.Visible = false;
Upvotes: 0
Reputation: 18155
What you have to remember is that the .NET code runs on the server and the jQuery code runs in the browser. There is no direct way to "call" JavaScript from .NET.
You can use the ClientScript property of the page object to inject the JavaScript into your page so that when the new page gets rendered into the browser it will contain the script and execute it.
http://msdn.microsoft.com/en-us/library/btf44dc9(v=vs.110).aspx
Upvotes: 1
Reputation: 6946
Most of the time when I need to do this, I place the javascript code in a asp.net placeholder control, which I set to be invisible at page load, and change it to visible when I need to execute the javascript.
This would also allow you to handle the try, catch and finally statements by doing it with 3 different placeholders...
Upvotes: 2