Problem Accessing WebForm Controls from a Dynamically created control (C#)

R

R Duke

I have tried everything I can think of to change the visible property
of a design time created control from a dynamically created control's
command event handler.

Here is the scenario.

I have a WebForm with some textboxes, dropdownlists, a panel,
imagebutton and so on. When I click on the image button (which was
created at design time) I dynamically build a table. In each of row of
that new table I put several cells and one cell contains an image
button. I am able to successfully get the event handler of this
dynamically created image button to fire (regardless of the row I am
clicking), but in the event handler I am setting the design'time
created panel's visible property to true but it doesn't actually do
anything. So I created a dummy textbox at design time and just tried
to set the text value of that textbox in the same dynamically created
image button event. It doesn't work. I know the event is firing
because I have debugged through it, but it doesn't actually change the
textbox text value even though the code runs.

Why can't I access the design-time controls of the form from my
dynamically created object's event handler?

Here is my event handler code:
string trackNum = e.CommandArgument.ToString();
this.lblArtistSearchType.Text = "Select Track " + trackNum + "
Artists";
this.txtHiddenArtistSearch.Text = "Unhide";
this.txtHiddenArtistSearchType.Text = trackNum;
this.pnlArtistSelection.Visible = true; <-- Doesn't work

Thank you,

Rodney
 
R

R Duke

I am now just creating a session variable in the event handler for my
dynamically created control(Image button) as follows:

private void LookupTrackArtist_Command(Object sender, CommandEventArgs
e)
{
// Get the command argument
string trackNum = e.CommandArgument.ToString();
Session["TrackSearch"] = trackNum;
}

And in the Page_Load (Where PostBack is true) event I am setting the
visibility of the panel as follows:
if (Session["TrackSearch"] != null)
{
this.pnlArtistSelection.Visible = true;
}

The problem now is that the Page_Load event seems to fire before the
event so the Session variable is not there when the Page_Load fires.
So I have to click on the dyanically created control (image button)
twice before the panel becomes visible.

What am I missing here? Thanks in advance for you help.

Rodney
 
G

Guest

Hi

First things first..

When u create a control DYNAMICALLY ... its yo responsibility to 'RECREATE' it every time a postback occurs ... that is because we work in a stateless envmt and every control(design-time) will be created by the page @ runtime EACH time the page is loaded (post-back) ...

Also the VIEWSTATE of that control will be saved (by default) and even u'll observe that if u'll recreate yo control in post-back (i.e in yo Page_Load event) its viewstate will be automatically assigned to it !!

Also to warn u that there certain :::Magical::: things that happen during debugging - that is sometines the behaviour while debugging the application and running it are not same !!

If u can describe it in more simple manner it'd help both of us ..

Tell me if it doesn't work([email protected])
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top