DataGrid EditItem ClientID

Discussion in 'ASP .Net Datagrid Control' started by Dave56, Dec 1, 2003.

  1. Dave56

    Dave56 Guest

    I have a column in a DataGrid with the following EditItemTemplate:

    <EditItemTemplate>
    <TextArea runat="server" Class="NormalText" wrap="soft"
    id="TxtDescription" rows="3" cols="85"
    style="FLOAT: left; POSITION: static">
    <%# DataBinder.Eval(Container, "DataItem.Description") %></TextArea>
    </EditItemTemplate>

    In the DataGrid_ItemCreated subroutine
    e.Item.FindControl("TxtDescription").ClientID returns "TxtDescription".

    However, when I run the page and view source I see the following:

    <td class="ResultsDim" valign="Top">
    <textarea name="DGItems:_ctl6:TxtDescription"
    id="DGItems__ctl6_TxtDescription" Class="NormalText" wrap="soft" rows="3"
    cols="85" style="FLOAT: left; POSITION: static"></textarea>
    </td>

    This shows a client id of "DGItems__ctl6_TxtDescription".
    - DGItems is the name of the DataGrid
    - TxtDescription is the control
    Where does the ctl6 come from?

    Is there any way in the DataGrid_ItemCreated subroutine that I can get this
    full clientID, or build it?

    Thanks in advance.
     
    Dave56, Dec 1, 2003
    #1
    1. Advertising

  2. Dave56

    MSFT Guest

    Hello

    "ctl6" mean the control is in the sixth row of the datagrid. DataGrid use
    this to distinguish sub control between rows. This ID is generated when the
    control was rendered.

    Luke
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    MSFT, Dec 2, 2003
    #2
    1. Advertising

  3. Dave56

    Dave56 Guest

    In the DataGrid_ItemCreated Event is there any way to get this "row number"?

    Thanks,
    Dave
     
    Dave56, Dec 2, 2003
    #3
  4. Dave56

    MSFT Guest

    Hi Dave,

    We get the row id with e.item.itemindex

    Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    DataGrid1.ItemCreated

    Response.Write(e.Item.ItemIndex)

    End Sub

    Becare that we need to exclude -1 from the values

    Luke
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    MSFT, Dec 3, 2003
    #4
  5. Dave56

    MSFT Guest

    Hello,

    Thank you for the reply. As for how to get the control's complete ClientID
    in the DataGrid's ItemGreated event, the DataGrid or the DataGridItem
    dosn't provide a method to get the full ID directly. However, the DataGrid
    generate the SubControl's ClientID via a standard named rules. So we can
    get the ID follow the rule:

    the DataGrid Control will named the subcontrol in its template as
    DataGrid's ID + "__ctl" + control's index + "_" + control's ID
    For example, if there is a DataGrid control named "dgTest", and it contains
    a TextArea in its ItemTemplate, the TextArea's ID in DesignTime is
    "txtContent", you can get its full clientID via such cole in the
    DataGrid_ItemCreated event:
    private void dgTest_ItemCreated(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)
    {

    HtmlTextArea area = (HtmlTextArea)e.Item.FindControl("txtContent");
    if(area != null)
    {
    string cid = dgTest.ClientID + "__ctl" +
    (e.Item.ItemIndex+2).toString() + "_" + area.ClientID
    }

    }

    To use (e.Item.ItemIndex+2) is because that the real clientID's index
    equals 2+e.Item.ItemIndex in ItemCreated event
    Please try it out to see whether this help you. Thanks.

    Regards,

    Steven Cheng
    Microsoft Developer Support Engineer
     
    MSFT, Dec 3, 2003
    #5
  6. Dave56

    Dave56 Guest

    > To use (e.Item.ItemIndex+2) is because that the real clientID's index
    > equals 2+e.Item.ItemIndex in ItemCreated event



    Thanks, it works if I add 3 instead of 2.

    Dave
     
    Dave56, Dec 4, 2003
    #6
  7. Dave56

    Dave56 Guest

    > > To use (e.Item.ItemIndex+2) is because that the real clientID's index
    > > equals 2+e.Item.ItemIndex in ItemCreated event

    >
    >
    > Thanks, it works if I add 3 instead of 2.


    Correction on some of my DataGrids I have to add 2, and on some I have to
    add 3.
    Does anyone know why?

    The grids where I have to add 3 have AllowPaging="True", could this affect
    it?

    Dave
     
    Dave56, Dec 4, 2003
    #7
  8. Dave56

    MSFT Guest

    Hello,

    Thank you for the reply. As for why the start index of the Items is
    different if you set the DataGrid's AllowPaging as true or false. This is
    because in the DataGrid's ItemGreated event. the DataGrid will create the
    items as a certain sequence. If not AllowPaging, then, the sequence is

    Header
    Header
    Item
    AlternatingItem
    Item
    AlternatingItem
    Item
    AlternatingItem
    .............................
    Item
    AlternatingItem
    Item
    AlternatingItem
    Footer

    You can use such code in the DataGrid's ItemCreated event to print out the
    sequence:
    private void dgTest_ItemCreated(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    Response.Write("<br/>" + e.Item.ItemType.ToString() );
    }

    If you set the AllowPaging as true, then, before create the Items and
    AlternatingItems, the sequece is
    Pager
    Header
    Item
    AlternatingItem
    Item
    AlternatingItem
    Item
    AlternatingItem
    .........................
    Item
    AlternatingItem
    Item
    AlternatingItem
    Footer
    Pager

    There will occur one more Pager object to be created before the items, so
    the Items' start index will be added by 1.
    That's why if set AllowPaging as false, item begin with 2, but begin with 3
    when set as true.

    In addition, a easy way to make sure the start index is just run the page
    once, and view it's source code in the Internet explorer. You can try out
    the means to see whether this helps.


    Regards,

    Steven Cheng
    Microsoft Developer Support Engineer
     
    MSFT, Dec 5, 2003
    #8
  9. Dave56

    Prodip Saha Guest

    The correct solution is:
    Add your code in the datagrid event called ItemDataBound. ItemCreated does
    not render correct CliendID. Why? I don't know. By the definition of
    ItemCreated, it should have been created in the memory and it should have
    the correct ClientID.

    ItemDataBound fires after ItemCreated and you have the correct ClientID
    there.

    Prodip Saha


    "Dave56" <> wrote in message
    news:...
    > I have a column in a DataGrid with the following EditItemTemplate:
    >
    > <EditItemTemplate>
    > <TextArea runat="server" Class="NormalText" wrap="soft"
    > id="TxtDescription" rows="3" cols="85"
    > style="FLOAT: left; POSITION: static">
    > <%# DataBinder.Eval(Container, "DataItem.Description") %></TextArea>
    > </EditItemTemplate>
    >
    > In the DataGrid_ItemCreated subroutine
    > e.Item.FindControl("TxtDescription").ClientID returns "TxtDescription".
    >
    > However, when I run the page and view source I see the following:
    >
    > <td class="ResultsDim" valign="Top">
    > <textarea name="DGItems:_ctl6:TxtDescription"
    > id="DGItems__ctl6_TxtDescription" Class="NormalText" wrap="soft" rows="3"
    > cols="85" style="FLOAT: left; POSITION: static"></textarea>
    > </td>
    >
    > This shows a client id of "DGItems__ctl6_TxtDescription".
    > - DGItems is the name of the DataGrid
    > - TxtDescription is the control
    > Where does the ctl6 come from?
    >
    > Is there any way in the DataGrid_ItemCreated subroutine that I can get

    this
    > full clientID, or build it?
    >
    > Thanks in advance.
    >
    >
     
    Prodip Saha, Dec 12, 2003
    #9
    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. Shane O. Pinnell
    Replies:
    0
    Views:
    442
    Shane O. Pinnell
    Jul 9, 2003
  2. Maras
    Replies:
    1
    Views:
    3,126
    Cowboy \(Gregory A. Beamer\)
    Oct 15, 2003
  3. Lars Netzel
    Replies:
    1
    Views:
    452
    Lars Netzel
    Jul 30, 2004
  4. Olivier Matrot

    Default button in Datagrid EditItem

    Olivier Matrot, May 24, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,232
    Brock Allen
    May 24, 2005
  5. James Cooke
    Replies:
    0
    Views:
    229
    James Cooke
    Sep 11, 2008
Loading...

Share This Page