473,494 Members | 2,223 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dynamic Datagrid UserControl Event Handling

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.DataGridCommandEventHand ler(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.DataGridCommandEventHand ler(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="+ite m+"&edit=true&page="+theRequest);
}
}
Nov 18 '05 #1
0 882

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1382
by: Daniel Armstrong | last post by:
Hello, I have a usercontrol that I have built that has a single generic datagrid. When setting up the inital properties I build the template columns for the datagrid dynamically so it can...
1
7535
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
0
1971
by: Colin Ramsay | last post by:
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...
2
3331
by: John Ninan | last post by:
I am creating Dynamic Usercontrol in Asp.net application. In this application I have a combobox(aspx Page). Which contains various items. Based on item selected I am dynamically populating...
3
3944
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
4
2198
by: Luca | last post by:
HI, I've a asp.net application that add a dynamic usercontrol with a button. If i've tried to click it Click Event don't load. But into a static usercontrol it's happens. Anyone can help me?
0
2256
by: jonelling | last post by:
I am having a problem where the page load event is not being fired for certain user controls that I load dynamically in placeholders. Here is what I'm doing in brief, with full test code supplied...
4
4135
by: EvelynAnd Ethan | last post by:
Hi, ItemCommand event not firing from a dynamic user control ,WHERE A DATAGRID HAS BUTTON,when i click on the linkbutton first time the itemcommand event doesnt fire,second time event fires up ...
2
3813
by: Developer_Software | last post by:
Thanks in advance to anyone who can help :) I've got a placeholder control WITHIN A USER CONTROL that has its contents dynamically added and removed at runtime by a regular .aspx page. At...
3
2096
by: s.bussing | last post by:
Hi, I have been reading for hours now, but I'm still not able to solve my problem, though a lot of people are struggling with something similar. On my page I have a nested repeater. In the inner...
0
6989
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6873
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7367
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4889
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
644
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
285
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.