Naming Container difference between VS2003 and VS2005

M

Monty

I have an ASP.Net project I developed in VS2003 (ASP.Net 1.1) that works
fine, but when I convert it to VS2005 (ASP.Net 2.0) it's behavior seems to
change. I have the following datagrid in my project:

<asp:datagrid id="grid" runat="server" AutoGenerateColumns="False"
AllowSorting="False" DataKeyField="myID">
<Columns>
<asp:TemplateColumn HeaderText="Action">
<ItemTemplate>
'''''''NOTE FOLLOWING LINE:
<input Runat="server" ID="SelectedRequest" type="hidden"
NAME="SelectedRequest" />
'''''''NOTE PREVIOUS LINE ^
<asp:Literal Runat="server" ID="litRadioButtons" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Action Message">
<ItemTemplate>
Message:<br>
<asp:TextBox ID="txtMessage" Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>

When I run this page in VS2003, the line noted above is rendered as:

<input name="grid:_ctl2:SelectedRequest" id="grid__ctl2_SelectedRequest"
type="hidden" />

But when I run my converted project in VS2005, it is rendered as:

<input name="SelectedRequest" type="hidden" id="SelectedRequest" />

What am I missing here? Why isn't the ID of my input control being modified
by it's naming container?

TIA.
 
W

Walter Wang [MSFT]

Hi Monty,

I've tested it on my side and wasn't able to reproduce the issue you
described:


Output from ASP.NET 1.1

<input name="grid:_ctl2:SelectedRequest"
id="grid__ctl2_SelectedRequest" type="hidden" />

Output from ASP.NET 2.0:

<input name="grid$ctl02$SelectedRequest" type="hidden"
id="grid_ctl02_SelectedRequest" />


As you can see, only the name is changed using a different separator (which
is expected behavior -- MSDN document already stated that developers should
not use hardcoded id separator).


Would you please tell me more about your configuration? Thanks.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Monty

I've created a simple demo page which should clearly demonstrate the issue
I'm having. Using the demo page (below) as is, my hidden input controls are
rendered as:
<input name="SelectedRequest" type="hidden" id="SelectedRequest" />
However, if I comment out the four lines in the ItemCreated event, my hidden
input controls are now rendered as:
<input name="grid:_ctl2:SelectedRequest" type="hidden"
id="grid__ctl2_SelectedRequest" />

The results are all from within VS2008 targeting the 2.0 Framework. Can you
tell me what is causing the change in the client IDs? Thanks.

Here is my demo page:

<%@ Page Language="vb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<script runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
grid.DataSource = New Integer(5) {}
grid.DataBind()
End Sub

Private Sub grid_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles grid.ItemCreated
Dim oLit As System.Web.UI.WebControls.Literal =
e.Item.FindControl("litRadioButtons")
If Not oLit Is Nothing Then
System.Diagnostics.Debug.Print(CType(e.Item.FindControl("SelectedRequest"),
System.Web.UI.Control).ClientID)
End If
End Sub
</script>
<html>
<head>
<title>Test Client IDs</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:DataGrid ID="grid" runat="server" AutoGenerateColumns="False"
AllowSorting="False" >
<Columns>
<asp:TemplateColumn HeaderText="Action">
<ItemTemplate>
<input runat="server" id="SelectedRequest" type="hidden"
name="SelectedRequest" />
<asp:Literal runat="server"
ID="litRadioButtons"></asp:Literal>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
 
W

Walter Wang [MSFT]

Hi Monty,

Is this the same code logic as you were using in VS2003 and it worked
correctly?

I have reproduced the issue you mentioned in VS2008. The issue seems
related to accessing the control's ClientID property in the ItemCreated
event. I will do some more research and get back to you later.

Thanks for your feedback.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Monty,

Well, I have just found out it's a known issue of ASP.NET 2.0, we have an
open bug record for it.

Some explanation of this issue:

When ClientID is called, part of the operation is to retrieve the naming
container prefix to return the full client ID (with naming container
prefix), through the calling of UniqueID property. At the time ItemCreated
event is called, the control created in the event has not been added to the
control tree. It is added to the control tree after the event is done in
DataGrid's code. So you observed that ClientID returns only the ID in
ItemCreated event.

In v1.x, the UniqueID is computed everytime the property is called, meaning
that the control would walk up the tree to gather the necessary id prefix
for the naming containers. In v2.0, for better performance, we introduced
some caching code so the control tree walk up would only be done once for
the first time it is being accessed.


As a workaround, we should avoid calling ClientID in ItemCreated event.

If this workaround doesn't work for you, please contact our Customer
Support and Service to see if there will be hotfix available before it's
fully fixed in future versions. We're sorry for the inconvenience caused.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Monty

Hi Walter,

Thanks for the research. Do you have KB number for this? Do you know if this
issue is resolved with v3.5?

Thanks again...
 
M

Monty

For anyone following along, no this issue has not changed in the 3.5
Framework, but I was able to work around this issue by moving my code from
the ItemCreated event to the ItemDataBound event. Good luck!
 
W

Walter Wang [MSFT]

Hi Monty,

The public KB is under development.

Thanks for your feedback, this will certainly benefit others if they also
encouter this issue.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top