ClintID does not work for datagrid template columns controls

Discussion in 'ASP .Net Datagrid Control' started by Tudor, Jul 15, 2003.

  1. Tudor

    Tudor Guest

    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
     
    Tudor, Jul 15, 2003
    #1
    1. Advertising

  2. Tudor

    Teemu Keiski Guest

    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" <> kirjoitti viestissä
    news:...
    > 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
     
    Teemu Keiski, Jul 15, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    941
  2. rob thomson
    Replies:
    0
    Views:
    264
    rob thomson
    Sep 4, 2003
  3. Tudor
    Replies:
    1
    Views:
    126
    Teemu Keiski
    Jul 15, 2003
  4. Vinki

    template columns and bound columns in gridview

    Vinki, Apr 25, 2007, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    874
    Vinki
    Apr 25, 2007
  5. avasilev
    Replies:
    4
    Views:
    579
    avasilev
    Dec 22, 2011
Loading...

Share This Page