Dynamic Datagrid UserControl Event Handling

Discussion in 'ASP .Net' started by Colin Ramsay, Apr 17, 2004.

  1. Colin Ramsay

    Colin Ramsay Guest

    Hi all,

    I don't normally post swathes of code like this but I am truly banging
    my head off my desk here...


    I've dynamically created a datagrid within a usercontrol. There are two
    columns which contain buttons to Edit & Delete rows. For some reason,
    the ItemCommand event which these should be connected to isn't firing.

    I know this is a lot of code, but I've been through a hell of a lot of
    forums and tutorials on this matter and just can't track down the
    problem. There is either something tiny I have missed or something
    fundemental I'm doing wrong. I would really really appreciate it if
    someone could look over this for me.


    Here's the .ascx:

    <%@ Control Language="C#" src="../cs/resultsgrid.cs"
    Inherits="resultsgrid" %>
    <asp:placeholder runat="server" id="gridholder"/>



    Here's the .cs behind it:

    using System;
    using System.Data;
    using System.Data.OleDb;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    public class resultsgrid: UserControl {

    protected string theRequest;
    protected string cs;
    protected OleDbConnection conn;
    protected OleDbConnection oleConnection;
    protected DataGrid grid = new DataGrid();

    protected void g_ItemCreated (Object s, DataGridItemEventArgs e)
    {
    this.grid.ItemCommand += new

    System.Web.UI.WebControls.DataGridCommandEventHandler(this.ItemsGrid_Command);

    }

    override protected void OnInit(EventArgs e)
    {
    this.grid.ItemCreated += new
    DataGridItemEventHandler(this.g_ItemCreated);
    makeGrid();
    }

    protected void makeGrid()
    {
    theRequest = Request.QueryString["page"];
    cs = (string)Application["dbConnectionString"];

    string sql;
    OleDbDataReader dtrAdmin;
    OleDbConnection conn = new OleDbConnection( cs );

    if (theRequest != null)
    {
    HtmlForm myForm = new HtmlForm();
    this.FindControl("gridholder").Controls.Add(myForm);
    this.FindControl("gridholder").Controls[0].Controls.Add(grid);

    grid.AutoGenerateColumns = false;

    grid.ShowHeader = false;
    grid.BorderColor = System.Drawing.Color.FromName("White");
    grid.AlternatingItemStyle.BackColor =
    System.Drawing.Color.FromName("White");


    oleConnection = new OleDbConnection(cs);

    /////// LOOK UP details of this table in the admin table ///////
    sql = "SELECT * FROM admin WHERE name='"+theRequest+"'";

    OleDbCommand oleCommand = new OleDbCommand(sql,oleConnection);

    oleConnection.Open();
    dtrAdmin = oleCommand.ExecuteReader();
    dtrAdmin.Read();
    string select = dtrAdmin["select"].ToString();
    string display = (string)dtrAdmin["display"];
    oleConnection.Close();

    //////// SPLIT the select and display strings into arrays ////////
    string delimStr = ",";
    char [] delimiter = delimStr.ToCharArray();

    string[] selectArray = select.Split(delimiter);
    string[] displayArray = display.Split(delimiter);

    // add the id column no matter what
    BoundColumn colm = new BoundColumn();
    colm.DataField = "id";
    grid.Columns.Add(colm);

    //////// ADD CONTROLS - boundcolumn controls for each field to display
    foreach (string str in displayArray) {
    // don't add the ID column as we just did that.
    if (str != "id")
    {
    BoundColumn col = new BoundColumn();
    col.DataField = str;
    grid.Columns.Add(col);
    }
    }

    //////// ADD CONTROLS - button columns - NOTE - BEFORE DATABIND
    ButtonColumn edit = new ButtonColumn();
    edit.ButtonType = ButtonColumnType.LinkButton;
    edit.CommandName = "Edit";
    edit.Text = "Edit";
    grid.Columns.Add(edit);

    ButtonColumn delete = new ButtonColumn();
    delete.ButtonType = ButtonColumnType.LinkButton;
    delete.CommandName = "Delete";
    delete.Text = "Delete";
    grid.Columns.Add(delete);

    grid.ItemCommand += new
    DataGridCommandEventHandler(this.ItemsGrid_Command);

    if (!IsPostBack)
    {
    bindGrid();
    }
    this.grid.ItemCommand += new

    System.Web.UI.WebControls.DataGridCommandEventHandler(this.ItemsGrid_Command);

    }
    }


    void bindGrid() {
    //////// GET DATA - actually obtain the data for the DataGrid
    string sql = "SELECT * FROM "+theRequest+" ORDER BY id DESC";

    OleDbDataReader dtrQuery;
    OleDbCommand oleCommand = new OleDbCommand(sql,oleConnection);

    oleConnection.Open();
    dtrQuery = oleCommand.ExecuteReader();
    grid.DataSource = dtrQuery;
    grid.DataBind();
    oleConnection.Close();

    }

    void ItemsGrid_Command (Object s, DataGridCommandEventArgs e)
    {
    Response.Write("test");

    string cmd = ((LinkButton)e.CommandSource).CommandName;

    if (cmd == "Edit")
    {
    editItem(e);
    }
    else if (cmd == "Delete")
    {
    deleteItem(e);
    }
    }

    protected void deleteItem (DataGridCommandEventArgs e)
    {
    // grab the text in the first cell of the row which was clicked
    TableCell itemCell = e.Item.Cells[0];
    string item = itemCell.Text;

    // the query string will contain the name of the current page we are
    editing
    string theRequest = Request.QueryString["page"];

    string sql = "DELETE FROM "+theRequest+" WHERE name='"+item+"'";

    OleDbConnection oleConnection = new OleDbConnection(cs);
    OleDbCommand oleCommand = new OleDbCommand(sql,oleConnection);

    oleConnection.Open();
    oleCommand.ExecuteNonQuery();
    oleConnection.Close();

    bindGrid();
    }

    protected void editItem (DataGridCommandEventArgs e)
    {
    // put "page" from the query string into a nice var
    theRequest = Request.QueryString["page"];

    TableCell itemCell = e.Item.Cells[0];
    string item = itemCell.Text;


    Response.Redirect("default.aspx?mode=edit&id="+item+"&edit=true&page="+theRequest);
    }
    }
    Colin Ramsay, Apr 17, 2004
    #1
    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. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    181
  2. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    187
    vatech1993
    Dec 11, 2004
  3. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    153
  4. John Hopper

    handling usercontrol event in container page in .net 2.0

    John Hopper, May 30, 2008, in forum: ASP .Net Web Controls
    Replies:
    6
    Views:
    174
    John Hopper
    Jun 2, 2008
  5. mpaine

    Declarative event handling from usercontrol

    mpaine, Sep 30, 2009, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    801
    mpaine
    Sep 30, 2009
Loading...

Share This Page