Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > ClintID does not work for datagrid template columns controls

Reply
Thread Tools

ClintID does not work for datagrid template columns controls

 
 
Tudor
Guest
Posts: n/a
 
      07-14-2003
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
 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      07-15-2003
I answered this on another group:

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" <(E-Mail Removed)> kirjoitti viestissä
news:(E-Mail Removed) om...
> 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



 
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
Function pointer as template argument - does not work for function template avasilev C++ 4 12-22-2011 02:11 PM
template columns and bound columns in gridview Vinki ASP .Net Web Controls 2 04-25-2007 04:22 PM
How to add a Dropdown list to a datagrid at runtime (dynamic) without using template columns in ASP.NET and still have the ability to us the datagrid Update event. Daniel Roth ASP .Net Datagrid Control 0 04-05-2005 03:58 AM
Columns and Inherited Datagrid...Active Schema does not support columns rob thomson ASP .Net Datagrid Control 0 09-04-2003 03:09 PM
ClintID does not work for datagrid template columns controls Tudor ASP .Net Web Controls 1 07-15-2003 11:35 AM



Advertisments