ClientID seems wrong inside DataGrid

J

Jonathan Levine

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,
 
T

Teemu Keiski

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 said:
Hi,

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

Steven Cheng[MSFT]

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
 
J

Jonathan Levine

Thanks, Teemu and Steve.

Steven Cheng said:
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
 

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,770
Messages
2,569,586
Members
45,084
Latest member
HansGeorgi

Latest Threads

Top