R
Richard Brown
Ok, I have create a datagrid in design time, but add the columns dynamically
at runtime.
There are two end columns each with buttons, the ItemCommand handler is set
to my procedure 'ProcessCommand'
All data and columns are displayed in the grid, however, when I click on one
of the buttons, the page refreshes and is blank!
I've placed breakpoints in the Process Command code, however, they are never
hit! The event does not seem to be called.
I hace also tried with the SortCommand handler, same thing, never called.
Previously, I had set up the grid bound to a dataset and added the columns
at design time, everything appears to work. But now that the columns must
be bound at runtime, none of the events seem to fire.
public class accredit : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid grdAccredit;
protected SqlConnection sqlConnection1;
protected System.Data.DataSet dsAccreditation;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
SetupGrid();
PullData();
DataBind();
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.dsAccreditation = new System.Data.DataSet();
((System.ComponentModel.ISupportInitialize)(this.dsAccreditation)).BeginInit
();
this.grdAccredit.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.ProcessCommand);
this.grdAccredit.SortCommand += new
System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.SortGrid);
this.sqlConnection1.ConnectionString = "...";
this.dsAccreditation.DataSetName = "Accreditation";
this.dsAccreditation.Locale = new
System.Globalization.CultureInfo("en-US");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dsAccreditation)).EndInit()
;
}
private void ProcessCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string id;
id = e.Item.Cells[0].Text;
switch (e.CommandName)
{
case "Edit":
PullData();
grdAccredit.DataBind();
break;
case "Delete":
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlConnection1;
cmd.CommandText = "deleteAccreditation";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@AccreditationId", id);
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
PullData();
grdAccredit.DataBind();
break;
}
}
private void PullData()
{
SqlDataAdapter dataAdapter = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlConnection1;
cmd.CommandText = "getAccreditationList";
cmd.CommandType = CommandType.StoredProcedure;
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dsAccreditation, "Accreditation");
}
private void SetupGrid()
{
BoundColumn dataCol;
ButtonColumn btnCol;
dataCol = new BoundColumn();
dataCol.DataField = "AccreditationId";
dataCol.Visible = false;
grdAccredit.Columns.Add(dataCol);
dataCol = new BoundColumn();
dataCol.DataField = "Code";
dataCol.HeaderText = "Code";
dataCol.SortExpression = "Code";
grdAccredit.Columns.Add(dataCol);
dataCol = new BoundColumn();
dataCol.DataField = "Name";
dataCol.HeaderText = "Name";
dataCol.SortExpression = "Name";
grdAccredit.Columns.Add(dataCol);
btnCol = new ButtonColumn();
btnCol.ButtonType = ButtonColumnType.LinkButton;
btnCol.CommandName = "Edit";
btnCol.Text = "Edit";
grdAccredit.Columns.Add(btnCol);
btnCol = new ButtonColumn();
btnCol.ButtonType = ButtonColumnType.LinkButton;
btnCol.CommandName = "Delete";
btnCol.Text = "Delete";
grdAccredit.Columns.Add(btnCol);
grdAccredit.DataSource = dsAccreditation;
grdAccredit.DataMember = "Accreditation";
grdAccredit.DataKeyField = "AccreditationId";
}
private void SortGrid(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
}
at runtime.
There are two end columns each with buttons, the ItemCommand handler is set
to my procedure 'ProcessCommand'
All data and columns are displayed in the grid, however, when I click on one
of the buttons, the page refreshes and is blank!
I've placed breakpoints in the Process Command code, however, they are never
hit! The event does not seem to be called.
I hace also tried with the SortCommand handler, same thing, never called.
Previously, I had set up the grid bound to a dataset and added the columns
at design time, everything appears to work. But now that the columns must
be bound at runtime, none of the events seem to fire.
public class accredit : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid grdAccredit;
protected SqlConnection sqlConnection1;
protected System.Data.DataSet dsAccreditation;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
SetupGrid();
PullData();
DataBind();
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.dsAccreditation = new System.Data.DataSet();
((System.ComponentModel.ISupportInitialize)(this.dsAccreditation)).BeginInit
();
this.grdAccredit.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.ProcessCommand);
this.grdAccredit.SortCommand += new
System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.SortGrid);
this.sqlConnection1.ConnectionString = "...";
this.dsAccreditation.DataSetName = "Accreditation";
this.dsAccreditation.Locale = new
System.Globalization.CultureInfo("en-US");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dsAccreditation)).EndInit()
;
}
private void ProcessCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string id;
id = e.Item.Cells[0].Text;
switch (e.CommandName)
{
case "Edit":
PullData();
grdAccredit.DataBind();
break;
case "Delete":
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlConnection1;
cmd.CommandText = "deleteAccreditation";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@AccreditationId", id);
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
PullData();
grdAccredit.DataBind();
break;
}
}
private void PullData()
{
SqlDataAdapter dataAdapter = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
cmd.Connection = sqlConnection1;
cmd.CommandText = "getAccreditationList";
cmd.CommandType = CommandType.StoredProcedure;
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dsAccreditation, "Accreditation");
}
private void SetupGrid()
{
BoundColumn dataCol;
ButtonColumn btnCol;
dataCol = new BoundColumn();
dataCol.DataField = "AccreditationId";
dataCol.Visible = false;
grdAccredit.Columns.Add(dataCol);
dataCol = new BoundColumn();
dataCol.DataField = "Code";
dataCol.HeaderText = "Code";
dataCol.SortExpression = "Code";
grdAccredit.Columns.Add(dataCol);
dataCol = new BoundColumn();
dataCol.DataField = "Name";
dataCol.HeaderText = "Name";
dataCol.SortExpression = "Name";
grdAccredit.Columns.Add(dataCol);
btnCol = new ButtonColumn();
btnCol.ButtonType = ButtonColumnType.LinkButton;
btnCol.CommandName = "Edit";
btnCol.Text = "Edit";
grdAccredit.Columns.Add(btnCol);
btnCol = new ButtonColumn();
btnCol.ButtonType = ButtonColumnType.LinkButton;
btnCol.CommandName = "Delete";
btnCol.Text = "Delete";
grdAccredit.Columns.Add(btnCol);
grdAccredit.DataSource = dsAccreditation;
grdAccredit.DataMember = "Accreditation";
grdAccredit.DataKeyField = "AccreditationId";
}
private void SortGrid(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
}