Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have created a page. There are the required fields. and at the end there is a submit button. What I want to do is this:

When an user clicks on the submit button, the data should be saved in the database. after that a msg box should appear showing some details of the user e.g: name, username, email, password etc. then after clicking a button in that msgbox, the page should be redirected to somewhere else.

I have put the whole code inside a try-catch-finally segment. I have checked that data are successfully moved to the database. But I'm unable to switch to a jquery msgbox via <asp:button></button>. How can I do this? help will be appreciated. thanks. :)


Actually I'm new in asp.net. So may be I did a small mistake while asking the question. By saying "Register page", I meant I created it myself.

I think the code will help you understand well.

Here is the UserRegister.aspx file code:

ASP.NET
<%@ Page Title="" Language="C#" MasterPageFile="~/BookStoreMasterPage1.master" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="Register" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        .style2
        {
            height: 1167px;
        }
        .style3
        {
            width: 57%;
            margin-left: 313px;
            height: 38px;
        }
        .style4
        {
            text-align: left;
            height: 48px;
            color: #000000;
        }
        .style7
        {
            width: 167px;
            font-family: "Times New Roman", Times, serif;
            font-size: medium;
            text-align: right;
        }
        .style8
        {
            text-align: left;
            width: 297px;
        }
        .style9
        {
            font-family: "Times New Roman", Times, serif;
            font-size: medium;
        }
        .style12
        {
            text-align: left;
            width: 38px;
        }
        .style13
        {
            text-align: left;
            width: 118px;
        }
        .style14
        {
            text-align: left;
        }
        .style15
        {
            text-align: right;
            width: 166px;
            font-family: "Times New Roman", Times, serif;
            font-size: medium;
        }
        .style22
        {
            text-align: left;
            width: 300px;
        }
        .style25
        {
            font-size: x-large;
            font-family: "Times New Roman", Times, serif;
            text-align: left;
            background-color: #FFFFFF;
        }
        .style26
        {
            background-color: #FFFFFF;
        }
        .style27
        {
            font-size: x-large;
            font-family: "Times New Roman", Times, serif;
            text-align: left;
            color: #000000;
            background-color: #FFFFFF;
        }
        .style28
        {
            text-align: left;
            color: #000000;
        }
        .style29
        {
            width: 720px;
            margin-left: 315px;
        }
        .style30
        {
            width: 100%;
        }
        .style32
        {
            width: 225px;
        }
        .style34
        {
            text-align: left;
            width: 241px;
        }
        </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div class="style2" 
        
        style="background-image: removed('images/BS3.jpg'); color: #FFFFFF; font-size: larger; text-align: center;">
        <div class="style4">
            <br class="style26" />
            <span class="style25">                                                    General Information </span></div>
        <table class="style3" frame="below">
            <tr>
                <td class="style7">
                    Firstname:</td>
                <td class="style8">
                    <asp:TextBox ID="TextBox_Firstname" runat="server" Width="200px"></asp:TextBox>
 
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                        ControlToValidate="TextBox_Firstname" ErrorMessage="*" 
                        Font-Names="Times New Roman" Font-Size="Medium" BackColor="Black"></asp:RequiredFieldValidator>
                </td>
                <td>
                     </td>
            </tr>
            <tr>
                <td class="style7">
                    Middlename:</td>
                <td class="style8">
                    <asp:TextBox ID="TextBox_Middlename" runat="server" Width="200px"></asp:TextBox>
 <span class="style9">(Optional)</span></td>
                <td>
                     </td>
            </tr>
            <tr>
                <td class="style7">
                    Lastname:</td>
                <td class="style8">
                    <asp:TextBox ID="TextBox_Lastname" runat="server" Width="200px"></asp:TextBox>
 
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                        ControlToValidate="TextBox_Lastname" ErrorMessage="*" 
                        Font-Names="Times New Roman" Font-Size="Medium" BackColor="Black"></asp:RequiredFieldValidator>
                </td>
                <td>
                     </td>
            </tr>
            <tr>
                <td class="style7">
                    Username:</td>
                <td class="style8">
                    <asp:TextBox ID="TextBox_Username" runat="server" Width="200px"></asp:TextBox>
 
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                        ControlToValidate="TextBox_Username" ErrorMessage="*" 
                        Font-Names="Times New Roman" Font-Size="Medium" BackColor="Black"></asp:RequiredFieldValidator>
                </td>
                <td>
                     </td>
            </tr>
            <tr>
                <td class="style7">
                    Password:</td>
                <td class="style8">
                    <asp:TextBox ID="TextBox_Password" runat="server" TextMode="Password" 
                        Width="200px"></asp:TextBox>
 
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
                        ControlToValidate="TextBox_Password" ErrorMessage="*" 
                        Font-Names="Times New Roman" Font-Size="Medium" BackColor="Black"></asp:RequiredFieldValidator>
                </td>
                <td>
                     </td>
            </tr>
            <tr>
                <td class="style7">
                    Confirm Password:</td>
                <td class="style8">
                    <asp:TextBox ID="TextBox_CPassword" runat="server" TextMode="Password" 
                        Width="200px"></asp:TextBox>
 
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" 
                        ControlToValidate="TextBox_CPassword" ErrorMessage="*" 
                        Font-Names="Times New Roman" Font-Size="Medium" BackColor="Black"></asp:RequiredFieldValidator>
                </td>
                <td>
                     </td>
            </tr>
        </table>
         <asp:CompareValidator ID="CompareValidator1" runat="server" 
            ControlToCompare="TextBox_Password" ControlToValidate="TextBox_CPassword" 
            ErrorMessage="* Passwords did not match" Font-Bold="True" 
            BackColor="Black"></asp:CompareValidator>
        <br />
        <div class="style14">
                                                                          
            <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
            </asp:ToolkitScriptManager>
            <br />
            <span class="style27">                                                    Personal Information</span></div>
        <table class="style3" frame="below">
            <tr>
                <td class="style7">
                    Date of Birth:</td>
                <td class="style8" colspan="2">
                    <asp:TextBox ID="TextBox_DOB" runat="server" Width="200px"></asp:TextBox>
                    <asp:CalendarExtender ID="CalenderExtender1" runat="server" TargetControlID="TextBox_DOB" Animated="false"></asp:CalendarExtender>
                     
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" 
                        ControlToValidate="TextBox_DOB" ErrorMessage="*" Font-Names="Times New Roman" 
                        Font-Size="Medium" BackColor="Black"></asp:RequiredFieldValidator>
                    <br />
                </td>
                <td>
                     </td>
            </tr>
            <tr>
                <td class="style7">
                    Gender:</td>
                <td class="style12">
                     
                    <asp:RadioButtonList ID="RadioButtonList_Gender" runat="server" 
                        RepeatDirection="Horizontal" Font-Names="Times New Roman" 
                        Font-Size="Large">
                        <asp:ListItem>Male</asp:ListItem>
                        <asp:ListItem>Female</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
                <td class="style13">
                       
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" 
                        ControlToValidate="RadioButtonList_Gender" ErrorMessage="Choose Gender" 
                        Font-Names="Times New Roman" Font-Size="Medium" BackColor="Black"></asp:RequiredFieldValidator>
                </td>
                <td>
                     </td>
            </tr>
        </table>
        <div class="style28">
            <br class="style26" />
            <br class="style26" />
            <span class="style25">                                                    Contact Information </span></div>
        
        <table class="style3" frame="below">
            <tr>
                <td class="style15">
                    Address Line 1:</td>
                <td class="style22">
                    <asp:TextBox ID="TextBox_Address1" runat="server" Width="200px"></asp:TextBox>
                 
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" 
                        ControlToValidate="TextBox_Address1" ErrorMessage="*"></asp:RequiredFieldValidator>
                </td>
                <td class="style14">
                     </td>
            </tr>
            <tr>
                <td class="style15">
                    Address Line 2:</td>
                <td class="style22">
                    <asp:TextBox ID="TextBox_Address2" runat="server" Width="200px"></asp:TextBox>
                 
                </td>
                <td class="style14">
                     </td>
            </tr>
            </table>

            <table class="style3" frame="below">
            <td>
            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
            <ContentTemplate>
            <table>
            <tr>
                <td class="style15">
                    Country:</td>
                <td class="style22">
                    <asp:DropDownList ID="DropDownList_Country" runat="server" AutoPostBack="True" 
                        onselectedindexchanged="DropDownList_Country_SelectedIndexChanged" 
                        Width="202px">
                        <asp:ListItem>Select Country</asp:ListItem>
                    </asp:DropDownList>
                     
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" 
                        ControlToValidate="DropDownList_Country" ErrorMessage="*"></asp:RequiredFieldValidator>
                </td>
                <td class="style34">
                     </td>
            </tr>
            <tr>
                <td class="style15">
                    State:</td>
                <td class="style22">
                    <asp:DropDownList ID="DropDownList_State" runat="server" 
                        onselectedindexchanged="DropDownList_State_SelectedIndexChanged" 
                        Width="202px" AutoPostBack="True">
                        <asp:ListItem>Select State</asp:ListItem>
                    </asp:DropDownList>
                     
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" 
                        ControlToValidate="DropDownList_State" ErrorMessage="*"></asp:RequiredFieldValidator>
                </td>
                <td class="style34">
                     </td>
            </tr>
            <tr>
                <td class="style15">
                    Ciy:</td>
                <td class="style22">
                    <asp:DropDownList ID="DropDownList_City" runat="server" 
                        Width="202px">
                        <asp:ListItem>Select City</asp:ListItem>
                    </asp:DropDownList>
                     
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" 
                        ControlToValidate="DropDownList_City" ErrorMessage="*"></asp:RequiredFieldValidator>
                </td>
                <td class="style34">
                     </td>
            </tr>
            </table>
            </ContentTemplate>
            </asp:UpdatePanel>
            </td>
            </table>

            <table class="style3" frame="below">
            <tr>
                <td class="style15">
                    Pin:</td>
                <td class="style22">
                    <asp:TextBox ID="TextBox_Pin" runat="server" Width="200px"></asp:TextBox>
                 
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator13" runat="server" 
                        ControlToValidate="TextBox_Pin" ErrorMessage="*"></asp:RequiredFieldValidator>
                </td>
                <td class="style14">
                     </td>
            </tr>
            <tr>
                <td class="style15">
                    Contact No. 1</td>
                <td class="style22">
                    <asp:TextBox ID="TextBox_Contact1" runat="server" Width="200px"></asp:TextBox>
                 
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator14" runat="server" 
                        ControlToValidate="TextBox_Contact1" ErrorMessage="*"></asp:RequiredFieldValidator>
                </td>
                <td class="style14">
                     </td>
            </tr>
            <tr>
                <td class="style15">
                    Contact No. 2</td>
                <td class="style22">
                    <asp:TextBox ID="TextBox_Contact2" runat="server" Width="200px"></asp:TextBox>
                </td>
                <td class="style14">
                     </td>
            </tr>
            <tr>
                <td class="style15">
                    Email:</td>
                <td class="style22">
                    <asp:TextBox ID="TextBox_Email" runat="server" Width="200px"></asp:TextBox>
                 
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator15" runat="server" 
                        ControlToValidate="TextBox_Email" ErrorMessage="*"></asp:RequiredFieldValidator>
                </td>
                <td class="style14">
                     </td>
            </tr>
        </table>
        <br />
        <asp:Label ID="Label_Error" runat="server" BackColor="Black" Font-Bold="True" 
            Font-Names="Times New Roman" Visible="False"></asp:Label>
        <br />
        <div class="style29" style="background-color: #000000; height: 68px;">
            <table class="style30" frame="box">
                <tr>
                    <td class="style32">
                        <asp:Button ID="Button_Reset0" runat="server" CausesValidation="false" 
                            Font-Names="Times New Roman" Font-Size="Medium" Height="30px" Text="Reset" 
                            Width="60px" />
                         <asp:Button ID="Button_Cancel" runat="server" CausesValidation="false" 
                            Font-Names="Times New Roman" Font-Size="Medium" Height="30px" Text="Cancel" 
                            Width="60px" onclick="Button_Cancel_Click" />
                    </td>
                    <td>
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        
                                <asp:CheckBox ID="CheckBox1" runat="server" Font-Names="Times New Roman" 
                                    Font-Size="Medium" 
                                    Text="I have read and agreed to the terms & conditions" 
                                    AutoPostBack="True" />
