最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript confirm box in C# code behind - Stack Overflow

programmeradmin0浏览0评论

I am trying to show a javascript confirm box in the middle of some server side code and after getting user confirmation continue processing but confirm box does not show up. I even wrote some sample code but no success.

after some server processing I need to ask user a question and after user confirmation continue some other server code. it seems to be very simple. even alert box work. How can I solve it?

please note that I can not call javascript confirmbox straight from buttonclick I need to do some serverside code and if that was ok then I want to show a confirmbox for continuation.

here is the code

<asp:Button ID="btn_deletefromDB" runat="server" OnClick="btn_deletefromDB_Click"  Text="Delete from Datatbase);" />


protected void btn_deletefromDB_Click(object sender, EventArgs e)
{

         //Delete service from Database 
        // some server side processing code
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", "return confirm('Do you want to delete it from runtime too? Click OK to proceed.');", true);
        Label1.Text = "delete from runtime confirmed";
        // continue and delete from runtime
        //bla bla bla

}

I am trying to show a javascript confirm box in the middle of some server side code and after getting user confirmation continue processing but confirm box does not show up. I even wrote some sample code but no success.

after some server processing I need to ask user a question and after user confirmation continue some other server code. it seems to be very simple. even alert box work. How can I solve it?

please note that I can not call javascript confirmbox straight from buttonclick I need to do some serverside code and if that was ok then I want to show a confirmbox for continuation.

here is the code

<asp:Button ID="btn_deletefromDB" runat="server" OnClick="btn_deletefromDB_Click"  Text="Delete from Datatbase);" />


protected void btn_deletefromDB_Click(object sender, EventArgs e)
{

         //Delete service from Database 
        // some server side processing code
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", "return confirm('Do you want to delete it from runtime too? Click OK to proceed.');", true);
        Label1.Text = "delete from runtime confirmed";
        // continue and delete from runtime
        //bla bla bla

}
Share Improve this question edited Mar 26, 2013 at 7:24 Niloofar asked Mar 26, 2013 at 5:17 NiloofarNiloofar 7519 silver badges9 bronze badges 1
  • I used RegisterClientScriptBlock instead of RegisterStartupScript and it shows the confirm box at least but cancel does not work. Here some one had some suggestions I have not tested it yet though stackoverflow./questions/11643515/… – Niloofar Commented Mar 27, 2013 at 11:25
Add a ment  | 

3 Answers 3

Reset to default 3

Instead of triggering this in the code-behind, you should add it to the Button1 OnClientClick event like so:

<asp:Button ID="Button1" runat="server" OnClientClick="return confirm('Do you want to delete it? Click OK to proceed.');" OnClick="Button1_Click" />

Web applications do not work like this. You cannot ask for user input in the middle of page's server-side life-cycle. This question has to be asked client-side and user's response has to e to the server as part of the page's submitted data.

All ScriptManager.RegisterStartupScript does is contributes to the final html content that will be sent to the client when the page pletes request processing. Keep in mind that by the time this html content arrives to client puter the user may have closed the browser and gone home.

Use AJAX to submit query to server and make the server code so that it does it’s processing in two parts. I don’t have any working examples of this but this is how it would work in general. For posting data to server you can use native AJAX objects in JS or any other library as others suggested and for processing data on the server side you can use generic handlers (ashx) instead of standard web pages.

  1. Send request to the server.
  2. Catch the first part of the processing via JS on the client page.
  3. Show JS window
  4. Submit the other part to server

You’ll have to send results of the first part of processing back to the client because server will not be able to connect second request with the first one by default.

发布评论

评论列表(0)

  1. 暂无评论