Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javasript Confirm() when a condition exist in the server

Status
Not open for further replies.

33396670

Programmer
Jun 26, 2008
5
US
Hi,
I am having a problem trying to pop up a confirm message box in the client side when a condition is met in the server.
I have a delete button that when pressed, could warm the user of something if that condition evaluates true in the server side.
I can get the confirm message box to appear if I add attributes to my delete button on my form_Load event:
something like this:
protected void Page_Load(object sender, EventArgs e)
{
btnDelete.Attributes.Add("OnClientClick","return confirm( 'You are going to Delete everything. Are you sure?';)");

}

but this just guarantee that the message will pop up everytime I pressed the delete button and not just when the condition is met.

I also tried to register a script in my btnDelete_Click event, doing something like this:

protected void btnDelete_Click(object sender, EventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(@"<script language='javascript'>");
sb.Append(@"return confirm('Delete Everything?');");
sb.Append(@"</script>");
if (!ClientScript.IsStartupScriptRegistered("JSScript"))
{

ClientScript.RegisterStartupScript(typeof(Page), "JSScript", sb.ToString());

}

}

but in this case the message box just appear when I click the button twice.


Can anybody help me with this? I would really appreciate any ideas.

Thanks,

Al


 
Just add a conditional statment in your page load:
Code:
 protected void Page_Load(object sender, EventArgs e)
    {
If {some condition is true}
       btnDelete.Attributes.Add("OnClientClick","return confirm( 'You are going to Delete everything. Are you sure?';)"); 

end if

}
I don't know C#, so my syntax is probably wrong, but I am sure you will get what I am saying
 
Thanks for your prompt help,
but this is the thing:
I cannot check for the condition when I am loading the page because this condition is going to be based in what the user selected from a combo box I have.
So, I need to check for the condition after I pressed the delete button.

Any other idea?

Thanks,
Al
 
Create your own js function that pops the confirm message. From the code behind, set a value in a hidden field. From the js function, check the value in the hidden field, and pop the confirm message based on that.
 
Hi jbenson,
Thanks for the response. I have been trying to do it but it doesnt work for me yet though.
I guess my problem is that I dont know how to call the java script function at the moment I want.
Would that be associating the function to one of the attributes of the Hidden Field? Can you help me a littel bit more with this?

Thanks,
Al
 
I have a delete button that when pressed, could warm the user of something if that condition evaluates true in the server side."

As you want this to happen when they press the delete button, this is where you need to attach the function. So when they press that call the function which will then check the value of the hidden field (which you have set based on the DB value) and if it meets certain conditions shows the message.

If this isn't working I would suggest pasting that part of your code (where you call the function and the function itself) here and we will have a look.
 
Thanks for your help.
I Got something to work following your advice about using a HiddtenField and setting its value somewhere in my codebehind page before I delete the Delete button.
This work fine In the following case:
I have a combobox and I want to warm the user when he pressed Delete without selecting an item from the combobox. I can do that because I can set my HiddenField value in the combobox.SelectedIndexChanged event, so whenever I press the Delete button, the value I want would be in the HiddenField already allowing me to identify if an item was selected or not and display the appropiate message to the user.
My Javascript function in this case is:

function ConfirmMsg()
{
obj=document.getElementById ('HiddenField1');
if (obj.value=="true")
return confirm("Are you sure you want to delete the selected TaskCode?");
else
return alert("You have to select a TaskCode");
}
I still have the problem though, of sending a message to the user after checking the Database for some especific condition. This is because to determine the answer for this condition I need to check my database after the user has clicked the Delete button, which won't allow me to run the function on the OnClientCLick attribute of my Delete button as I did before. I was thinking about using like a hidden server control and programmatically fire up the OnCLick event of it, but I havent been successfullin getting it to work.

Any idea on how to do this?

Sorry for the length of the message...

Al
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top