Controls ID are not being respected?

G

Guest

I have a Custom WebControl which adds several children at OnInit.
In OnInit I set the ChildControl to ParentID + "_" + Control's ID
so it would be something like MyContainer_VendorID
Now the control is moved to a TableCell in the Control's OnLoad event.
This control still has an ID of MyContainer_VendorID.
the Control is added to the Page. There the control has a value of
MyContainer_VendorID
In the Page's Render statement I overloaded it so i could put a breakpoint
and double check. so i checked myTable.Rows[AnIndex].Cells[5].Controls[0].ID
and sure enough the ID is MyContainer_VendorID.

When the page is rendered however it shows up as VendorID, and I do not see
why. the control always has a value of MyContainer_VendorID

WebControl_OnInit
Create Select
Give It ID OF VendorID
Give it new ID of ID + Vendor so it is now MyContainer_VendorID
AddControl to this.Controls
End Init

WebControl_OnLoad
Transfer all this.Controls to New TableRow as Cells
return this row as a property

Page_OnLoad
add the TableRow to our table
// here the Id is MyContainer_Vendor

Page_Render(
base.Render() //. here the Id is MyContainer_VendorID


now when the page is actually rendered the id is VendorID
I have NO idea how this is possible since before the control is added to the
WebControl's collection it is given the id of MyContainer_VendorID

Actual Debugger Excerpt
tblPOParts.Rows[1].Cells[4].Controls[0].ID "pork0_VendorID" string

protected override void Render(HtmlTextWriter writer)

{

base.Render (writer); // tblPOParts.Rows[1].Cells[4].Controls[0].ID
"pork0_VendorID" string


}
 
G

Guest

No that really does not help because my problem is that the ID is getting
set properly and it is retaining its value all the way up to and into
Page_Render but when the page renders the ID mysteriously is not what it was
at Page_Render
The problem is not setting them.
I am setting them

1) CreateControl
2) Give Name of VendorID
2) Give Name of ParentID + VendorID so name is now "MyContainer_VendorID" <<
Verified with the DEBUGGER
3) Add Control to WebControl.
4) Still has a ID of MyContainer_VendorID

5) in the Page_Render method
this.Controls[mytableindex].Rows[2].Cells[5].Controls[0].ID is
"MyContainer_VendorID";
Seen from the debugger again.

6) When it renders it spits out VendorID.
I am not sure why it is even remembering that VendorID
because nothing significant happens until it is given a new nickname





Steve C. Orr said:
You need to get the ClientID, not just the ID.
The reason they get named such ways is all about naming containers.
Here's more info:
http://msdn.microsoft.com/library/d...l/vbtskreferencingcontrolsinwebformspages.asp




I have a Custom WebControl which adds several children at OnInit.
In OnInit I set the ChildControl to ParentID + "_" + Control's ID
so it would be something like MyContainer_VendorID
Now the control is moved to a TableCell in the Control's OnLoad event.
This control still has an ID of MyContainer_VendorID.
the Control is added to the Page. There the control has a value of
MyContainer_VendorID
In the Page's Render statement I overloaded it so i could put a
breakpoint and double check. so i checked
myTable.Rows[AnIndex].Cells[5].Controls[0].ID and sure enough the ID is
MyContainer_VendorID.

When the page is rendered however it shows up as VendorID, and I do not
see why. the control always has a value of MyContainer_VendorID

WebControl_OnInit
Create Select
Give It ID OF VendorID
Give it new ID of ID + Vendor so it is now MyContainer_VendorID
AddControl to this.Controls
End Init

WebControl_OnLoad
Transfer all this.Controls to New TableRow as Cells
return this row as a property

Page_OnLoad
add the TableRow to our table
// here the Id is MyContainer_Vendor

