A
Alan Silver
Hello,
After much battling, I think I am finally seeing some progress on my
first user control (thanks to the patient souls who have answered my
many questions!!). I am left with one more problem though...
As detailed elsewhere, I have identified the need to use the ViewState
to hold a flag that indicates whether or not this is the first time the
control is loaded (ie if the flag isn't in the ViewState then it's the
first time, otherwise it's not the first time). I was going to do this
with something like ViewState["NotFirstTime"] and give it a value "y".
This led me to wonder what happens if I use two instances of the control
on a page. Presumably the first one to be loaded will set the ViewState
value, and the second will read this and think it has already been
loaded.
That led me to think of using the control's ID instead, so that it would
be uniquely identified. However, if I create a user control (which could
simply be some HTML, the ASP.NET equivalent of a server-side include
file) and that contains an instance of my control called (say) "fred",
then there is nothing to stop me adding an instance of the same control
with the same name to the main page. There wouldn't be a clash as the
fred inside the user control would not be visible directly to the main
page. Trouble is, this then causes a clash in the ViewState, as there
would be two controls looking at ViewState["fred"].
So (after all that waffle), is there a way of uniquely identifying a
control in the entire page hierarchy? I assume that there will be some
ID that the ASP.NET engine uses to identify controls. Presumably these
would be unique, and would be preserved across postbacks. Can I get at
this to make my ViewState identifier unique? Or is there a better way of
doing this?
Any advice appreciated. TIA
After much battling, I think I am finally seeing some progress on my
first user control (thanks to the patient souls who have answered my
many questions!!). I am left with one more problem though...
As detailed elsewhere, I have identified the need to use the ViewState
to hold a flag that indicates whether or not this is the first time the
control is loaded (ie if the flag isn't in the ViewState then it's the
first time, otherwise it's not the first time). I was going to do this
with something like ViewState["NotFirstTime"] and give it a value "y".
This led me to wonder what happens if I use two instances of the control
on a page. Presumably the first one to be loaded will set the ViewState
value, and the second will read this and think it has already been
loaded.
That led me to think of using the control's ID instead, so that it would
be uniquely identified. However, if I create a user control (which could
simply be some HTML, the ASP.NET equivalent of a server-side include
file) and that contains an instance of my control called (say) "fred",
then there is nothing to stop me adding an instance of the same control
with the same name to the main page. There wouldn't be a clash as the
fred inside the user control would not be visible directly to the main
page. Trouble is, this then causes a clash in the ViewState, as there
would be two controls looking at ViewState["fred"].
So (after all that waffle), is there a way of uniquely identifying a
control in the entire page hierarchy? I assume that there will be some
ID that the ASP.NET engine uses to identify controls. Presumably these
would be unique, and would be preserved across postbacks. Can I get at
this to make my ViewState identifier unique? Or is there a better way of
doing this?
Any advice appreciated. TIA