DataGrid EditItem ClientID

D

Dave56

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.
 
M

MSFT

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.)
 
D

Dave56

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

Thanks,
Dave
 
M

MSFT

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.)
 
M

MSFT

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
(e-mail address removed)
 
D

Dave56

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
 
D

Dave56

To use (e.Item.ItemIndex+2) is because that the real clientID's index
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
 
M

MSFT

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
(e-mail address removed)
 
P

Prodip Saha

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
 

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

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top