Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Events not firing in datagrid with programmatically created columns

Reply
Thread Tools

Events not firing in datagrid with programmatically created columns

 
 
J.ShepherdII@gmail.com
Guest
Posts: n/a
 
      04-03-2006
I've run into a rather strange bug with a datagrid I've been working
on. For some reason, when I programmatically create columns within a
datagrid, the events attached to these columns simply refuse to fire.
Trying to generate a postback by clicking one of the column headers in
the programmatically added columns just results in the programmatically
generated columns disappearing on postback. This application is on the
1.1 framework, on a Windows Server 2003 machine.

Here is the relavent code:


ASP

<aspataGrid id="dg_bulls" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn><ItemTemplate> <asp:CheckBox
id="CheckBox1" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>

</aspataGrid>

C#

//Databinding method for the grid
private void dg_bulls_databind(DataView dvData, ArrayList baseCols){
foreach(string str in baseCols)
{

//Create the new datacolumn and set it's style attributes
BoundColumn bc_NewColumn = new BoundColumn();
bc_NewColumn.DataField = str;
bc_NewColumn.HeaderText = str;
bc_NewColumn.ItemStyle.CssClass = "datagrid-item";
bc_NewColumn.HeaderStyle.CssClass = "datagrid-header";
bc_NewColumn.HeaderStyle.ForeColor =System.Drawing.Color.White;




//Check the sort attribute on the dataview
if(dvData.Sort.StartsWith("["+str+"]"))
{
//Put in the opposite of the current ASC/DESC expression if the
view is sorting on
//the current column, for reversible ASC/DESC sorting.
if(dvData.Sort.EndsWith("ASC"))
{
bc_NewColumn.SortExpression = dvData.Sort.Replace("ASC","DESC");
}
else
{
bc_NewColumn.SortExpression = dvData.Sort.Replace("DESC","ASC");
}
}
else
{
//no sort expression found, default to sorting by the current
column DESC
bc_NewColumn.SortExpression = "["+str+"] DESC";
}

//Add column to the grid
this.dg_bulls.Columns.Add(bc_NewColumn);
}


this.dg_bulls.DataSource = dvData;
this.dg_bulls.DataBind();
}

//InitializeComponent, just to prove that the handlers do exist and are
tied to the grid
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.dg_bulls.ItemCommand +=new
DataGridCommandEventHandler(dg_bulls_ItemCommand);
this.dg_bulls.SortCommand +=new
DataGridSortCommandEventHandler(dg_bulls_sort);
this.dg_bulls.SelectedIndexChanged += new
System.EventHandler(this.dg_bulls_SelectedIndexCha nged);


}

The C# codebehind works by parsing through a datalist full of
checkboxes elsewhere on the page, and generating a list of columns the
user wishes to see added to the datagrid, programmatically adding the
columns to the grid, and then binding from a dataview that is either
generated from a database or pulled out of session, depending on
circumstances.

I've seen threads about this on this group, as well as various blog
posts, etc. I have tried all the fixes they've suggested, but haven't
had any luck. Has anyone come up with a workable fix for this, or do I
need to redesign this grid?

 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      04-17-2006
You would need to recreate the columns on postback also.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke


<> wrote in message
news: oups.com...
> I've run into a rather strange bug with a datagrid I've been working
> on. For some reason, when I programmatically create columns within a
> datagrid, the events attached to these columns simply refuse to fire.
> Trying to generate a postback by clicking one of the column headers in
> the programmatically added columns just results in the programmatically
> generated columns disappearing on postback. This application is on the
> 1.1 framework, on a Windows Server 2003 machine.
>
> Here is the relavent code:
>
>
> ASP
>
> <aspataGrid id="dg_bulls" runat="server" AllowPaging="True"
> AllowSorting="True" AutoGenerateColumns="False">
> <Columns>
> <asp:TemplateColumn><ItemTemplate> <asp:CheckBox
> id="CheckBox1" runat="server"></asp:CheckBox>
> </ItemTemplate>
> </asp:TemplateColumn>
> </Columns>
>
> </aspataGrid>
>
> C#
>
> //Databinding method for the grid
> private void dg_bulls_databind(DataView dvData, ArrayList baseCols){
> foreach(string str in baseCols)
> {
>
> //Create the new datacolumn and set it's style attributes
> BoundColumn bc_NewColumn = new BoundColumn();
> bc_NewColumn.DataField = str;
> bc_NewColumn.HeaderText = str;
> bc_NewColumn.ItemStyle.CssClass = "datagrid-item";
> bc_NewColumn.HeaderStyle.CssClass = "datagrid-header";
> bc_NewColumn.HeaderStyle.ForeColor =System.Drawing.Color.White;
>
>
>
>
> //Check the sort attribute on the dataview
> if(dvData.Sort.StartsWith("["+str+"]"))
> {
> //Put in the opposite of the current ASC/DESC expression if the
> view is sorting on
> //the current column, for reversible ASC/DESC sorting.
> if(dvData.Sort.EndsWith("ASC"))
> {
> bc_NewColumn.SortExpression = dvData.Sort.Replace("ASC","DESC");
> }
> else
> {
> bc_NewColumn.SortExpression = dvData.Sort.Replace("DESC","ASC");
> }
> }
> else
> {
> //no sort expression found, default to sorting by the current
> column DESC
> bc_NewColumn.SortExpression = "["+str+"] DESC";
> }
>
> //Add column to the grid
> this.dg_bulls.Columns.Add(bc_NewColumn);
> }
>
>
> this.dg_bulls.DataSource = dvData;
> this.dg_bulls.DataBind();
> }
>
> //InitializeComponent, just to prove that the handlers do exist and are
> tied to the grid
> private void InitializeComponent()
> {
> this.Load += new System.EventHandler(this.Page_Load);
> this.dg_bulls.ItemCommand +=new
> DataGridCommandEventHandler(dg_bulls_ItemCommand);
> this.dg_bulls.SortCommand +=new
> DataGridSortCommandEventHandler(dg_bulls_sort);
> this.dg_bulls.SelectedIndexChanged += new
> System.EventHandler(this.dg_bulls_SelectedIndexCha nged);
>
>
> }
>
> The C# codebehind works by parsing through a datalist full of
> checkboxes elsewhere on the page, and generating a list of columns the
> user wishes to see added to the datagrid, programmatically adding the
> columns to the grid, and then binding from a dataview that is either
> generated from a database or pulled out of session, depending on
> circumstances.
>
> I've seen threads about this on this group, as well as various blog
> posts, etc. I have tried all the fixes they've suggested, but haven't
> had any luck. Has anyone come up with a workable fix for this, or do I
> need to redesign this grid?
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid dynamic columns / firing events Abelardo Vacca ASP .Net Web Controls 1 01-12-2006 05:10 PM
Dynamically created webcontrol's events not firing Mark Rae ASP .Net 3 09-24-2005 02:16 PM
Dynamically created DropDownList not firing events or persisting viewstate bryanp10@hotmail.com ASP .Net 5 03-11-2005 10:41 PM
Programmatically Created DataGrid Template Columns Nicole ASP .Net 2 11-05-2003 12:52 AM
Programmatically creating a datagrid control - Events NOT Firing!!!! Please Help KJ ASP .Net Datagrid Control 0 08-11-2003 05:20 PM



Advertisments