views:

163

answers:

4

Hi All,

I my application i am using ajax(Updatepanel).I am using the following code to show confirmation dialog box after finishing some update process in database. but it is not working for me.

Problem:

Confirmation box is not displaying.

code:

 protected void imbtnUpdate_Click(object sender, ImageClickEventArgs e)
 {

   // Database process     


 string javaScript = "<script language=JavaScript>\n " + "if(confirm('Do you want to update
the files?'))window.location.href = \"Upload.aspx?ID=" + ID +  
"&pt=Gm&page=Gms\"; else return false;\n" + "</script>";

 RegisterStartupScript("imbtnUpdate_Click", javaScript);
 }
A: 

Dear Geetha

this code is just register on the page the javascript that say... if(... bla bla bla)...

Where is the function ?

The RegisterStartupScript, is not going to set on imbtnUpdateClick, this code, and is not going to call it on Clicking the Update.

Also you must always return false, from the code I see.

Advice: You the view page source code, to see the generated html and see what this code do and then you understand whats the problem.

Aristos
A: 

Try this in your code behind:

imbtnUpdate.Attributes.Add("onclick", "return ConfirmUpdate();");

Then put your script into a javascript function called ConfrimUpdate()

Daniel Dyson
A: 

Are you using AJAX?

Because if your button is in an update panel, and if you are trying to add this startup script on a partial-postback you should register it by using ScriptManager.

 protected void imbtnUpdate_Click(object sender, ImageClickEventArgs e)
 {

   // Database process     


 string javaScript = "<script language=JavaScript>\n " + "if(confirm('Do you want to update
the files?'))window.location.href = \"Upload.aspx?ID=" + ID +  
"&pt=Gm&page=Gms\"; else return false;\n" + "</script>";

// RegisterStartupScript("imbtnUpdate_Click", javaScript);

ScriptManager.RegisterStartupScript(Page, Page.GetType(),"imbtnUpdate_Click", javaScript , true);

 }
burak ozdogan
Yes i am using ajax. I have tried your code. Conform box is not getting displayed?
Geetha
Well, can you try like this: string javaScript = "if(confirm('Do you want to updatethe files?'))window.location.href = \"Upload.aspx?ID=" + ID + " else return false;";Because <script> tags will be automatically by this line:ScriptManager.RegisterStartupScript(Page, Page.GetType(),"imbtnUpdate_Click", javaScript , true);
burak ozdogan
ho no. I have tried this. Not showing. I dont know where the problem is.
Geetha
A: 

You should use RegisterClientScriptBlock instead of RegisterStartupScript

Your code should be look like this

protected void imbtnUpdate_Click(object sender, ImageClickEventArgs e) {

// Database process

string javaScript = "\n " + "if(confirm('Do you want to update the files?'))window.location.href = \"Upload.aspx?ID=" + ID +
"&pt=Gm&page=Gms\"; else return false;\n" + "";

Page.RegisterClientScriptBlock("imbtnUpdate_Click", javaScript); }

Waqas Zamir
I am not getting the dialogbox.
Geetha