Confirm message box using code behind in asp.net

Source: Internet
Author: User

When js write confirmation boxes Are frequently used, return confirm ('Are you sure? '), Or use attributes in the background code. add () to add the following, but when there is a need: click the button to execute a piece of code, there is a judgment in this section of code, when the conditions are met, the user is asked whether to confirm the submission. We don't seem to be doing this.

Can this happen? As follows:

Aspx page:

<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Untitled Page </title>
<Script type = "text/javascript" language = "javascript">
Function ShowConfirmation ()
{
If (confirm ("Are you want to show the value? ") = True)
{
// Calling the server side code after confirmation from the user
Document. getElementById ("btnAlelrt"). click ();
}
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: TextBox ID = "txtDetails" runat = "server"> </asp: TextBox>
<Br/>
<Asp: Button ID = "btnDetails" runat = "server" Text = "GetValue" OnClick = "btnDetails_Click"/>
<Asp: Button ID = "btnAlelrt" runat = "server" Text = "GetDetails" OnClick = "btnAlelrt_Click"/>
<Br/>
<Br/>
</Div>
</Form>
</Body>
</Html>

Code Section:

Using System;

Using System. Data;

Using System. Configuration;

Using System. Web;

Using System. Web. Security;

Using System. Web. UI;

Using System. Web. UI. WebControls;

Using System. Web. UI. WebControls. WebParts;

Using System. Web. UI. HtmlControls;

Public partial class _ Default: System. Web. UI. Page

{

Protected void Page_Load (object sender, EventArgs e)

{

// Hide the button from users visibility

BtnAlelrt. Style. Add ("display", "none ");

}

Protected void btnDetails_Click (object sender, EventArgs e)

{

// Here some conditions

If (txtDetails. Text = "111111 ")

{
Page. ClientScript. RegisterStartupScript (this. GetType (), "showAl", "ShowConfirmation ();", true );

}

Else

{

// Do nothing!

}

}

Protected void btnAlelrt_Click (object sender, EventArgs e)

{

// Write your code

Page. ClientScript. RegisterStartupScript (this. GetType (), "showVal", "alert ('" + txtDetails. Text + "');", true );

}

}

 

PS: A small Demo, not original. Google came when I used it. I don't know if it is appropriate to store the homepage... (This is the second time for a newbie. it is not suitable to leave a comment. I want to change places !), Also, how can I fold and indent the code !!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.