Slide 7.16: Embedding SQL in ASP.NET (cont.)
Slide 8.1: Microsoft C#
Home

Embedding SQL in ASP.NET (Cont.)


Below shows the C# code of this application. C# includes the event-driven feature, so one of the following functions is triggered when an event is detected.

C:\ASP.NET-workspace\WebSite2\WebSite2\Default.aspx.cs
using System;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page {

 protected void DropDownList1_SelectedIndexChanged( object sender, EventArgs e ) {
  DetailsView1.ChangeMode( DetailsViewMode.ReadOnly );
 }

 protected void GridView1_SelectedIndexChanged( object sender, EventArgs e ) {
  DetailsView1.ChangeMode( DetailsViewMode.ReadOnly );
 }

 protected void GridView1_RowDeleted( object sender, GridViewDeletedEventArgs e ) {
  if ( e.Exception != null ) {
   ErrorMessageLabel.Text  = "Failed to DELETE due to foreign key contstraint. ";
   ErrorMessageLabel.Text += "You may only delete rows which have no related records.";
   e.ExceptionHandled = true;
  }
 }

 protected void GridView1_Sorted( object sender, EventArgs e ) {
  DetailsView1.ChangeMode( DetailsViewMode.ReadOnly );
 }

 protected void GridView1_PageIndexChanged( object sender, EventArgs e ) {
  DetailsView1.ChangeMode( DetailsViewMode.ReadOnly );
 }

 protected void DetailsView1_ItemUpdated( object sender, DetailsViewUpdatedEventArgs e ) {
  GridView1.DataBind( );
 }

 protected void DetailsView1_ItemInserted( object sender, DetailsViewInsertedEventArgs e ) {
  if ( e.Exception != null ) {
   ErrorMessageLabel.Text  = "An error occured while entering this record. ";
   ErrorMessageLabel.Text += "Please verify you have entered data in the correct format.";
   e.ExceptionHandled=true;
  }
  GridView1.DataBind( );
 }

 protected void DetailsView1_DataBound( object sender, EventArgs e ) {
  if ( DetailsView1.CurrentMode == DetailsViewMode.Insert ) {
   TextBox stateTextBox = new TextBox( );
   stateTextBox.ID = DetailsView1.Rows[6].Cells[1].ID;
   stateTextBox.Text = DropDownList1.SelectedValue;
   stateTextBox.Enabled = false;
  }
 }
}