By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,422 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,422 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

rd****@austin.rr.com (R Duke) wrote in message news:<9b*************************@posting.google.c om>...
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

Nov 18 '05 #2

P: n/a
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(Sa*********@hotmail.com)
Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.