Ive been looking for a tutorial which will show me how to call a jquery/javascript popup after I have saved some details back to a database.
Does anyone have any examples of this? Ideally i would like a popup after the post saying the Name entered onto the form i.e Thanks for signing up "John Smith"
This is my code so far, I had the idea of grabing the last entered ID(GetLastInsertId) in the table checking its not 0 then calling a popup but im not sure if this is the correct approach also how would i get the name value from the form?
Razor script
@{
Validation.RequireField("name", "You must enter your Name");
Validation.Add("email",
Validator.Regex(@"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$",
"Invalid format for an email address")
);
var name = "";
var email = "";
decimal lastID = 0m;
if(IsPost && Validation.IsValid()){
email = Request.Form["email"];
name = Request.Form["name"];
var regDate = DateTime.Now;
var db = Database.Open("MiniDB");
var insertCommand = "INSERT INTO Registrations (Email, Name, regDate) VALUES(@0, @1, @2)";
db.Execute(insertCommand, email, name, regDate);
lastID = db.GetLastInsertId();
}
}
ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('hello');", true);
You can write name of user in place of Hello like this alert('"+txtUserName.Text+"');
CS1502: The best overloaded method match for 'System.Web.UI.ScriptManager.RegisterStartupScript(System.Web.UI.Page, System.Type, string, string, bool)' has some invalid arguments
Hi Keyur, there is no .cs file. I am using asp.net webpages using webmatrix. The script is at the top of the page. I dont want to have a cs file. And I dont want the alert triggered on a button click event it has to happend after a succesful post back to
the Database.
I worked it out pretty simple in the end. I added an exception handler to check if data scved correctly the result stored in boolean 'saved'variable. Then just had to do the following: I didnt realsie you could call Javascript from with Razor script! awesome!
<div id="dialog" title="Registration Successful" style="display:none;">
Thankyou for registering with us
</div>
@{
if(saved){
<script type="text/javascript">
$(document).ready(function(){
$( "#dialog" ).dialog({
modal: true,
buttons: {
Ok: function() {
$( this ).dialog( "close" );
}
}
});
});
</script>
}
}
Marked as answer by daveyc on Aug 16, 2012 10:25 AM
daveyC
Member
132 Points
76 Posts
Confirmation popup after data has been stored.
Aug 16, 2012 08:04 AM|LINK
Hi,
Ive been looking for a tutorial which will show me how to call a jquery/javascript popup after I have saved some details back to a database.
Does anyone have any examples of this? Ideally i would like a popup after the post saying the Name entered onto the form i.e Thanks for signing up "John Smith"
This is my code so far, I had the idea of grabing the last entered ID(GetLastInsertId) in the table checking its not 0 then calling a popup but im not sure if this is the correct approach also how would i get the name value from the form?
Razor script
@{ Validation.RequireField("name", "You must enter your Name"); Validation.Add("email", Validator.Regex(@"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$", "Invalid format for an email address") ); var name = ""; var email = ""; decimal lastID = 0m; if(IsPost && Validation.IsValid()){ email = Request.Form["email"]; name = Request.Form["name"]; var regDate = DateTime.Now; var db = Database.Open("MiniDB"); var insertCommand = "INSERT INTO Registrations (Email, Name, regDate) VALUES(@0, @1, @2)"; db.Execute(insertCommand, email, name, regDate); lastID = db.GetLastInsertId(); } }<div> <p>LastID =</p>@lastID </div> <div class = "footerbox"> <h1>Sign up</h1> <form method="post"> <p> <label>Name:</label> <span>@Html.TextBox("name", Request["name"])</span> @Html.ValidationMessage("name") </p> <p> <label>Email:</label> <span>@Html.TextBox("email", Request["email"])</span> @Html.ValidationMessage("email") </p> <div class="btnsignup"> <input type="submit" name="submit" value="Sign Up" /> <img src="/Images/WhiteRightArrow.gif" alt="#"> </div> </form> </div>Keyur Shah
Participant
1002 Points
278 Posts
Re: Confirmation popup after data has been stored.
Aug 16, 2012 08:21 AM|LINK
Write this where you want to show popup.
ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('hello');", true); You can write name of user in place of Hello like this alert('"+txtUserName.Text+"');daveyC
Member
132 Points
76 Posts
Re: Confirmation popup after data has been stored.
Aug 16, 2012 08:36 AM|LINK
When I try this i get
CS1502: The best overloaded method match for 'System.Web.UI.ScriptManager.RegisterStartupScript(System.Web.UI.Page, System.Type, string, string, bool)' has some invalid arguments
Where exactly would this go?
Keyur Shah
Participant
1002 Points
278 Posts
Re: Confirmation popup after data has been stored.
Aug 16, 2012 08:42 AM|LINK
What are you exactly writing?
You have to use this in .cs file in button_click event
daveyC
Member
132 Points
76 Posts
Re: Confirmation popup after data has been stored.
Aug 16, 2012 08:47 AM|LINK
Hi Keyur, there is no .cs file. I am using asp.net webpages using webmatrix. The script is at the top of the page. I dont want to have a cs file. And I dont want the alert triggered on a button click event it has to happend after a succesful post back to the Database.
Thanks
Dave
daveyC
Member
132 Points
76 Posts
Re: Confirmation popup after data has been stored.
Aug 16, 2012 10:25 AM|LINK
I worked it out pretty simple in the end. I added an exception handler to check if data scved correctly the result stored in boolean 'saved'variable. Then just had to do the following: I didnt realsie you could call Javascript from with Razor script! awesome!
<div id="dialog" title="Registration Successful" style="display:none;"> Thankyou for registering with us </div> @{ if(saved){ <script type="text/javascript"> $(document).ready(function(){ $( "#dialog" ).dialog({ modal: true, buttons: { Ok: function() { $( this ).dialog( "close" ); } } }); }); </script> } }