- 1). Open Visual Studio.NET and click "File."
- 2). Select "New Website," then double-click "ASP.NET Web Site" to create a new website project. Visual Studio will place the project files in the Solution Explorer on the right side of the window.
- 3). Locate "Default.aspx" in the solution explorer.
- 4). Double-click "Default.axpx." The file's code will appear in the editing window.
- 5). Click the "Design" button at the bottom of the window to switch to design view. In this view, you can see the Web page as it will appear in a browser.
- 6). Click "View" and select "Toolbox." Double-click "Label." Visual Studio will add a label to the page.
- 7). Return to the toolbox and double-click "Button." A button will appear on the page next to the label. Double-click that button. Visual Studio will create an event handler and display its code in the editor. The code will look like this:
protected void Button1_Click(object sender, EventArgs e)
{
this.Label1.Text = "Processing Button Click";
}
This code sets the label's text to "Processing Button Click" when you click the button. - 8). Return to the Solution Explorer on the right side of the window and double-click "Default.aspx." Click the "Source" tab at the bottom of the window to view the file's source code.
- 9). Locate this line of code in the editing window:
<asp:Button runat="server" onclick="Button1_Click" Text="Button" />
This code creates the button. - 10
Modify the code so that it looks like this:
<asp:Button runat="server" onclick="Button1_Click" Text="Button"
OnClientClick="return confirm('Continue with this operation?');" />
The code is the same except for the "OnClientClick" property appended to the end of the line. This property causes the browser to open a JavaScript "confirm" window when you click the button. - 11
Press "F5" to run the project. Your browser will open and display the label and the button. Click the button next to the label. A pop-up window will open and display "Continue with this operation?" It will also display two confirmation buttons labeled "Yes" and "No." - 12
Click the "No" confirmation button. The confirmation window will close and nothing will happen. This occurs because you canceled the post back to the Web server by answering "No" in the confirmation window. - 13
Click the button next to the label again. The confirmation window will reopen. Click the "Yes" confirmation button this time. The browser will continue and send a request to the server. The server will then send back a response and cause the code in the event handler to run. This code will set the contents of the label to "Processing Button Click."
SHARE