<br />
                                <asp:Button ID="Button_Submit" runat="server" Font-Names="Times New Roman" 
                                    Font-Size="Medium" Text="Submit" Height="30px" 
                                    Width="60px" onclick="Button_Submit_Click" />
                                    
                </ContentTemplate>
                </asp:UpdatePanel>
                    </td>
                </tr>
            </table>
        </div>
        <br />
        </div>
</asp:Content>


Here is the .aspx.cs file code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class Register : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS_New_1;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True");
    SqlCommand cmd;
    SqlCommand cmd1;
    SqlCommand cmd2;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (CheckBox1.Checked == false)
        {
            Button_Submit.Enabled = false;
        }
        else
        {
            Button_Submit.Enabled = true;
        }

        if (!IsPostBack)
        {
            DropDownList_Country.AppendDataBoundItems = true;
            cmd = new SqlCommand("Select ID, CountryName From Country", con);
            cmd.Parameters.AddWithValue("@CountryID", DropDownList_Country.SelectedItem.Value);

            try
            {
                con.Open();
                DropDownList_Country.DataSource = cmd.ExecuteReader();
                DropDownList_Country.DataTextField = "CountryName";
                DropDownList_Country.DataValueField = "ID";
                DropDownList_Country.DataBind();
            }
            catch
            {
                Label_Error.Visible = true;
                Label_Error.Text = "An error occurred. Please refresh and try again.";
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
    }

    protected void Button_Reset_Click(object sender, EventArgs e)
    {
        Response.Redirect("Register.aspx");
    }
    protected void Button_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Login.aspx");
    }

    protected void DropDownList_Country_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList_State.Items.Clear();
        DropDownList_State.Items.Add(new ListItem("Select State", ""));
        DropDownList_City.Items.Clear();
        DropDownList_City.Items.Add(new ListItem("Select City", ""));

        DropDownList_State.AppendDataBoundItems = true;

        cmd = new SqlCommand("Select ID, StateName From State " + "Where CountryID=@CountryID", con);
        cmd.Parameters.AddWithValue("@CountryID", DropDownList_Country.SelectedItem.Value);

        try
        {
            con.Open();
            DropDownList_State.DataSource = cmd.ExecuteReader();
            DropDownList_State.DataTextField = "StateName";
            DropDownList_State.DataValueField = "ID";
            DropDownList_State.DataBind();

            if (DropDownList_State.Items.Count > 1)
            {
                DropDownList_State.Enabled = true;
            }
            else
            {
                DropDownList_State.Enabled = false;
                DropDownList_City.Enabled = false;
            }
        }
        catch
        {
            Label_Error.Visible = true;
            Label_Error.Text = "An error occurred. Please refresh and try again.";
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }

    protected void DropDownList_State_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList_City.Items.Clear();
        DropDownList_City.Items.Add(new ListItem("Select City"));
        DropDownList_City.AppendDataBoundItems = true;

        cmd = new SqlCommand("Select ID, CityName From City" + " Where StateID=@StateID", con);
        cmd.Parameters.AddWithValue("@StateID", DropDownList_State.SelectedItem.Value);
        try
        {
            con.Open();
            DropDownList_City.DataSource = cmd.ExecuteReader();
            DropDownList_City.DataTextField = "CityName";
            DropDownList_City.DataValueField = "ID";
            DropDownList_City.DataBind();

            if (DropDownList_City.Items.Count > 1)
            {
                DropDownList_City.Enabled = true;
            }
            else
            {
                DropDownList_City.Enabled = false;
            }
        }
        catch
        {
            Label_Error.Visible = true;
            Label_Error.Text = "An error occurred. Please refresh and try again.";
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }
    protected void Button_Submit_Click(object sender, EventArgs e)
    {
        try
        {
            cmd = new SqlCommand("Insert Into UserLogin Values('" + TextBox_Username.Text + "', '" + TextBox_Password.Text + "', '" + TextBox_Firstname.Text + "')", con);
            cmd1 = new SqlCommand("Insert Into UserPersonalInfo Values('" + TextBox_Username.Text + "', '" + TextBox_Firstname.Text + "', '" + TextBox_Middlename.Text + "', '" + TextBox_Lastname.Text + "', '" + RadioButtonList_Gender.SelectedItem.ToString() + "', '" + TextBox_DOB.Text + "')", con);
            cmd2 = new SqlCommand("Insert Into UserContactInfo Values('" + TextBox_Username.Text + "', '" + TextBox_Address1.Text + "', '" + TextBox_Address2.Text + "', '" + TextBox_Contact1.Text + "', '" + TextBox_Contact2.Text + "', '" + TextBox_Email.Text + "', '" + DropDownList_Country.SelectedItem.Text + "', '" + DropDownList_State.SelectedItem.Text + "', '" + DropDownList_City.SelectedItem.Text + "', '" + TextBox_Pin.Text + "')", con);

            con.Open();

            cmd.ExecuteNonQuery();
            cmd1.ExecuteNonQuery();
            cmd2.ExecuteNonQuery();

            con.Close();
        }
        catch
        {
            Label_Error.Text = "This username is already taken.";
        }
        finally
        {
        }
    }
}
Posted
Updated 13-Jul-13 5:40am
v3

if you want to use javascript then this is very easy

this is one example you have to do it for whole form

take on hidden field with status 0 and 1

and take all labels for fields of your forms when user click on submit then

labels.text= textboxes.text
and do hiddenfield status to 1
after that using script manager call this javscript function that will show your entered fields and after this using jquery

$("youridofdiv").click(function()
{
window.location='your path';
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900