Page_Render(
base.Render() //. here the Id is MyContainer_VendorID


now when the page is actually rendered the id is VendorID
I have NO idea how this is possible since before the control is added to
the WebControl's collection it is given the id of MyContainer_VendorID

Actual Debugger Excerpt
tblPOParts.Rows[1].Cells[4].Controls[0].ID "pork0_VendorID" string

protected override void Render(HtmlTextWriter writer)

{

base.Render (writer); // tblPOParts.Rows[1].Cells[4].Controls[0].ID
"pork0_VendorID" string


}
 
S

Steve C. Orr [MVP, MCSD]

As I said, do not look at the ID property to determine if it is what you
expected. This is not very reliable.
Instead, look at the ClientID property.




No that really does not help because my problem is that the ID is getting
set properly and it is retaining its value all the way up to and into
Page_Render but when the page renders the ID mysteriously is not what it
was at Page_Render
The problem is not setting them.
I am setting them

1) CreateControl
2) Give Name of VendorID
2) Give Name of ParentID + VendorID so name is now "MyContainer_VendorID"
<< Verified with the DEBUGGER
3) Add Control to WebControl.
4) Still has a ID of MyContainer_VendorID

5) in the Page_Render method
this.Controls[mytableindex].Rows[2].Cells[5].Controls[0].ID is
"MyContainer_VendorID";
Seen from the debugger again.

6) When it renders it spits out VendorID.
I am not sure why it is even remembering that VendorID
because nothing significant happens until it is given a new nickname





Steve C. Orr said:
You need to get the ClientID, not just the ID.
The reason they get named such ways is all about naming containers.
Here's more info:
http://msdn.microsoft.com/library/d...l/vbtskreferencingcontrolsinwebformspages.asp




I have a Custom WebControl which adds several children at OnInit.
In OnInit I set the ChildControl to ParentID + "_" + Control's ID
so it would be something like MyContainer_VendorID
Now the control is moved to a TableCell in the Control's OnLoad event.
This control still has an ID of MyContainer_VendorID.
the Control is added to the Page. There the control has a value of
MyContainer_VendorID
In the Page's Render statement I overloaded it so i could put a
breakpoint and double check. so i checked
myTable.Rows[AnIndex].Cells[5].Controls[0].ID and sure enough the ID is
MyContainer_VendorID.

When the page is rendered however it shows up as VendorID, and I do not
see why. the control always has a value of MyContainer_VendorID

WebControl_OnInit
Create Select
Give It ID OF VendorID
Give it new ID of ID + Vendor so it is now MyContainer_VendorID
AddControl to this.Controls
End Init

WebControl_OnLoad
Transfer all this.Controls to New TableRow as Cells
return this row as a property

Page_OnLoad
add the TableRow to our table
// here the Id is MyContainer_Vendor

Page_Render(
base.Render() //. here the Id is MyContainer_VendorID


now when the page is actually rendered the id is VendorID
I have NO idea how this is possible since before the control is added to
the WebControl's collection it is given the id of MyContainer_VendorID

Actual Debugger Excerpt
tblPOParts.Rows[1].Cells[4].Controls[0].ID "pork0_VendorID" string

protected override void Render(HtmlTextWriter writer)

{

base.Render (writer); // tblPOParts.Rows[1].Cells[4].Controls[0].ID
"pork0_VendorID" string


}
 
G

Guest

I guess I will restate my real question for the third time then.
WHY Is it behaving this way?

Steve C. Orr said:
As I said, do not look at the ID property to determine if it is what you
expected. This is not very reliable.
Instead, look at the ClientID property.




No that really does not help because my problem is that the ID is getting
set properly and it is retaining its value all the way up to and into
Page_Render but when the page renders the ID mysteriously is not what it
was at Page_Render
The problem is not setting them.
I am setting them

1) CreateControl
2) Give Name of VendorID
2) Give Name of ParentID + VendorID so name is now "MyContainer_VendorID"
<< Verified with the DEBUGGER
3) Add Control to WebControl.
4) Still has a ID of MyContainer_VendorID

5) in the Page_Render method
this.Controls[mytableindex].Rows[2].Cells[5].Controls[0].ID is
"MyContainer_VendorID";
Seen from the debugger again.

6) When it renders it spits out VendorID.
I am not sure why it is even remembering that VendorID
because nothing significant happens until it is given a new nickname





Steve C. Orr said:
You need to get the ClientID, not just the ID.
The reason they get named such ways is all about naming containers.
Here's more info:
http://msdn.microsoft.com/library/d...l/vbtskreferencingcontrolsinwebformspages.asp




