ItemCommand not firing?

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)
{


}
 
R

Richard Brown

Ok, I figured it out -- just in case any other *newbie* like myself runs
across this problem.

I had to put in the grid setup code into the Page Init function, after the
InitializeControls function.
Evidently, if the columns don't exist by the time the base.Init(e) function
is called, then the event doesn't have the column to bind to or something.
So it was never being called.

Everything is working fine now that I figured out where to setup the grid.
 
S

Stamen Gortchev

Hallo Richard,

can you please post the part of your code where you did it. (The init_event)
I am very curios how you did it.

Thanks
Stamen
 
R

Richard Brown

Hello Stamen,

It was very easy, from the code posted in the original message, just take
out the "SetupGrid();" line where it was originally and then change the
OnInit function to be:

override protected void OnInit(EventArgs e)
{
InitializeComponent();
SetupGrid();
base.OnInit(e);
}

I could have put SetupGrid inside the InitializeComponents function, but
I've been told to leave that function alone since the designer uses it and
rewrites it. The SetupGrid function just adds the columns to the grid --
the code that was originally posted adds static columns, which can just have
been done in the designer, except I didn't want to add a DataSet,
DataAdapter, etc. The PullData function just uses the dataset, and since
then, I have modified it to use a DataTable and DataView to implement the
sorting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top