ClintID does not work for datagrid template columns controls

T

Tudor

Hello

I have a datagrid "dg" which has two template columns. The first
template column contains a button denoted "b" and the second template
column contains a div server control denoted "div" which in turn
contains another grid denoted "dg_second".

"dg_second" is bound to "dg" item data. Of course, for each item from
"dg" it is generated a button "b" and div element "div" which contains
a "dg_second". What i want is to attach a javascript code to "b" in
order to alternatively hide or show the corresponding element "div"
from the same line(second column).

Theoretically, it is quite simple to do that: i treat the itemCreated
event of "dg" which is fired for every item creation. Inside this
event, i identify for each item the "b" and "div" elements and i
associate to the "b" element a javascript function having as argument
the ClientID value of the "div" element. The javascript function will
simply hide or show the div element who's id receives in the
parameter.

The problem is that in this function, the property ClientID of "b"
and "div" elements DOES NOT return the correct values that are
rendered into html code. More exactly ClientID always returns "b" and
"div" instead of some complicated unique strings.

Here it is the implementation of my itemCreated event

private void itemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType==ListItemType.Item || e.Item.ItemType==
ListItemType.AlternatingItem )
{


Button b=(Button)e.Item.FindControl("b");
HtmlGenericControl div= (HtmlGenericControl)e.Item.FindControl("div");
String divClientID=div.ClientID;
String js="javascript:setNewContent('" + divClientID+"');return
false;";
b.Attributes["onclick"]=js;

}
}

My purpose is to implement a hierarchical grid display.

I would greately appreciate any qualified help for this matter.

Tudor
 
T

Teemu Keiski

It is because basically DataGrid's control hierarchy isn't "clear" yet, that
is this item is just created and it hasn't been added to DataGrid's controls
collection and therefore they have not been assigned full IDs yet.

You could overcome the problem by attaching a handler for DataGrid's
PreRender event and set the ID's there. My code is VB as I had VB IDE open
by the time I read your question, hope it doesn't mind).

Private Sub DataGrid1_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DataGrid1.PreRender
For Each dgi As DataGridItem In DataGrid1.Items
Dim b As Button = CType(dgi.FindControl("b"), Button)
Dim div As HtmlGenericControl = CType(dgi.FindControl("div"),
HtmlGenericControl)
Dim divClientID As String = div.ClientID
Dim js As String = "javascript:setNewContent('" + divClientID +
"');return false;"
b.Attributes("onclick") = js
Next
End Sub

And BTW it should work on ItemDataBound as well as on that phase item is
added to the DataGrids controls.
--
Teemu Keiski
MCP,Designer/Developer
Mansoft tietotekniikka Oy
http://www.mansoft.fi

AspInsiders Member, www.aspinsiders.com
ASP.NET Forums Moderator, www.asp.net
AspAlliance Columnist, www.aspalliance.com


Tudor said:
Hello

I have a datagrid "dg" which has two template columns. The first
template column contains a button denoted "b" and the second template
column contains a div server control denoted "div" which in turn
contains another grid denoted "dg_second".

"dg_second" is bound to "dg" item data. Of course, for each item from
"dg" it is generated a button "b" and div element "div" which contains
a "dg_second". What i want is to attach a javascript code to "b" in
order to alternatively hide or show the corresponding element "div"
from the same line(second column).

Theoretically, it is quite simple to do that: i treat the itemCreated
event of "dg" which is fired for every item creation. Inside this
event, i identify for each item the "b" and "div" elements and i
associate to the "b" element a javascript function having as argument
the ClientID value of the "div" element. The javascript function will
simply hide or show the div element who's id receives in the
parameter.

The problem is that in this function, the property ClientID of "b"
and "div" elements DOES NOT return the correct values that are
rendered into html code. More exactly ClientID always returns "b" and
"div" instead of some complicated unique strings.

Here it is the implementation of my itemCreated event

private void itemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType==ListItemType.Item || e.Item.ItemType==
ListItemType.AlternatingItem )
{


Button b=(Button)e.Item.FindControl("b");
HtmlGenericControl div= (HtmlGenericControl)e.Item.FindControl("div");
String divClientID=div.ClientID;
String js="javascript:setNewContent('" + divClientID+"');return
false;";
b.Attributes["onclick"]=js;

}
}

My purpose is to implement a hierarchical grid display.

I would greately appreciate any qualified help for this matter.

Tudor
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top