I have a Custom WebControl which adds several children at OnInit.
In OnInit I set the ChildControl to ParentID + "_" + Control's ID
so it would be something like MyContainer_VendorID
Now the control is moved to a TableCell in the Control's OnLoad event.
This control still has an ID of MyContainer_VendorID.
the Control is added to the Page. There the control has a value of
MyContainer_VendorID
In the Page's Render statement I overloaded it so i could put a
breakpoint and double check. so i checked
myTable.Rows[AnIndex].Cells[5].Controls[0].ID and sure enough the ID is
MyContainer_VendorID.

When the page is rendered however it shows up as VendorID, and I do not
see why. the control always has a value of MyContainer_VendorID

WebControl_OnInit
Create Select
Give It ID OF VendorID
Give it new ID of ID + Vendor so it is now MyContainer_VendorID
AddControl to this.Controls
End Init

WebControl_OnLoad
Transfer all this.Controls to New TableRow as Cells
return this row as a property

Page_OnLoad
add the TableRow to our table
// here the Id is MyContainer_Vendor

Page_Render(
base.Render() //. here the Id is MyContainer_VendorID


now when the page is actually rendered the id is VendorID
I have NO idea how this is possible since before the control is added
to the WebControl's collection it is given the id of
MyContainer_VendorID

Actual Debugger Excerpt
tblPOParts.Rows[1].Cells[4].Controls[0].ID "pork0_VendorID" string

protected override void Render(HtmlTextWriter writer)

{

base.Render (writer); // tblPOParts.Rows[1].Cells[4].Controls[0].ID
"pork0_VendorID" string


}
 
W

Wilco Bauwer

Because an ID of a Control is not unique, which they should be on the
client (read: in the output html). For example, if you have a Repeater
with a Button with a certain ID, then the ID of each Button should be
unique.

ASP.NET solves this by not using the ID property as an ID on the
client, but by generating a ClientID in the PreRender phase of a
control's life cycle. The ClientID is based on a Control's ID and its
parent controls.

Whenever you need to refer to an ID of a control on the client, you
should use the ClientID, as Steve suggested, which is available to you
in the PreRender phase.
 
G

Guest

Right, I understand this and I was making the ID's Unique.
Hence why i was adding the ParentContainer.ID + "_" to the control's ID
However (for some reason asp.net is stripping it off) and when the form
posts back it says that the there are multiple controls with the same ID.
because there are.

Should I be "setting" the ClientID to whatever I want instead of
"getting" as Steve kept repeating? I had always assumed that the clientID
was something that one tried to avoid touching when creating web controls as
this could possibly break the control with the exact error I am getting in
certain scenarios.
 
W

Wilco Bauwer

First of all, it seems weird if ASP.NET "strips" stuff from your ID.
How are you testing this?

Second of all, no, you should not (nor can you afaik) set the ClientID.
ClientID only exposes the ID which will be in the output (e.g. HTML).

Is there any reason you are trying to manually build a unique ID?
Contrary to your assumption, it _is_ wise to use ClientID, also in
webcontrols. Doing so shouldn't result in the error you get right now.
If you do, the source of the problem lies somewhere else (for example a
missing INamingContainer marker interface implemented by your composite
control).
 
G

Guest

I have a Web Control that creates about 20 Control's
the Control's are Created in OnInit
in Onit
Create NewControl
Set ID
call Base.Init (because the class is inhierted)
in Base.Init
Set ID of controls to ParentID + "_" + Existing ID
Add Control's to WebControl's Child Collection

Each control is wrapped inside a TableCell which is wrapped inside of a
TableRow which is exhibited as a property.
in the Page_Init
I create an array of these custom web controls
I give each WebControl an ID + its Index
I add the Custom WebControls to the Page's Control Collection
I then have a valid reference to the TableRow I need.
I add this TableRow to the Page's ServerSide Table.

Each row will have multiple DataControls with ID's that are givven the ID's
of
MyContainer1_VendorID
MyContainer2_VendorID
etc because there are multiple controls.

The reason I am building a Unique ID is because for some reason it is NOT
giving them a unique ID. And it seems to be thwarting my attempt to make
anyunique ID's by stripping the ID i give each control so that they all have
the same ID
MyControl1_VendorID becomes VendorID
MyControl2_VendorID becomes VendorID
MyControl33_VendorID becomes VendorID
MyControl35_VendorID becomes VendorID

This of course naturally creates a problem when posting back.
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top