protected void Submit_Click(object sender, EventArgs e) { if (IsValid) // IsValid is a property on the page that checks with the validation controls to ensure that the data in the controls passes all the validation. { if (IsDuplicate()) { MessageLabel.Text = "That account number has already been taken"; } else { MessageLabel.Text = "Your new bank account will be processed soon."; BankAccount data = new BankAccount // The following is an "initializer list" { AccountHolder = AccountHolder.Text, AccountNumber = long.Parse(AccountNumber.Text), OpeningBalance = decimal.Parse(OpeningBalance.Text), AccountType = AccountType.Text }; BankAccounts.Add(data); BankAccountGridView.DataSource = BankAccounts; BankAccountGridView.DataBind(); } } // else.... // The ValidationSummary will automatically display its contents should the IsValid check return False. }
protected void Page_Load(Object sender, EventArgs e) { // Page_Load happens before any of the individual control events. // It's great popint in time of the page's Page Lifecycle to go // ahead and "popluate" the form's contorls on the first visit to the page if (!IsPostBack) // a GET request - the "first" visit to the page { // Populate GridView BankAccountGridView.DataSource = BankAccounts; BankAccountGridView.DataBind(); } }