ClientID seems wrong inside DataGrid

Discussion in 'ASP .Net Web Controls' started by Jonathan Levine, Jun 23, 2004.

  1. Hi,

    I am trying to access the value of a text box in a DataGrid from inside JavaScript. I have tried the following:

    void Item_Created(object sender, DataGridItemEventArgs e)
    {
    Button b = ((Button)e.Item.FindControl("AddUserButton"));
    TextBox t = ((TextBox)e.Item.FindControl("Textbox"));
    string onclick = "if(document.getElementById('" + t.ClientID + "').value != \"\") {";

    //(extra stuff removed)

    b.Attributes.Add("onclick", onclick);
    }

    However, the JavaScript generated is

    if(document.getElementById('Textbox').value != "" {

    This is strange, as the item itself has an ID of MyPage1:UserGrid:_ctl3:Textbox

    I hope someone can help me with this.

    Thanks,
     
    Jonathan Levine, Jun 23, 2004
    #1
    1. Advertising

  2. Jonathan Levine

    Teemu Keiski Guest

    Hi,

    it is because you do this in ItemCreated. Control's complete ID is
    determined when it is added to the Controls collection and when in
    ItemCreated the current DataGridItem (which you access via e.Item) is not
    yet added to the DataGrid's Controls collection.

    I suggest you remove this code to ItemdataBound or optionally in DataGrid's
    PreRender event handler where you'd loop through all the dataGridItems and
    declare the attribute (script) for the TextBox.

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist
    http://blogs.aspadvice.com/joteke


    "Jonathan Levine" <> wrote in message
    news:...
    > Hi,
    >
    > I am trying to access the value of a text box in a DataGrid from inside

    JavaScript. I have tried the following:
    >
    > void Item_Created(object sender, DataGridItemEventArgs e)
    > {
    > Button b = ((Button)e.Item.FindControl("AddUserButton"));
    > TextBox t = ((TextBox)e.Item.FindControl("Textbox"));
    > string onclick = "if(document.getElementById('" + t.ClientID + "').value

    != \"\") {";
    >
    > //(extra stuff removed)
    >
    > b.Attributes.Add("onclick", onclick);
    > }
    >
    > However, the JavaScript generated is
    >
    > if(document.getElementById('Textbox').value != "" {
    >
    > This is strange, as the item itself has an ID of

    MyPage1:UserGrid:_ctl3:Textbox
    >
    > I hope someone can help me with this.
    >
    > Thanks,
     
    Teemu Keiski, Jun 23, 2004
    #2
    1. Advertising

  3. Thanks for Teemu's informative suggestions.

    Hi Jonathan,

    In composite asp.net server controls, the sub controls(nested controls)'s
    id may be composed of the Parent Control's ClientID and its own clientID.
    And Teemu's suggestions are correct that in ItemDataBound or PreRender
    event, the DataGrid's child controls has been added into the control
    hierarchy and we can get the complete acutal clientside ID there. Please
    have a try. Thanks.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
     
    Steven Cheng[MSFT], Jun 24, 2004
    #3
  4. Thanks, Teemu and Steve.

    "Steven Cheng[MSFT]" wrote:
    > In composite asp.net server controls, the sub controls(nested controls)'s
    > id may be composed of the Parent Control's ClientID and its own clientID.
    > And Teemu's suggestions are correct that in ItemDataBound or PreRender
    > event, the DataGrid's child controls has been added into the control
    > hierarchy and we can get the complete acutal clientside ID there. Please
    > have a try. Thanks.


    One problem is that the control is actually in the Data Grid's footer. Is there any way to get access to the controls in the footer from inside the PreRender?

    Thanks,

    Jonathan
     
    Jonathan Levine, Jun 25, 2004
    #4
    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. Maras
    Replies:
    1
    Views:
    3,126
    Cowboy \(Gregory A. Beamer\)
    Oct 15, 2003
  2. Alexandre Soares

    getting ClientID of control inside datagrid item

    Alexandre Soares, Oct 3, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    746
    Alexandre Soares
    Oct 3, 2004
  3. Replies:
    1
    Views:
    4,842
    =?Utf-8?B?c29jaWV0b3BpYS5uZXQ=?=
    Jul 15, 2005
  4. DC
    Replies:
    3
    Views:
    1,376
    sbellouti
    Nov 9, 2006
  5. John Kotuby
    Replies:
    4
    Views:
    2,339
    John Kotuby
    Jan 12, 2009
Loading...

Share This Page