ItemCommand not firing?

Discussion in 'ASP .Net Datagrid Control' started by Richard Brown, Sep 24, 2003.

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


    }
     
    Richard Brown, Sep 24, 2003
    #1
    1. Advertising

  2. 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.
     
    Richard Brown, Sep 26, 2003
    #2
    1. Advertising

  3. 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

    "Richard Brown" <> schrieb im Newsbeitrag
    news:...
    > 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.
    >
    >
     
    Stamen Gortchev, Sep 26, 2003
    #3
  4. 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.



    "Stamen Gortchev" <> wrote in message
    news:%...
    > 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
    >
    > "Richard Brown" <> schrieb im Newsbeitrag
    > news:...
    > > 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.
    > >
    > >

    >
    >
     
    Richard Brown, Sep 26, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael

    Repeater ItemCommand not firing

    Michael, Nov 15, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,155
    Michael
    Nov 15, 2004
  2. =?Utf-8?B?RGVlcGVzaA==?=

    Datagrid ItemCommand Event Not Firing

    =?Utf-8?B?RGVlcGVzaA==?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    5,819
    =?Utf-8?B?RGVlcGVzaA==?=
    Oct 6, 2005
  3. =?Utf-8?B?Q3VydF9DIFtNVlBd?=

    repeater ItemCommand not firing on first click

    =?Utf-8?B?Q3VydF9DIFtNVlBd?=, Oct 12, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,781
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Oct 12, 2005
  4. EvelynAnd Ethan
    Replies:
    4
    Views:
    978
    =?Utf-8?B?Sko=?=
    Jan 9, 2006
  5. =?Utf-8?B?ZGFuYw==?=

    DataGrid ItemCommand event not firing if not first page

    =?Utf-8?B?ZGFuYw==?=, Oct 26, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    4,153
    =?Utf-8?B?ZGFuYw==?=
    Oct 26, 2005
Loading...

Share This Page