|
-
Jul 16th, 2008, 06:35 AM
#1
[RESOLVED] Embed or use javascript confirmation box in asp.net save button
Hi,
In javascript there is an alert message box...What i want to do is include a javascript popup/ for confirmation of whether the user will save the record or not..
Since asp.net has no message box for web, i was thinking of using javascript..
heres the scenario:
asp.net 2.0 code Code:
protected void saveEmployeeRecord_click()
{
try
{
objEmp.Emp_ID = this.txtEmployee.Text;
//other statements for assigning to object
//show the popup dialog here
//if you click ok in the popup
//call the save method to the database
//if you click cancel, do not save the record
}
catch(Exception ex)
{
//do error handling here
}
}
or what are some other means to do this?
Greg
-
Jul 16th, 2008, 06:59 AM
#2
Frenzied Member
Re: Embed or use javascript confirmation box in asp.net save button
-
Jul 16th, 2008, 09:02 AM
#3
Re: Embed or use javascript confirmation box in asp.net save button
thanks for the input on using ajax
however some of the projects does not use ajax for some reasons....
But there are some that use ajax...
Is my question on post #1 doable?
Greg
-
Jul 16th, 2008, 03:16 PM
#4
Fanatic Member
Re: Embed or use javascript confirmation box in asp.net save button
If you're not using AJAX then JavaScript is the way to go. for that you'll have to add the JS method to the button attributes
Code:
saveEmployeeRecord.Attributes.Add("onclick","javascript:ConfirmUpdate()")
And then in the JS you can do something like
Code:
function ConfirmUpdate()
{
var response = confirm("Are you sure?");
if (response)
{
return true;
}
else
{
return false;
}
}
-
Jul 16th, 2008, 03:45 PM
#5
Re: Embed or use javascript confirmation box in asp.net save button
Dude.
return confirm('Are you sure?');
You can use ButtonName.Attributes.Add() to add the javascript to the button's onclick.
-
Jul 16th, 2008, 04:29 PM
#6
Fanatic Member
Re: Embed or use javascript confirmation box in asp.net save button
Yes a more brief version but that is the way I do as I generally try to avoid writing any inline JS as much as possible. No reasons, just preference.
-
Jul 16th, 2008, 10:04 PM
#7
Re: Embed or use javascript confirmation box in asp.net save button
tnx for the code:
here's the running one...actually i used hidden fields for this:
Code:
<script type ="text/javascript" language="javascript">
//function to save and update
function ConfirmSave()
{
// return confirm("Are you sure you want to update?");
// document.getElementById("HiddenField1").value = "3434";
// var a;
// a = return confirm("Are you sure you want to update?");
// document.getElementById("HiddenField1").value = a;
var response = confirm("Are you sure you want to save?");
if (response)
{
//return for save and update;
document.getElementById("HiddenField1").value = "true";
}
else
{
// return for cancel;
document.getElementById("HiddenField1").value = "false";
}
}
</script>
Code:
protected void Page_Load(object sender, EventArgs e)
{
//use this for deletion of other programs
this.btnSave.Attributes.Add("onclick", "return ConfirmSave();"); //this will trigger if the btnSave will clicked
}
here's the code for save:
code Code:
protected void btnSave_click()
{
//use this code to test whether to save or not
string sq = this.HiddenField1.Value;
if(sq == "true")
{
//save statement
//insert into and etc......
}
}
Tnx guys
Greg
-
Jul 17th, 2008, 05:12 AM
#8
Re: Embed or use javascript confirmation box in asp.net save button
 Originally Posted by rjv_rnjn
Yes a more brief version but that is the way I do as I generally try to avoid writing any inline JS as much as possible. No reasons, just preference.
When you call the ConfirmUpdate() method, that's still inline javascript. There is no difference.
-
Jul 17th, 2008, 11:22 AM
#9
Fanatic Member
Re: Embed or use javascript confirmation box in asp.net save button
When you call the ConfirmUpdate() method, that's still inline javascript. There is no difference.
I'm not sure I get you. But I never suggested calling JS methods from my .Net code. I just add the method name to button attributes and that is pure .Net code.
-
Jul 17th, 2008, 12:16 PM
#10
Re: [RESOLVED] Embed or use javascript confirmation box in asp.net save button
I didn't suggest that either. You mentioned that you want to avoid writing inline JS. Calling a javascript function is inline javascript.
Doing an .Attributes.Add("onclick","javascript:ConfirmUpdate()") is also the equivalent of using inline js because that's what it renders out to in the <input> tag's attribute.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|