Sophisticated Dynamic DataGrid Not Firing Event ! Desperate !

P

poi

Help!


I made a DataGrid in code behind only. There is no reference to it in
the ASPNET page template.
With a search button on the web form, the click gets a dataset and
assigns it to the page dataset property. The datagrid is created and
bound and I can click on any row. But on postback, the grid disappears
and I am not able to tell what row was actually clicked.

How do I trap what row was clicked by the user?

Thanks.

Page properties added:

public DataSet _infoSet;
public DataGrid _tempGrid = new DataGrid();

In the OnInit:

this.tempGrid = new DataGrid();
this.tempGrid.EnableViewState = true;
this.tempGrid.ID = "tempGrid";
this.tempGrid.ItemCreated +=new DataGridItemEventHandler(
tempGrid_ItemCreated );
this.tempGrid.ItemCommand += new DataGridCommandEventHandler(
tempGrid_ItemCommand );
this.tempGrid.SelectedIndexChanged += new EventHandler(
tempGrid_SelectedIndexChanged );
this.tempGrid.AllowPaging = false;
this.tempGrid.PageSize = 30;
this.tempGrid.Width = System.Web.UI.WebControls.Unit.Percentage(100);
this.tempGrid.GridLines = GridLines.None;
this.tempGrid.Font.Name = "Tahoma";
this.tempGrid.Font.Size = FontUnit.Point(9);
System.Web.UI.WebControls.ButtonColumn actionCol = new ButtonColumn();
actionCol.ButtonType = ButtonColumnType.LinkButton;
actionCol.HeaderText = "Action";
actionCol.ItemStyle.Width = 40;
actionCol.ItemStyle.Wrap = false;
actionCol.Text = "Get";
actionCol.CommandName = "SelectThis";
actionCol.Visible = false;
this.tempGrid.Columns.Add( actionCol );


This shows the JavaScript alert box:
public virtual void infoGrid_ItemCreated( object sender ,
DataGridItemEventArgs dgEventArgs)
{
if (
dgEventArgs.Item.ItemType == ListItemType.Item ||
dgEventArgs.Item.ItemType == ListItemType.AlternatingItem ||
dgEventArgs.Item.ItemType == ListItemType.SelectedItem
)
{
dgEventArgs.Item.Attributes.Add( "onmouseover" ,
"this.style.backgroundColor='#00FF99';this.style.cursor='hand'" );
dgEventArgs.Item.Attributes.Add( "onmouseout" ,
"this.style.backgroundColor='#FFFFEA';" );
string onClickString = "javascript:alert('" +
dgEventArgs.Item.ItemIndex.ToString() + "')";
dgEventArgs.Item.Attributes.Add( "onclick" , onClickString );
int ty=4;
}
}

But if I change the onClickString to

string onClickString = "javascript:__doPostBack('tempGrid$_" + "ctl" +
dgEventArgs.Item.ItemIndex.ToString() + "$_ctl0','')";

the tempGrid_SelectedIndexChanged never fires.

Help!

TIA
 
T

Toby Mills

I had a similar problem. Creating datagrids dynamically and adding
them into a placeholder control. With each postback they would
disappear. So what I did was create a session variable of the same
name and stick a reference to the grid in there, and then on page
load, retrieve the grid from the session variable and re-add it into
the placeholder control.

It works for me, hopefully it can sort you out!

I now have a different problem however! Try using paging with your
grid that you created programatically! Not easy! Especially as I have
a variable number of grids and each one must use paging. Eventually I
got there, creating prev & next buttons and adding them to the
placeholder control too!

So now paging works beautifully EXCEPT when you try going to the last
page. If there is anything except an exact number of records it raises
an error. e.g. if my last page has 9 records, but my paging size is
10.

If anybody can solve that I'd be really happy!!!!

Cheers,
Toby :)
 
I

ipmccun

I wasted the better part of an entire day trying to figure this one out
and after copious walking through disassembly, I've figured out what
happens. I don't have a firm solution, but...

Essentially, when you post back, it needs to recreate the control
hierarchy. For a template column this happens during the (private)
method TemplateColumn.InitializeCell(...) In other words, this is when
the framework calls ITemplate.InstantiateIn(Control).

Normally, on post back, this call happens as a result of
Control.LoadViewState() being called on the DataGrid, which in turn
creates the child control hierarchy by calling EnsureChildControls when
the Controls collection is accessed for the first time. The problem
here is that if your TemplateColumns haven't been added to the Columns
collection before this point, you're simply not going to be able to
participate in the recreation process.

Unfortunately, you cant force it to recreate its child control
hierarchy at Load time because it marks that its already done this in a
private member.

At this point the answer seems to be "if you cant get your columns
added to the Columns collection during the Init handler, you're SOL."

The solution is probably some combination of inheriting the DataGrid
control and overriding methods in such a way that either a) there is an
event called just before it creates the child control hierarchy where
you can add your columns or b) add a way to, at Load time, force it to
recreate the child control hierarchy from scratch. b) feels like the
way to go, but I'm not sure if the "ProcessPostData Second Try" stage
will have access to postback information that was already processed by
the subsequently purged-and-recreated controls.

It kind of irks me that the documentation gives examples which will
fail to work in the naturally expected way... grr... If anyone has come
up with a solution to this problem, by all means let me know. In the
mean time I've resorted to using Request.Params to pull out the entered
data and use it that way. Definitely an ugly hack.

Ian
 

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,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top