user control - image button only works on second select

R

richB

I have a web app which on selection from a dropdown dynamically loads using a
method:

CreateTree()
{
tree = this.LoadControl("DynamicTree.ascx");
//set tree Properties
....
this.treePanel.Controls.Add(tree);
}

with DynamicTree.ascx being user control containing labels, images and image
buttons (within a panel within the user control).

The objective is to display a pop-up .aspx window containing information
dependent on which image button was selected.

Initially I set just everything up with an ImageClickEventHandler on each
image button within the user control which performs javascript to launch a
new window. This event handler when triggered just reloaded the treePanel
without displaying the original user control or the pop-up.

Why is the user control not maintained within the panel on postback?

In order to resolve this I added the create tree method to the Page_Load
event of the main page and saved the information to recreate the tree
Properties within a Page.Session property. This initially appeared to resolve
the issue, the pop-up window appeared on clicking each button as required.
However if I changed the entry in the drop down on the main page the user
control correctly reloaded, but on selecting an image button for the first
time the pop-up did not appear. But if I select the same or a different
button for a second time then the window pops exactly as required.

Can anyone explain this behaviour? Do I need to run the createTree method on
Page_Load or is there a better approach?


Thanks,
Richard
 
S

Steven Cheng[MSFT]

Hi Richard,

Welcome to ASPNET newsgroup.
Regarding on the problem in creating dynamic webuser control, here are some
of my understanding and suggestions:

In the ASP.NET webform page model, all the controls are created in each
page request, not only the initial one, but also all the sequential post
back requests. Also, for dynamic created webcontrols, we need to add them
into the parent (container) control's child collection in each request, not
only in a certain postback event so as to make sure they'll appear in the
page correctly. So generally, Page's Init or Load event is the most proper
place for creating dynamic web server/user controls. Here are some tech
articles on creating dynamic webcontrols;

#HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET
http://support.microsoft.com/default.aspx?scid=kb;en-us;317794

#Creating Dynamic Data Entry User Interfaces
http://msdn.microsoft.com/library/en-us/dnaspp/html/dynamicui.asp?frame=true

So for your scenario, we'd suggest you put the dynamic web user
control(ascx) 's creating code in a function and call this function in
Page's Init or Load event according to a certain variable's value( you
can consider using ViewState or a <input type="hidden" ...> element to
store this value). Also, to make it eaiser, I've made a simple demo page
which dynamically display a UserControl according to a DropDownlist's
selected value on the page . I've attached the code in the attachement of
this message, you can get it through OE. (please let me know if you have
any problem accessing them).

In addition ,here are some other good articles on Usercontrol and asp.net
page model, event lifecycle:


#Events in ASP.NET Server Controls
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconeventsinwebformsco
ntrols.asp?frame=true

#Control Execution Lifecycle
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconControlExecutionLi
fecycle.asp?frame=true

#An Extensive Examination of User Controls
http://msdn.microsoft.com/library/en-us/dnaspp/html/usercontrols.asp?frame=t
rue

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)







--------------------
| Thread-Topic: user control - image button only works on second select
| thread-index: AcW9CLsGEpD/KcttSTWr0Xaekzr6Uw==
| X-WBNR-Posting-Host: 86.132.141.218
| From: "=?Utf-8?B?cmljaEI=?=" <[email protected]>
| Subject: user control - image button only works on second select
| Date: Mon, 19 Sep 2005 03:56:04 -0700
| Lines: 39
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:10895
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| I have a web app which on selection from a dropdown dynamically loads
using a
| method:
|
| CreateTree()
| {
| tree = this.LoadControl("DynamicTree.ascx");
| //set tree Properties
| ...
| this.treePanel.Controls.Add(tree);
| }
|
| with DynamicTree.ascx being user control containing labels, images and
image
| buttons (within a panel within the user control).
|
| The objective is to display a pop-up .aspx window containing information
| dependent on which image button was selected.
|
| Initially I set just everything up with an ImageClickEventHandler on each
| image button within the user control which performs javascript to launch
a
| new window. This event handler when triggered just reloaded the treePanel
| without displaying the original user control or the pop-up.
|
| Why is the user control not maintained within the panel on postback?
|
| In order to resolve this I added the create tree method to the Page_Load
| event of the main page and saved the information to recreate the tree
| Properties within a Page.Session property. This initially appeared to
resolve
| the issue, the pop-up window appeared on clicking each button as
required.
| However if I changed the entry in the drop down on the main page the user
| control correctly reloaded, but on selecting an image button for the
first
| time the pop-up did not appear. But if I select the same or a different
| button for a second time then the window pops exactly as required.
|
| Can anyone explain this behaviour? Do I need to run the createTree method
on
| Page_Load or is there a better approach?
|
|
| Thanks,
| Richard
|
 
R

richB

Thanks Steven, I have had a look through the articles which you have
referred, and particularly through your example and compared this to my own
code. I have found no fundamental differences. However the following pieces
are different:

1. I am using Page.Session to maintain details of the information in order
to recreate the user control. With the CreateTree() method used when I select
from the dropdown (on first and subsequent occasions):

private void DropDownList1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
this.Panel1.Controls.Clear();
this.Session.Clear();

if (this.DropDownList1.SelectedValue != "0")
{
int personId = Int32.Parse(this.DropDownList1.SelectedValue);

int [] parent = GetData.GetParents(personId);

familytree = new Tree(parent[0]== 0 ? personId : parent[0]);

people = familytree.GetTreePeople();
this.Session.Add("people",people);
this.Session.Add("zoom",zoom);
this.CreateTree();
this.Panel1.Visible = true;
}
else
{
this.Panel1.Visible = false;
}


private void CreateTree()
{
//original code
/*tree = this.LoadControl("DynamicTree.ascx");
((DynamicTree)tree).people= people;
((DynamicTree)tree).familytree = familytree;
((DynamicTree)tree).multiple = zoom;
//tree.EnableViewState = true;
this.Panel1.Controls.Add(tree);*/

//After post to MSDN
tree = Page.LoadControl("DynamicTree.ascx") as DynamicTree;
tree.people = people;
tree.familytree = familytree;
tree.multiple = zoom;
this.Panel1.Controls.Add(tree);


}
2. I have multiple selections of people within my drop down. Each person has
a number of related people all of which are used to build a tree of
relationships within the user control. Each related person has an information
button which is used to pop-up the window. Page load contains a foreach loop
which creates an ImageButton for each person:

ImageButton informationButton = new ImageButton();
informationButton.Style["POSITION"] = "absolute";
informationButton.Style["LEFT"] = (int)boxX-15+"px";
informationButton.Style["TOP"] = (int)boxY+"px";
informationButton.ImageUrl = "info.gif";
informationButton.ID = "Info"+ pers.id.ToString();
informationButton.Click += new
System.Web.UI.ImageClickEventHandler(this.informationButton_Click);
Trace.Warn(informationButton.ID.ToString()+ "event handler added");
informationButton.ToolTip = "information";
Panel1.Controls.Add(informationButton);

private void informationButton_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
Trace.Warn("sender: "+((ImageButton)sender).ID.ToString());


string sUrl = "PersonInformation.aspx?button="+((ImageButton)sender).ID;

string sScript = "<script language =javascript> ";

sScript += "window.open('" + sUrl +
"',null,'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=800,height=600,left=100,top=100');";

sScript += "</script> ";

Page.RegisterStartupScript("newpage",sScript);

}

I think that this is all the relevant code, is there any reason which either
of these two differences would cause the results that I have described. I.e.
the image buttons working on first selecting a person form the drop down, but
only working on selecting the button twice. (Interestingly I have just
discovered that if I select the default option – index 0 – and then select
another dropdown item then the button works on first press)

Any further help greatly appreciated.

Thanks,
Richard.
 
S

Steven Cheng[MSFT]

Hi Richard,

Thanks for your detailed response.
So does the test page and usercontrol I sent you works as you expected?
Also, from your further description, I have some question on the user
control, as for the main page and dropdownlist, I don't think there will
have any particular problems in them as long as we all Load the Usercontrol
correctly each time in Page's Load or Init event. Something I still not
sure is about the sub controls in your Usercontrol(which is dynamically
loaded), are the controls in the usercontrol (imagebuttons ) also
dynamically created and added ? Also, seems you mentioned that there're
also some other controls (buttons) in addition to the DropDownlist on the
main page?
If so, the problem will be abit more complex than I have expected. I'll
need some further tests.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



--------------------
| Thread-Topic: user control - image button only works on second select
| thread-index: AcW+GdakPHhJiWoOTDCOWHuZhfpqPA==
| X-WBNR-Posting-Host: 86.132.141.218
| From: "=?Utf-8?B?cmljaEI=?=" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: user control - image button only works on second select
| Date: Tue, 20 Sep 2005 12:31:03 -0700
| Lines: 240
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 8bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:10935
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Thanks Steven, I have had a look through the articles which you have
| referred, and particularly through your example and compared this to my
own
| code. I have found no fundamental differences. However the following
pieces
| are different:
|
| 1. I am using Page.Session to maintain details of the information in
order
| to recreate the user control. With the CreateTree() method used when I
select
| from the dropdown (on first and subsequent occasions):
|
| private void DropDownList1_SelectedIndexChanged(object sender,
| System.EventArgs e)
| {
| this.Panel1.Controls.Clear();
| this.Session.Clear();
|
| if (this.DropDownList1.SelectedValue != "0")
| {
| int personId = Int32.Parse(this.DropDownList1.SelectedValue);
|
| int [] parent = GetData.GetParents(personId);
|
| familytree = new Tree(parent[0]== 0 ? personId : parent[0]);
|
| people = familytree.GetTreePeople();
| this.Session.Add("people",people);
| this.Session.Add("zoom",zoom);
| this.CreateTree();
| this.Panel1.Visible = true;
| }
| else
| {
| this.Panel1.Visible = false;
| }
|
|
| private void CreateTree()
| {
| //original code
| /*tree = this.LoadControl("DynamicTree.ascx");
| ((DynamicTree)tree).people= people;
| ((DynamicTree)tree).familytree = familytree;
| ((DynamicTree)tree).multiple = zoom;
| //tree.EnableViewState = true;
| this.Panel1.Controls.Add(tree);*/
|
| //After post to MSDN
| tree = Page.LoadControl("DynamicTree.ascx") as DynamicTree;
| tree.people = people;
| tree.familytree = familytree;
| tree.multiple = zoom;
| this.Panel1.Controls.Add(tree);
|
|
| }
| 2. I have multiple selections of people within my drop down. Each person
has
| a number of related people all of which are used to build a tree of
| relationships within the user control. Each related person has an
information
| button which is used to pop-up the window. Page load contains a foreach
loop
| which creates an ImageButton for each person:
|
| ImageButton informationButton = new ImageButton();
| informationButton.Style["POSITION"] = "absolute";
| informationButton.Style["LEFT"] = (int)boxX-15+"px";
| informationButton.Style["TOP"] = (int)boxY+"px";
| informationButton.ImageUrl = "info.gif";
| informationButton.ID = "Info"+ pers.id.ToString();
| informationButton.Click += new
| System.Web.UI.ImageClickEventHandler(this.informationButton_Click);
| Trace.Warn(informationButton.ID.ToString()+ "event handler added");
| informationButton.ToolTip = "information";
| Panel1.Controls.Add(informationButton);
|
| private void informationButton_Click(object sender,
| System.Web.UI.ImageClickEventArgs e)
| {
| Trace.Warn("sender: "+((ImageButton)sender).ID.ToString());
|
|
| string sUrl =
"PersonInformation.aspx?button="+((ImageButton)sender).ID;
|
| string sScript = "<script language =javascript> ";
|
| sScript += "window.open('" + sUrl +
|
"',null,'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0
,width=800,height=600,left=100,top=100');";
|
| sScript += "</script> ";
|
| Page.RegisterStartupScript("newpage",sScript);
|
| }
|
| I think that this is all the relevant code, is there any reason which
either
| of these two differences would cause the results that I have described.
I.e.
| the image buttons working on first selecting a person form the drop down,
but
| only working on selecting the button twice. (Interestingly I have just
| discovered that if I select the default option �index 0 �and then
select
| another dropdown item then the button works on first press)
|
| Any further help greatly appreciated.
|
| Thanks,
| Richard.
|
|
| "Steven Cheng[MSFT]" wrote:
|
| > Hi Richard,
| >
| > Welcome to ASPNET newsgroup.
| > Regarding on the problem in creating dynamic webuser control, here are
some
| > of my understanding and suggestions:
| >
| > In the ASP.NET webform page model, all the controls are created in each
| > page request, not only the initial one, but also all the sequential
post
| > back requests. Also, for dynamic created webcontrols, we need to add
them
| > into the parent (container) control's child collection in each request,
not
| > only in a certain postback event so as to make sure they'll appear in
the
| > page correctly. So generally, Page's Init or Load event is the most
proper
| > place for creating dynamic web server/user controls. Here are some tech
| > articles on creating dynamic webcontrols;
| >
| > #HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET
| > http://support.microsoft.com/default.aspx?scid=kb;en-us;317794
| >
| > #Creating Dynamic Data Entry User Interfaces
| >
http://msdn.microsoft.com/library/en-us/dnaspp/html/dynamicui.asp?frame=true
| >
| > So for your scenario, we'd suggest you put the dynamic web user
| > control(ascx) 's creating code in a function and call this function in
| > Page's Init or Load event according to a certain variable's value(
you
| > can consider using ViewState or a <input type="hidden" ...> element to
| > store this value). Also, to make it eaiser, I've made a simple demo
page
| > which dynamically display a UserControl according to a DropDownlist's
| > selected value on the page . I've attached the code in the attachement
of
| > this message, you can get it through OE. (please let me know if you
have
| > any problem accessing them).
| >
| > In addition ,here are some other good articles on Usercontrol and
asp.net
| > page model, event lifecycle:
| >
| >
| > #Events in ASP.NET Server Controls
| >
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconeventsinwebformsco
| > ntrols.asp?frame=true
| >
| > #Control Execution Lifecycle
| >
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconControlExecutionLi
| > fecycle.asp?frame=true
| >
| > #An Extensive Examination of User Controls
| >
http://msdn.microsoft.com/library/en-us/dnaspp/html/usercontrols.asp?frame=t
| > rue
| >
| > Hope helps. Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| >
| >
| >
| >
| > --------------------
| > | Thread-Topic: user control - image button only works on second select
| > | thread-index: AcW9CLsGEpD/KcttSTWr0Xaekzr6Uw==
| > | X-WBNR-Posting-Host: 86.132.141.218
| > | From: "=?Utf-8?B?cmljaEI=?=" <[email protected]>
| > | Subject: user control - image button only works on second select
| > | Date: Mon, 19 Sep 2005 03:56:04 -0700
| > | Lines: 39
| > | Message-ID: <[email protected]>
| > | MIME-Version: 1.0
| > | Content-Type: text/plain;
| > | charset="Utf-8"
| > | Content-Transfer-Encoding: 7bit
| > | X-Newsreader: Microsoft CDO for Windows 2000
| > | Content-Class: urn:content-classes:message
| > | Importance: normal
| > | Priority: normal
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.webcontrols:10895
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| > |
| > | I have a web app which on selection from a dropdown dynamically loads
| > using a
| > | method:
| > |
| > | CreateTree()
| > | {
| > | tree = this.LoadControl("DynamicTree.ascx");
| > | //set tree Properties
| > | ...
| > | this.treePanel.Controls.Add(tree);
| > | }
| > |
| > | with DynamicTree.ascx being user control containing labels, images
and
| > image
| > | buttons (within a panel within the user control).
| > |
| > | The objective is to display a pop-up .aspx window containing
information
| > | dependent on which image button was selected.
| > |
| > | Initially I set just everything up with an ImageClickEventHandler on
each
| > | image button within the user control which performs javascript to
launch
| > a
| > | new window. This event handler when triggered just reloaded the
treePanel
| > | without displaying the original user control or the pop-up.
| > |
| > | Why is the user control not maintained within the panel on postback?
| > |
| > | In order to resolve this I added the create tree method to the
Page_Load
| > | event of the main page and saved the information to recreate the tree
| > | Properties within a Page.Session property. This initially appeared to
| > resolve
| > | the issue, the pop-up window appeared on clicking each button as
| > required.
| > | However if I changed the entry in the drop down on the main page the
user
| > | control correctly reloaded, but on selecting an image button for the
| > first
| > | time the pop-up did not appear. But if I select the same or a
different
| > | button for a second time then the window pops exactly as required.
| > |
| > | Can anyone explain this behaviour? Do I need to run the createTree
method
| > on
| > | Page_Load or is there a better approach?
| > |
| > |
| > | Thanks,
| > | Richard
| > |
|
 
R

richB

Steven,

Thanks again, hopefully the following will answer your questions:

1. test page and uc was fine, although mine is more complex with several
options in the drop down: option 0 performs hide, option 1 shows button 1,
option 2 shows button 2, options 3 showns button 1 & 3 etc...I shall try to
adjust your example and see if I can recreate my problem with this.

2. I am pretty sure that I am loading the control correctly using my
CreateTree() method.

3. The user control also has labels placed within the panel and images
generated to show the links between the different labels. ( I have tried
commenting these out and the problem still exists).

4. The image buttons are all dynamically loaded as per the code after point
2 in my last post.

5. the main page does contain two other linkbutton controls which change the
scaling of the controls within the user control (except for the image
buttons) as follws:

private void LinkButton1_Click(object sender, System.EventArgs e)
{
zoom = (int)(zoom * 1.25);
Trace.Write("zoom = " + zoom.ToString() );
this.Panel1.Controls.Remove(tree);
this.Session.Add("zoom",zoom);
CreateTree();

}

Again I have tried commenting out all of the code related to these buttons
and this makes no difference to my problem. It is however the case that if
the zoom is selected the image buttons do not work on first click.

As I say I shall try to build a simple example which I can attach to the
post which demonstrates the same behaviour unless there is anything which you
can think of which is likely to cause me these problems.

Thanks, Richard



Steven Cheng said:
Hi Richard,

Thanks for your detailed response.
So does the test page and usercontrol I sent you works as you expected?
Also, from your further description, I have some question on the user
control, as for the main page and dropdownlist, I don't think there will
have any particular problems in them as long as we all Load the Usercontrol
correctly each time in Page's Load or Init event. Something I still not
sure is about the sub controls in your Usercontrol(which is dynamically
loaded), are the controls in the usercontrol (imagebuttons ) also
dynamically created and added ? Also, seems you mentioned that there're
also some other controls (buttons) in addition to the DropDownlist on the
main page?
If so, the problem will be abit more complex than I have expected. I'll
need some further tests.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



--------------------
| Thread-Topic: user control - image button only works on second select
| thread-index: AcW+GdakPHhJiWoOTDCOWHuZhfpqPA==
| X-WBNR-Posting-Host: 86.132.141.218
| From: "=?Utf-8?B?cmljaEI=?=" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: user control - image button only works on second select
| Date: Tue, 20 Sep 2005 12:31:03 -0700
| Lines: 240
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 8bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:10935
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Thanks Steven, I have had a look through the articles which you have
| referred, and particularly through your example and compared this to my
own
| code. I have found no fundamental differences. However the following
pieces
| are different:
|
| 1. I am using Page.Session to maintain details of the information in
order
| to recreate the user control. With the CreateTree() method used when I
select
| from the dropdown (on first and subsequent occasions):
|
| private void DropDownList1_SelectedIndexChanged(object sender,
| System.EventArgs e)
| {
| this.Panel1.Controls.Clear();
| this.Session.Clear();
|
| if (this.DropDownList1.SelectedValue != "0")
| {
| int personId = Int32.Parse(this.DropDownList1.SelectedValue);
|
| int [] parent = GetData.GetParents(personId);
|
| familytree = new Tree(parent[0]== 0 ? personId : parent[0]);
|
| people = familytree.GetTreePeople();
| this.Session.Add("people",people);
| this.Session.Add("zoom",zoom);
| this.CreateTree();
| this.Panel1.Visible = true;
| }
| else
| {
| this.Panel1.Visible = false;
| }
|
|
| private void CreateTree()
| {
| //original code
| /*tree = this.LoadControl("DynamicTree.ascx");
| ((DynamicTree)tree).people= people;
| ((DynamicTree)tree).familytree = familytree;
| ((DynamicTree)tree).multiple = zoom;
| //tree.EnableViewState = true;
| this.Panel1.Controls.Add(tree);*/
|
| //After post to MSDN
| tree = Page.LoadControl("DynamicTree.ascx") as DynamicTree;
| tree.people = people;
| tree.familytree = familytree;
| tree.multiple = zoom;
| this.Panel1.Controls.Add(tree);
|
|
| }
| 2. I have multiple selections of people within my drop down. Each person
has
| a number of related people all of which are used to build a tree of
| relationships within the user control. Each related person has an
information
| button which is used to pop-up the window. Page load contains a foreach
loop
| which creates an ImageButton for each person:
|
| ImageButton informationButton = new ImageButton();
| informationButton.Style["POSITION"] = "absolute";
| informationButton.Style["LEFT"] = (int)boxX-15+"px";
| informationButton.Style["TOP"] = (int)boxY+"px";
| informationButton.ImageUrl = "info.gif";
| informationButton.ID = "Info"+ pers.id.ToString();
| informationButton.Click += new
| System.Web.UI.ImageClickEventHandler(this.informationButton_Click);
| Trace.Warn(informationButton.ID.ToString()+ "event handler added");
| informationButton.ToolTip = "information";
| Panel1.Controls.Add(informationButton);
|
| private void informationButton_Click(object sender,
| System.Web.UI.ImageClickEventArgs e)
| {
| Trace.Warn("sender: "+((ImageButton)sender).ID.ToString());
|
|
| string sUrl =
"PersonInformation.aspx?button="+((ImageButton)sender).ID;
|
| string sScript = "<script language =javascript> ";
|
| sScript += "window.open('" + sUrl +
|
"',null,'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0
,width=800,height=600,left=100,top=100');";
|
| sScript += "</script> ";
|
| Page.RegisterStartupScript("newpage",sScript);
|
| }
|
| I think that this is all the relevant code, is there any reason which
either
| of these two differences would cause the results that I have described.
I.e.
| the image buttons working on first selecting a person form the drop down,
but
| only working on selecting the button twice. (Interestingly I have just
| discovered that if I select the default option �index 0 �and then
select
| another dropdown item then the button works on first press)
|
| Any further help greatly appreciated.
|
| Thanks,
| Richard.
|
|
| "Steven Cheng[MSFT]" wrote:
|
| > Hi Richard,
| >
| > Welcome to ASPNET newsgroup.
| > Regarding on the problem in creating dynamic webuser control, here are
some
| > of my understanding and suggestions:
| >
| > In the ASP.NET webform page model, all the controls are created in each
| > page request, not only the initial one, but also all the sequential
post
| > back requests. Also, for dynamic created webcontrols, we need to add
them
| > into the parent (container) control's child collection in each request,
not
| > only in a certain postback event so as to make sure they'll appear in
the
| > page correctly. So generally, Page's Init or Load event is the most
proper
| > place for creating dynamic web server/user controls. Here are some tech
| > articles on creating dynamic webcontrols;
| >
| > #HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET
| > http://support.microsoft.com/default.aspx?scid=kb;en-us;317794
| >
| > #Creating Dynamic Data Entry User Interfaces
| >
http://msdn.microsoft.com/library/en-us/dnaspp/html/dynamicui.asp?frame=true
| >
| > So for your scenario, we'd suggest you put the dynamic web user
| > control(ascx) 's creating code in a function and call this function in
| > Page's Init or Load event according to a certain variable's value(
you
| > can consider using ViewState or a <input type="hidden" ...> element to
| > store this value). Also, to make it eaiser, I've made a simple demo
page
| > which dynamically display a UserControl according to a DropDownlist's
| > selected value on the page . I've attached the code in the attachement
of
| > this message, you can get it through OE. (please let me know if you
have
| > any problem accessing them).
| >
| > In addition ,here are some other good articles on Usercontrol and
asp.net
| > page model, event lifecycle:
| >
| >
| > #Events in ASP.NET Server Controls
| >
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconeventsinwebformsco
| > ntrols.asp?frame=true
| >
| > #Control Execution Lifecycle
| >
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconControlExecutionLi
| > fecycle.asp?frame=true
| >
| > #An Extensive Examination of User Controls
| >
http://msdn.microsoft.com/library/en-us/dnaspp/html/usercontrols.asp?frame=t
| > rue
| >
| > Hope helps. Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| >
| >
| >
| >
| > --------------------
| > | Thread-Topic: user control - image button only works on second select
| > | thread-index: AcW9CLsGEpD/KcttSTWr0Xaekzr6Uw==
| > | X-WBNR-Posting-Host: 86.132.141.218
| > | From: "=?Utf-8?B?cmljaEI=?=" <[email protected]>
| > | Subject: user control - image button only works on second select
| > | Date: Mon, 19 Sep 2005 03:56:04 -0700
| > | Lines: 39
| > | Message-ID: <[email protected]>
| > | MIME-Version: 1.0
| > | Content-Type: text/plain;
| > | charset="Utf-8"
| > | Content-Transfer-Encoding: 7bit
| > | X-Newsreader: Microsoft CDO for Windows 2000
| > | Content-Class: urn:content-classes:message
| > | Importance: normal
| > | Priority: normal
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.webcontrols:10895
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| > |
| > | I have a web app which on selection from a dropdown dynamically loads
| > using a
| > | method:
| > |
| > | CreateTree()
| > | {
| > | tree = this.LoadControl("DynamicTree.ascx");
| > | //set tree Properties
| > | ...
| > | this.treePanel.Controls.Add(tree);
| > | }
| > |
| > | with DynamicTree.ascx being user control containing labels, images
and
| > image
| > | buttons (within a panel within the user control).
| > |
| > | The objective is to display a pop-up .aspx window containing
information
| > | dependent on which image button was selected.
| > |
| > | Initially I set just everything up with an ImageClickEventHandler on
each
| > | image button within the user control which performs javascript to
 
R

richB

I have now modified your example and recreated my problem (and found a little
extra in your code which I had missed). Can you explain for me what is
happening...

Firstly I have added multiple listitems to the dropdown (Show 1, Show2,
Show3). I then ran the app and selected Show 1 and then Show2. On selecting
Show2 I get the error:
'Multiple controls with the same ID 'puc_1' were found. FindControl requires
that controls have unique IDs. ' hence noticing your line setting the uc.ID
(Sorry I missed this before).

I then commented out this code and this resulted in a second set of buttons
appearing when I select show2 (after initially selecting show1) which
disappear when I select one of them, leaving the original set of buttons
which work OK.

Can you explain whether setting the ID is necessary and also what is
happening when I have removed it and how I might solve it in the example....
I can then try and apply this to my own app.

Thanks, Richard







richB said:
Steven,

Thanks again, hopefully the following will answer your questions:

1. test page and uc was fine, although mine is more complex with several
options in the drop down: option 0 performs hide, option 1 shows button 1,
option 2 shows button 2, options 3 showns button 1 & 3 etc...I shall try to
adjust your example and see if I can recreate my problem with this.

2. I am pretty sure that I am loading the control correctly using my
CreateTree() method.

3. The user control also has labels placed within the panel and images
generated to show the links between the different labels. ( I have tried
commenting these out and the problem still exists).

4. The image buttons are all dynamically loaded as per the code after point
2 in my last post.

5. the main page does contain two other linkbutton controls which change the
scaling of the controls within the user control (except for the image
buttons) as follws:

private void LinkButton1_Click(object sender, System.EventArgs e)
{
zoom = (int)(zoom * 1.25);
Trace.Write("zoom = " + zoom.ToString() );
this.Panel1.Controls.Remove(tree);
this.Session.Add("zoom",zoom);
CreateTree();

}

Again I have tried commenting out all of the code related to these buttons
and this makes no difference to my problem. It is however the case that if
the zoom is selected the image buttons do not work on first click.

As I say I shall try to build a simple example which I can attach to the
post which demonstrates the same behaviour unless there is anything which you
can think of which is likely to cause me these problems.

Thanks, Richard



Steven Cheng said:
Hi Richard,

Thanks for your detailed response.
So does the test page and usercontrol I sent you works as you expected?
Also, from your further description, I have some question on the user
control, as for the main page and dropdownlist, I don't think there will
have any particular problems in them as long as we all Load the Usercontrol
correctly each time in Page's Load or Init event. Something I still not
sure is about the sub controls in your Usercontrol(which is dynamically
loaded), are the controls in the usercontrol (imagebuttons ) also
dynamically created and added ? Also, seems you mentioned that there're
also some other controls (buttons) in addition to the DropDownlist on the
main page?
If so, the problem will be abit more complex than I have expected. I'll
need some further tests.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



--------------------
| Thread-Topic: user control - image button only works on second select
| thread-index: AcW+GdakPHhJiWoOTDCOWHuZhfpqPA==
| X-WBNR-Posting-Host: 86.132.141.218
| From: "=?Utf-8?B?cmljaEI=?=" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: user control - image button only works on second select
| Date: Tue, 20 Sep 2005 12:31:03 -0700
| Lines: 240
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 8bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:10935
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Thanks Steven, I have had a look through the articles which you have
| referred, and particularly through your example and compared this to my
own
| code. I have found no fundamental differences. However the following
pieces
| are different:
|
| 1. I am using Page.Session to maintain details of the information in
order
| to recreate the user control. With the CreateTree() method used when I
select
| from the dropdown (on first and subsequent occasions):
|
| private void DropDownList1_SelectedIndexChanged(object sender,
| System.EventArgs e)
| {
| this.Panel1.Controls.Clear();
| this.Session.Clear();
|
| if (this.DropDownList1.SelectedValue != "0")
| {
| int personId = Int32.Parse(this.DropDownList1.SelectedValue);
|
| int [] parent = GetData.GetParents(personId);
|
| familytree = new Tree(parent[0]== 0 ? personId : parent[0]);
|
| people = familytree.GetTreePeople();
| this.Session.Add("people",people);
| this.Session.Add("zoom",zoom);
| this.CreateTree();
| this.Panel1.Visible = true;
| }
| else
| {
| this.Panel1.Visible = false;
| }
|
|
| private void CreateTree()
| {
| //original code
| /*tree = this.LoadControl("DynamicTree.ascx");
| ((DynamicTree)tree).people= people;
| ((DynamicTree)tree).familytree = familytree;
| ((DynamicTree)tree).multiple = zoom;
| //tree.EnableViewState = true;
| this.Panel1.Controls.Add(tree);*/
|
| //After post to MSDN
| tree = Page.LoadControl("DynamicTree.ascx") as DynamicTree;
| tree.people = people;
| tree.familytree = familytree;
| tree.multiple = zoom;
| this.Panel1.Controls.Add(tree);
|
|
| }
| 2. I have multiple selections of people within my drop down. Each person
has
| a number of related people all of which are used to build a tree of
| relationships within the user control. Each related person has an
information
| button which is used to pop-up the window. Page load contains a foreach
loop
| which creates an ImageButton for each person:
|
| ImageButton informationButton = new ImageButton();
| informationButton.Style["POSITION"] = "absolute";
| informationButton.Style["LEFT"] = (int)boxX-15+"px";
| informationButton.Style["TOP"] = (int)boxY+"px";
| informationButton.ImageUrl = "info.gif";
| informationButton.ID = "Info"+ pers.id.ToString();
| informationButton.Click += new
| System.Web.UI.ImageClickEventHandler(this.informationButton_Click);
| Trace.Warn(informationButton.ID.ToString()+ "event handler added");
| informationButton.ToolTip = "information";
| Panel1.Controls.Add(informationButton);
|
| private void informationButton_Click(object sender,
| System.Web.UI.ImageClickEventArgs e)
| {
| Trace.Warn("sender: "+((ImageButton)sender).ID.ToString());
|
|
| string sUrl =
"PersonInformation.aspx?button="+((ImageButton)sender).ID;
|
| string sScript = "<script language =javascript> ";
|
| sScript += "window.open('" + sUrl +
|
"',null,'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0
,width=800,height=600,left=100,top=100');";
|
| sScript += "</script> ";
|
| Page.RegisterStartupScript("newpage",sScript);
|
| }
|
| I think that this is all the relevant code, is there any reason which
either
| of these two differences would cause the results that I have described.
I.e.
| the image buttons working on first selecting a person form the drop down,
but
| only working on selecting the button twice. (Interestingly I have just
| discovered that if I select the default option �index 0 �and then
select
| another dropdown item then the button works on first press)
|
| Any further help greatly appreciated.
|
| Thanks,
| Richard.
|
|
| "Steven Cheng[MSFT]" wrote:
|
| > Hi Richard,
| >
| > Welcome to ASPNET newsgroup.
| > Regarding on the problem in creating dynamic webuser control, here are
some
| > of my understanding and suggestions:
| >
| > In the ASP.NET webform page model, all the controls are created in each
| > page request, not only the initial one, but also all the sequential
post
| > back requests. Also, for dynamic created webcontrols, we need to add
them
| > into the parent (container) control's child collection in each request,
not
| > only in a certain postback event so as to make sure they'll appear in
the
| > page correctly. So generally, Page's Init or Load event is the most
proper
| > place for creating dynamic web server/user controls. Here are some tech
| > articles on creating dynamic webcontrols;
| >
| > #HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET
| > http://support.microsoft.com/default.aspx?scid=kb;en-us;317794
| >
| > #Creating Dynamic Data Entry User Interfaces
| >
http://msdn.microsoft.com/library/en-us/dnaspp/html/dynamicui.asp?frame=true
| >
| > So for your scenario, we'd suggest you put the dynamic web user
| > control(ascx) 's creating code in a function and call this function in
| > Page's Init or Load event according to a certain variable's value(
you
| > can consider using ViewState or a <input type="hidden" ...> element to
| > store this value). Also, to make it eaiser, I've made a simple demo
page
| > which dynamically display a UserControl according to a DropDownlist's
| > selected value on the page . I've attached the code in the attachement
of
| > this message, you can get it through OE. (please let me know if you
have
| > any problem accessing them).
| >
| > In addition ,here are some other good articles on Usercontrol and
asp.net
| > page model, event lifecycle:
| >
| >
| > #Events in ASP.NET Server Controls
| >
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconeventsinwebformsco
| > ntrols.asp?frame=true
| >
| > #Control Execution Lifecycle
| >
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconControlExecutionLi
| > fecycle.asp?frame=true
| >
| > #An Extensive Examination of User Controls
| >
http://msdn.microsoft.com/library/en-us/dnaspp/html/usercontrols.asp?frame=t
| > rue
| >
| > Hope helps. Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| >
| >
| >
| >
| > --------------------
| > | Thread-Topic: user control - image button only works on second select
 
S

Steven Cheng[MSFT]

Hi Richard,

As for the Control's ID, it's very important for our dynanmically created
controls since for each control when adding into a certain NamingContainer,
the runtime will generate a page wide idenitifier for it according to that
control's ID and its naming container's ID. Then, this ID will be used to
identify the control in several processing, include VIEWSTATE mapping ,
postback event mapping (if exist). So when we add dynami control, we must
assign a unique control ID for each control instance. Also, this ID must be
consistent in each page's request, otherwaise, we'll get many unexpected
problems on Control's displaying or postback event handling.

Here is a good article discussing on the ASP.NET viewstate in page model:

#Understanding ASP.NET View State
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
/viewstate.asp

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)





--------------------
| Thread-Topic: user control - image button only works on second select
| thread-index: AcW+3nbKUiwhLRrbSFecodvqzTNnag==
| X-WBNR-Posting-Host: 86.132.141.218
| From: "=?Utf-8?B?cmljaEI=?=" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
| Subject: RE: user control - image button only works on second select
| Date: Wed, 21 Sep 2005 11:58:33 -0700
| Lines: 330
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 8bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:10952
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| I have now modified your example and recreated my problem (and found a
little
| extra in your code which I had missed). Can you explain for me what is
| happening...
|
| Firstly I have added multiple listitems to the dropdown (Show 1, Show2,
| Show3). I then ran the app and selected Show 1 and then Show2. On
selecting
| Show2 I get the error:
| 'Multiple controls with the same ID 'puc_1' were found. FindControl
requires
| that controls have unique IDs. ' hence noticing your line setting the
uc.ID
| (Sorry I missed this before).
|
| I then commented out this code and this resulted in a second set of
buttons
| appearing when I select show2 (after initially selecting show1) which
| disappear when I select one of them, leaving the original set of buttons
| which work OK.
|
| Can you explain whether setting the ID is necessary and also what is
| happening when I have removed it and how I might solve it in the
example....
| I can then try and apply this to my own app.
|
| Thanks, Richard
|
|
|
|
|
|
|
| "richB" wrote:
|
| > Steven,
| >
| > Thanks again, hopefully the following will answer your questions:
| >
| > 1. test page and uc was fine, although mine is more complex with
several
| > options in the drop down: option 0 performs hide, option 1 shows button
1,
| > option 2 shows button 2, options 3 showns button 1 & 3 etc...I shall
try to
| > adjust your example and see if I can recreate my problem with this.
| >
| > 2. I am pretty sure that I am loading the control correctly using my
| > CreateTree() method.
| >
| > 3. The user control also has labels placed within the panel and images
| > generated to show the links between the different labels. ( I have
tried
| > commenting these out and the problem still exists).
| >
| > 4. The image buttons are all dynamically loaded as per the code after
point
| > 2 in my last post.
| >
| > 5. the main page does contain two other linkbutton controls which
change the
| > scaling of the controls within the user control (except for the image
| > buttons) as follws:
| >
| > private void LinkButton1_Click(object sender, System.EventArgs e)
| > {
| > zoom = (int)(zoom * 1.25);
| > Trace.Write("zoom = " + zoom.ToString() );
| > this.Panel1.Controls.Remove(tree);
| > this.Session.Add("zoom",zoom);
| > CreateTree();
| >
| > }
| >
| > Again I have tried commenting out all of the code related to these
buttons
| > and this makes no difference to my problem. It is however the case that
if
| > the zoom is selected the image buttons do not work on first click.
| >
| > As I say I shall try to build a simple example which I can attach to
the
| > post which demonstrates the same behaviour unless there is anything
which you
| > can think of which is likely to cause me these problems.
| >
| > Thanks, Richard
| >
| >
| >
| > "Steven Cheng[MSFT]" wrote:
| >
| > > Hi Richard,
| > >
| > > Thanks for your detailed response.
| > > So does the test page and usercontrol I sent you works as you
expected?
| > > Also, from your further description, I have some question on the user
| > > control, as for the main page and dropdownlist, I don't think there
will
| > > have any particular problems in them as long as we all Load the
Usercontrol
| > > correctly each time in Page's Load or Init event. Something I still
not
| > > sure is about the sub controls in your Usercontrol(which is
dynamically
| > > loaded), are the controls in the usercontrol (imagebuttons ) also
| > > dynamically created and added ? Also, seems you mentioned that
there're
| > > also some other controls (buttons) in addition to the DropDownlist on
the
| > > main page?
| > > If so, the problem will be abit more complex than I have expected.
I'll
| > > need some further tests.
| > >
| > > Thanks,
| > >
| > > Steven Cheng
| > > Microsoft Online Support
| > >
| > > Get Secure! www.microsoft.com/security
| > > (This posting is provided "AS IS", with no warranties, and confers no
| > > rights.)
| > >
| > >
| > >
| > > --------------------
| > > | Thread-Topic: user control - image button only works on second
select
| > > | thread-index: AcW+GdakPHhJiWoOTDCOWHuZhfpqPA==
| > > | X-WBNR-Posting-Host: 86.132.141.218
| > > | From: "=?Utf-8?B?cmljaEI=?=" <[email protected]>
| > > | References: <[email protected]>
| > > <[email protected]>
| > > | Subject: RE: user control - image button only works on second select
| > > | Date: Tue, 20 Sep 2005 12:31:03 -0700
| > > | Lines: 240
| > > | Message-ID: <[email protected]>
| > > | MIME-Version: 1.0
| > > | Content-Type: text/plain;
| > > | charset="Utf-8"
| > > | Content-Transfer-Encoding: 8bit
| > > | X-Newsreader: Microsoft CDO for Windows 2000
| > > | Content-Class: urn:content-classes:message
| > > | Importance: normal
| > > | Priority: normal
| > > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| > > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| > > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| > > | Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA03.phx.gbl
| > > | Xref: TK2MSFTNGXA01.phx.gbl
| > > microsoft.public.dotnet.framework.aspnet.webcontrols:10935
| > > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| > > |
| > > | Thanks Steven, I have had a look through the articles which you
have
| > > | referred, and particularly through your example and compared this
to my
| > > own
| > > | code. I have found no fundamental differences. However the
following
| > > pieces
| > > | are different:
| > > |
| > > | 1. I am using Page.Session to maintain details of the information
in
| > > order
| > > | to recreate the user control. With the CreateTree() method used
when I
| > > select
| > > | from the dropdown (on first and subsequent occasions):
| > > |
| > > | private void DropDownList1_SelectedIndexChanged(object sender,
| > > | System.EventArgs e)
| > > | {
| > > | this.Panel1.Controls.Clear();
| > > | this.Session.Clear();
| > > |
| > > | if (this.DropDownList1.SelectedValue != "0")
| > > | {
| > > | int personId = Int32.Parse(this.DropDownList1.SelectedValue);
| > > |
| > > | int [] parent = GetData.GetParents(personId);
| > > |
| > > | familytree = new Tree(parent[0]== 0 ? personId : parent[0]);
| > > |
| > > | people = familytree.GetTreePeople();
| > > | this.Session.Add("people",people);
| > > | this.Session.Add("zoom",zoom);
| > > | this.CreateTree();
| > > | this.Panel1.Visible = true;
| > > | }
| > > | else
| > > | {
| > > | this.Panel1.Visible = false;
| > > | }
| > > |
| > > |
| > > | private void CreateTree()
| > > | {
| > > | //original code
| > > | /*tree = this.LoadControl("DynamicTree.ascx");
| > > | ((DynamicTree)tree).people= people;
| > > | ((DynamicTree)tree).familytree = familytree;
| > > | ((DynamicTree)tree).multiple = zoom;
| > > | //tree.EnableViewState = true;
| > > | this.Panel1.Controls.Add(tree);*/
| > > |
| > > | //After post to MSDN
| > > | tree = Page.LoadControl("DynamicTree.ascx") as DynamicTree;
| > > | tree.people = people;
| > > | tree.familytree = familytree;
| > > | tree.multiple = zoom;
| > > | this.Panel1.Controls.Add(tree);
| > > |
| > > |
| > > | }
| > > | 2. I have multiple selections of people within my drop down. Each
person
| > > has
| > > | a number of related people all of which are used to build a tree of
| > > | relationships within the user control. Each related person has an
| > > information
| > > | button which is used to pop-up the window. Page load contains a
foreach
| > > loop
| > > | which creates an ImageButton for each person:
| > > |
| > > | ImageButton informationButton = new ImageButton();
| > > | informationButton.Style["POSITION"] = "absolute";
| > > | informationButton.Style["LEFT"] = (int)boxX-15+"px";
| > > | informationButton.Style["TOP"] = (int)boxY+"px";
| > > | informationButton.ImageUrl = "info.gif";
| > > | informationButton.ID = "Info"+ pers.id.ToString();
| > > | informationButton.Click += new
| > > | System.Web.UI.ImageClickEventHandler(this.informationButton_Click);
| > > | Trace.Warn(informationButton.ID.ToString()+ "event handler added");
| > > | informationButton.ToolTip = "information";
| > > | Panel1.Controls.Add(informationButton);
| > > |
| > > | private void informationButton_Click(object sender,
| > > | System.Web.UI.ImageClickEventArgs e)
| > > | {
| > > | Trace.Warn("sender: "+((ImageButton)sender).ID.ToString());
| > > |
| > > |
| > > | string sUrl =
| > > "PersonInformation.aspx?button="+((ImageButton)sender).ID;
| > > |
| > > | string sScript = "<script language =javascript> ";
| > > |
| > > | sScript += "window.open('" + sUrl +
| > > |
| > >
"',null,'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0
| > > ,width=800,height=600,left=100,top=100');";
| > > |
| > > | sScript += "</script> ";
| > > |
| > > | Page.RegisterStartupScript("newpage",sScript);
| > > |
| > > | }
| > > |
| > > | I think that this is all the relevant code, is there any reason
which
| > > either
| > > | of these two differences would cause the results that I have
described.
| > > I.e.
| > > | the image buttons working on first selecting a person form the drop
down,
| > > but
| > > | only working on selecting the button twice. (Interestingly I have
just
| > > | discovered that if I select the default option ââ‚?index 0 Ã
¢â‚?and then
| > > select
| > > | another dropdown item then the button works on first press)
| > > |
| > > | Any further help greatly appreciated.
| > > |
| > > | Thanks,
| > > | Richard.
| > > |
| > > |
| > > | "Steven Cheng[MSFT]" wrote:
| > > |
| > > | > Hi Richard,
| > > | >
| > > | > Welcome to ASPNET newsgroup.
| > > | > Regarding on the problem in creating dynamic webuser control,
here are
| > > some
| > > | > of my understanding and suggestions:
| > > | >
| > > | > In the ASP.NET webform page model, all the controls are created
in each
| > > | > page request, not only the initial one, but also all the
sequential
| > > post
| > > | > back requests. Also, for dynamic created webcontrols, we need to
add
| > > them
| > > | > into the parent (container) control's child collection in each
request,
| > > not
| > > | > only in a certain postback event so as to make sure they'll
appear in
| > > the
| > > | > page correctly. So generally, Page's Init or Load event is the
most
| > > proper
| > > | > place for creating dynamic web server/user controls. Here are
some tech
| > > | > articles on creating dynamic webcontrols;
| > > | >
| > > | > #HOW TO: Dynamically Create Controls in ASP.NET by Using Visual
C# .NET
| > > | > http://support.microsoft.com/default.aspx?scid=kb;en-us;317794
| > > | >
| > > | > #Creating Dynamic Data Entry User Interfaces
| > > | >
| > >
http://msdn.microsoft.com/library/en-us/dnaspp/html/dynamicui.asp?frame=true
| > > | >
| > > | > So for your scenario, we'd suggest you put the dynamic web user
| > > | > control(ascx) 's creating code in a function and call this
function in
| > > | > Page's Init or Load event according to a certain variable's
value(
| > > you
| > > | > can consider using ViewState or a <input type="hidden" ...>
element to
| > > | > store this value). Also, to make it eaiser, I've made a simple
demo
| > > page
| > > | > which dynamically display a UserControl according to a
DropDownlist's
| > > | > selected value on the page . I've attached the code in the
attachement
| > > of
| > > | > this message, you can get it through OE. (please let me know if
you
| > > have
| > > | > any problem accessing them).
| > > | >
| > > | > In addition ,here are some other good articles on Usercontrol and
| > > asp.net
| > > | > page model, event lifecycle:
| > > | >
| > > | >
| > > | > #Events in ASP.NET Server Controls
| > > | >
| > >
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconeventsinwebformsco
| > > | > ntrols.asp?frame=true
| > > | >
| > > | > #Control Execution Lifecycle
| > > | >
| > >
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconControlExecutionLi
| > > | > fecycle.asp?frame=true
| > > | >
| > > | > #An Extensive Examination of User Controls
| > > | >
| > >
http://msdn.microsoft.com/library/en-us/dnaspp/html/usercontrols.asp?frame=t
| > > | > rue
| > > | >
| > > | > Hope helps. Thanks,
| > > | >
| > > | > Steven Cheng
| > > | > Microsoft Online Support
| > > | >
| > > | > Get Secure! www.microsoft.com/security
| > > | > (This posting is provided "AS IS", with no warranties, and
confers no
| > > | > rights.)
| > > | >
| > > | >
| > > | >
| > > | >
| > > | >
| > > | >
| > > | >
| > > | > --------------------
| > > | > | Thread-Topic: user control - image button only works on second
select
|
 
R

RichB

Steven, Thanks....

I've managed to sort things out by changing my CreateTree() method as follows:


private void CreateTree()
{
tree = Page.LoadControl("DynamicTree.ascx") as DynamicTree;

Panel1.Controls.Clear();
tree.ID = "puc_1";

tree.people = people;
tree.familytree = familytree;
tree.multiple = zoom;
this.Panel1.Controls.Add(tree);
}

I am still not quite sure why the behaviour exists, however it would appear
the ID is critical for the page to find the event to fire on postback.
This doesn't however really explain why the first set of controls added work
OK before and after the second set were added but I am not too concerned
as all is working now.

Thanks for your assistance.
Richard


Hello Steven Cheng[MSFT],
Hi Richard,

As for the Control's ID, it's very important for our dynanmically
created controls since for each control when adding into a certain
NamingContainer, the runtime will generate a page wide idenitifier for
it according to that control's ID and its naming container's ID. Then,
this ID will be used to identify the control in several processing,
include VIEWSTATE mapping , postback event mapping (if exist). So
when we add dynami control, we must assign a unique control ID for
each control instance. Also, this ID must be consistent in each page's
request, otherwaise, we'll get many unexpected problems on Control's
displaying or postback event handling.

Here is a good article discussing on the ASP.NET viewstate in page
model:

#Understanding ASP.NET View State
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnasp
p/html /viewstate.asp

Thanks,

Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| Thread-Topic: user control - image button only works on second
select
| thread-index: AcW+3nbKUiwhLRrbSFecodvqzTNnag==
| X-WBNR-Posting-Host: 86.132.141.218
| From: "=?Utf-8?B?cmljaEI=?=" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
| Subject: RE: user control - image button only works on second select
| Date: Wed, 21 Sep 2005 11:58:33 -0700
| Lines: 330
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 8bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:10952
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| I have now modified your example and recreated my problem (and found
a
little
| extra in your code which I had missed). Can you explain for me what
is
| happening...
|
| Firstly I have added multiple listitems to the dropdown (Show 1,
Show2,
| Show3). I then ran the app and selected Show 1 and then Show2. On
selecting
| Show2 I get the error:
| 'Multiple controls with the same ID 'puc_1' were found. FindControl
requires
| that controls have unique IDs. ' hence noticing your line setting
the
uc.ID
| (Sorry I missed this before).
|
| I then commented out this code and this resulted in a second set of
buttons
| appearing when I select show2 (after initially selecting show1)
which
| disappear when I select one of them, leaving the original set of
buttons
| which work OK.
|
| Can you explain whether setting the ID is necessary and also what is
| happening when I have removed it and how I might solve it in the
example....
| I can then try and apply this to my own app.
|
| Thanks, Richard
|
|
|
|
|
|
|
| "richB" wrote:
|
| > Steven,
| >
| > Thanks again, hopefully the following will answer your questions:
| >
| > 1. test page and uc was fine, although mine is more complex with
several
| > options in the drop down: option 0 performs hide, option 1 shows
button
1,
| > option 2 shows button 2, options 3 showns button 1 & 3 etc...I
shall
try to
| > adjust your example and see if I can recreate my problem with
this.
| >
| > 2. I am pretty sure that I am loading the control correctly using
my
| > CreateTree() method.
| >
| > 3. The user control also has labels placed within the panel and
images
| > generated to show the links between the different labels. ( I have
tried
| > commenting these out and the problem still exists).
| >
| > 4. The image buttons are all dynamically loaded as per the code
after
point
| > 2 in my last post.
| >
| > 5. the main page does contain two other linkbutton controls which
change the
| > scaling of the controls within the user control (except for the
image
| > buttons) as follws:
| >
| > private void LinkButton1_Click(object sender, System.EventArgs e)
| > {
| > zoom = (int)(zoom * 1.25);
| > Trace.Write("zoom = " + zoom.ToString() );
| > this.Panel1.Controls.Remove(tree);
| > this.Session.Add("zoom",zoom);
| > CreateTree();
| >
| > }
| >
| > Again I have tried commenting out all of the code related to these
buttons
| > and this makes no difference to my problem. It is however the case
that
if
| > the zoom is selected the image buttons do not work on first click.
| >
| > As I say I shall try to build a simple example which I can attach
to
the
| > post which demonstrates the same behaviour unless there is
anything
which you
| > can think of which is likely to cause me these problems.
| >
| > Thanks, Richard
| >
| >
| >
| > "Steven Cheng[MSFT]" wrote:
| >
| > > Hi Richard,
| > >
| > > Thanks for your detailed response.
| > > So does the test page and usercontrol I sent you works as you
expected?
| > > Also, from your further description, I have some question on the
user
| > > control, as for the main page and dropdownlist, I don't think
there
will
| > > have any particular problems in them as long as we all Load the
Usercontrol
| > > correctly each time in Page's Load or Init event. Something I
still
not
| > > sure is about the sub controls in your Usercontrol(which is
dynamically
| > > loaded), are the controls in the usercontrol (imagebuttons )
also
| > > dynamically created and added ? Also, seems you mentioned that
there're
| > > also some other controls (buttons) in addition to the
DropDownlist on
the
| > > main page?
| > > If so, the problem will be abit more complex than I have
expected.
I'll
| > > need some further tests.
| > >
| > > Thanks,
| > >
| > > Steven Cheng
| > > Microsoft Online Support
| > >
| > > Get Secure! www.microsoft.com/security
| > > (This posting is provided "AS IS", with no warranties, and
confers no
| > > rights.)
| > >
| > >
| > >
| > > --------------------
| > > | Thread-Topic: user control - image button only works on second
select
| > > | thread-index: AcW+GdakPHhJiWoOTDCOWHuZhfpqPA==
| > > | X-WBNR-Posting-Host: 86.132.141.218
| > > | From: "=?Utf-8?B?cmljaEI=?=" <[email protected]>
| > > | References:
<[email protected]>
| > > <[email protected]>
| > > | Subject: RE: user control - image button only works on second
select
| > > | Date: Tue, 20 Sep 2005 12:31:03 -0700
| > > | Lines: 240
| > > | Message-ID:
<[email protected]>
| > > | MIME-Version: 1.0
| > > | Content-Type: text/plain;
| > > | charset="Utf-8"
| > > | Content-Transfer-Encoding: 8bit
| > > | X-Newsreader: Microsoft CDO for Windows 2000
| > > | Content-Class: urn:content-classes:message
| > > | Importance: normal
| > > | Priority: normal
| > > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| > > | Newsgroups:
microsoft.public.dotnet.framework.aspnet.webcontrols
| > > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| > > | Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA03.phx.gbl
| > > | Xref: TK2MSFTNGXA01.phx.gbl
| > > microsoft.public.dotnet.framework.aspnet.webcontrols:10935
| > > | X-Tomcat-NG:
microsoft.public.dotnet.framework.aspnet.webcontrols
| > > |
| > > | Thanks Steven, I have had a look through the articles which
you
have
| > > | referred, and particularly through your example and compared
this
to my
| > > own
| > > | code. I have found no fundamental differences. However the
following
| > > pieces
| > > | are different:
| > > |
| > > | 1. I am using Page.Session to maintain details of the
information
in
| > > order
| > > | to recreate the user control. With the CreateTree() method
used
when I
| > > select
| > > | from the dropdown (on first and subsequent occasions):
| > > |
| > > | private void DropDownList1_SelectedIndexChanged(object sender,
| > > | System.EventArgs e)
| > > | {
| > > | this.Panel1.Controls.Clear();
| > > | this.Session.Clear();
| > > |
| > > | if (this.DropDownList1.SelectedValue != "0")
| > > | {
| > > | int personId =
Int32.Parse(this.DropDownList1.SelectedValue);
| > > |
| > > | int [] parent = GetData.GetParents(personId);
| > > |
| > > | familytree = new Tree(parent[0]== 0 ? personId :
parent[0]);
| > > |
| > > | people = familytree.GetTreePeople();
| > > | this.Session.Add("people",people);
| > > | this.Session.Add("zoom",zoom);
| > > | this.CreateTree();
| > > | this.Panel1.Visible = true;
| > > | }
| > > | else
| > > | {
| > > | this.Panel1.Visible = false;
| > > | }
| > > |
| > > |
| > > | private void CreateTree()
| > > | {
| > > | //original code
| > > | /*tree = this.LoadControl("DynamicTree.ascx");
| > > | ((DynamicTree)tree).people= people;
| > > | ((DynamicTree)tree).familytree = familytree;
| > > | ((DynamicTree)tree).multiple = zoom;
| > > | //tree.EnableViewState = true;
| > > | this.Panel1.Controls.Add(tree);*/
| > > |
| > > | //After post to MSDN
| > > | tree = Page.LoadControl("DynamicTree.ascx") as DynamicTree;
| > > | tree.people = people;
| > > | tree.familytree = familytree;
| > > | tree.multiple = zoom;
| > > | this.Panel1.Controls.Add(tree);
| > > |
| > > |
| > > | }
| > > | 2. I have multiple selections of people within my drop down.
Each
person
| > > has
| > > | a number of related people all of which are used to build a
tree of
| > > | relationships within the user control. Each related person has
an
| > > information
| > > | button which is used to pop-up the window. Page load contains
a
foreach
| > > loop
| > > | which creates an ImageButton for each person:
| > > |
| > > | ImageButton informationButton = new ImageButton();
| > > | informationButton.Style["POSITION"] = "absolute";
| > > | informationButton.Style["LEFT"] = (int)boxX-15+"px";
| > > | informationButton.Style["TOP"] = (int)boxY+"px";
| > > | informationButton.ImageUrl = "info.gif";
| > > | informationButton.ID = "Info"+ pers.id.ToString();
| > > | informationButton.Click += new
| > > |
System.Web.UI.ImageClickEventHandler(this.informationButton_Click);
| > > | Trace.Warn(informationButton.ID.ToString()+ "event handler
added");
| > > | informationButton.ToolTip = "information";
| > > | Panel1.Controls.Add(informationButton);
| > > |
| > > | private void informationButton_Click(object sender,
| > > | System.Web.UI.ImageClickEventArgs e)
| > > | {
| > > | Trace.Warn("sender: "+((ImageButton)sender).ID.ToString());
| > > |
| > > |
| > > | string sUrl =
| > > "PersonInformation.aspx?button="+((ImageButton)sender).ID;
| > > |
| > > | string sScript = "<script language =javascript> ";
| > > |
| > > | sScript += "window.open('" + sUrl +
| > > |
| > >
"',null,'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resiz
able=0
| > > ,width=800,height=600,left=100,top=100');";
| > > |
| > > | sScript += "</script> ";
| > > |
| > > | Page.RegisterStartupScript("newpage",sScript);
| > > |
| > > | }
| > > |
| > > | I think that this is all the relevant code, is there any
reason
which
| > > either
| > > | of these two differences would cause the results that I have
described.
| > > I.e.
| > > | the image buttons working on first selecting a person form the
drop
down,
| > > but
| > > | only working on selecting the button twice. (Interestingly I
have
just
| > > | discovered that if I select the default option ââ‚?index 0 Ã
¢â‚?and then
| > > select
| > > | another dropdown item then the button works on first press)
| > > |
| > > | Any further help greatly appreciated.
| > > |
| > > | Thanks,
| > > | Richard.
| > > |
| > > |
| > > | "Steven Cheng[MSFT]" wrote:
| > > |
| > > | > Hi Richard,
| > > | >
| > > | > Welcome to ASPNET newsgroup.
| > > | > Regarding on the problem in creating dynamic webuser
control,
here are
| > > some
| > > | > of my understanding and suggestions:
| > > | >
| > > | > In the ASP.NET webform page model, all the controls are
created
in each
| > > | > page request, not only the initial one, but also all the
sequential
| > > post
| > > | > back requests. Also, for dynamic created webcontrols, we
need to
add
| > > them
| > > | > into the parent (container) control's child collection in
each
request,
| > > not
| > > | > only in a certain postback event so as to make sure they'll
appear in
| > > the
| > > | > page correctly. So generally, Page's Init or Load event is
the
most
| > > proper
| > > | > place for creating dynamic web server/user controls. Here
are
some tech
| > > | > articles on creating dynamic webcontrols;
| > > | >
| > > | > #HOW TO: Dynamically Create Controls in ASP.NET by Using
Visual
C# .NET
| > > | >
http://support.microsoft.com/default.aspx?scid=kb;en-us;317794
| > > | >
| > > | > #Creating Dynamic Data Entry User Interfaces
| > > | >
| > >
http://msdn.microsoft.com/library/en-us/dnaspp/html/dynamicui.asp?fram
e=true
| > > | >
| > > | > So for your scenario, we'd suggest you put the dynamic web
user
| > > | > control(ascx) 's creating code in a function and call this
function in
| > > | > Page's Init or Load event according to a certain
variable's
value(
| > > you
| > > | > can consider using ViewState or a <input type="hidden" ...>
element to
| > > | > store this value). Also, to make it eaiser, I've made a
simple
demo
| > > page
| > > | > which dynamically display a UserControl according to a
DropDownlist's
| > > | > selected value on the page . I've attached the code in the
attachement
| > > of
| > > | > this message, you can get it through OE. (please let me know
if
you
| > > have
| > > | > any problem accessing them).
| > > | >
| > > | > In addition ,here are some other good articles on
Usercontrol and
| > > asp.net
| > > | > page model, event lifecycle:
| > > | >
| > > | >
| > > | > #Events in ASP.NET Server Controls
| > > | >
| > >
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconeventsinwebf
ormsco
| > > | > ntrols.asp?frame=true
| > > | >
| > > | > #Control Execution Lifecycle
| > > | >
| > >
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconControlExecu
tionLi
| > > | > fecycle.asp?frame=true
| > > | >
| > > | > #An Extensive Examination of User Controls
| > > | >
| > >
http://msdn.microsoft.com/library/en-us/dnaspp/html/usercontrols.asp?f
rame=t
| > > | > rue
| > > | >
| > > | > Hope helps. Thanks,
| > > | >
| > > | > Steven Cheng
| > > | > Microsoft Online Support
| > > | >
| > > | > Get Secure! www.microsoft.com/security
| > > | > (This posting is provided "AS IS", with no warranties, and
confers no
| > > | > rights.)
| > > | >
| > > | >
| > > | >
| > > | >
| > > | >
| > > | >
| > > | >
| > > | > --------------------
| > > | > | Thread-Topic: user control - image button only works on
second
select
|
 
S

Steven Cheng[MSFT]

Thanks for your further followup Richard,

Glad that you've got your control working now. Developing custom control is
really a hard work especially when the structure become very complex.
Anyway, if there're any thing we can help later, please feel free to post
here.

Good luck!

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| Message-ID: <[email protected]>
| From: RichB <[email protected]>
| Subject: RE: user control - image button only works on second select
| References: <[email protected]>
| MIME-Version: 1.0
| Content-Transfer-Encoding: 8bit
| Content-Type: text/plain; charset=iso-8859-1; format=flowed
| X-Newsreader: JetBrains Omea Reader 671.6
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| Date: Thu, 22 Sep 2005 03:42:49 -0700
| NNTP-Posting-Host: host86-132-141-218.range86-132.btcentralplus.com
86.132.141.218
| Lines: 1
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:10980
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Steven, Thanks....
|
| I've managed to sort things out by changing my CreateTree() method as
follows:
|
|
| private void CreateTree()
| {
| tree = Page.LoadControl("DynamicTree.ascx") as DynamicTree;
|
| Panel1.Controls.Clear();
| tree.ID = "puc_1";
|
| tree.people = people;
| tree.familytree = familytree;
| tree.multiple = zoom;
| this.Panel1.Controls.Add(tree);
| }
|
| I am still not quite sure why the behaviour exists, however it would
appear
| the ID is critical for the page to find the event to fire on postback.
| This doesn't however really explain why the first set of controls added
work
| OK before and after the second set were added but I am not too concerned
| as all is working now.
|
| Thanks for your assistance.
| Richard
|
|
| Hello Steven Cheng[MSFT],
|
| > Hi Richard,
| >
| > As for the Control's ID, it's very important for our dynanmically
| > created controls since for each control when adding into a certain
| > NamingContainer, the runtime will generate a page wide idenitifier for
| > it according to that control's ID and its naming container's ID. Then,
| > this ID will be used to identify the control in several processing,
| > include VIEWSTATE mapping , postback event mapping (if exist). So
| > when we add dynami control, we must assign a unique control ID for
| > each control instance. Also, this ID must be consistent in each page's
| > request, otherwaise, we'll get many unexpected problems on Control's
| > displaying or postback event handling.
| >
| > Here is a good article discussing on the ASP.NET viewstate in page
| > model:
| >
| > #Understanding ASP.NET View State
| > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnasp
| > p/html /viewstate.asp
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| > --------------------
| > | Thread-Topic: user control - image button only works on second
| > select
| > | thread-index: AcW+3nbKUiwhLRrbSFecodvqzTNnag==
| > | X-WBNR-Posting-Host: 86.132.141.218
| > | From: "=?Utf-8?B?cmljaEI=?=" <[email protected]>
| > | References: <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <#[email protected]>
| > <[email protected]>
| > | Subject: RE: user control - image button only works on second select
| > | Date: Wed, 21 Sep 2005 11:58:33 -0700
| > | Lines: 330
| > | Message-ID: <[email protected]>
| > | MIME-Version: 1.0
| > | Content-Type: text/plain;
| > | charset="Utf-8"
| > | Content-Transfer-Encoding: 8bit
| > | X-Newsreader: Microsoft CDO for Windows 2000
| > | Content-Class: urn:content-classes:message
| > | Importance: normal
| > | Priority: normal
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.webcontrols:10952
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| > |
| > | I have now modified your example and recreated my problem (and found
| > a
| > little
| > | extra in your code which I had missed). Can you explain for me what
| > is
| > | happening...
| > |
| > | Firstly I have added multiple listitems to the dropdown (Show 1,
| > Show2,
| > | Show3). I then ran the app and selected Show 1 and then Show2. On
| > selecting
| > | Show2 I get the error:
| > | 'Multiple controls with the same ID 'puc_1' were found. FindControl
| > requires
| > | that controls have unique IDs. ' hence noticing your line setting
| > the
| > uc.ID
| > | (Sorry I missed this before).
| > |
| > | I then commented out this code and this resulted in a second set of
| > buttons
| > | appearing when I select show2 (after initially selecting show1)
| > which
| > | disappear when I select one of them, leaving the original set of
| > buttons
| > | which work OK.
| > |
| > | Can you explain whether setting the ID is necessary and also what is
| > | happening when I have removed it and how I might solve it in the
| > example....
| > | I can then try and apply this to my own app.
| > |
| > | Thanks, Richard
| > |
| > |
| > |
| > |
| > |
| > |
| > |
| > | "richB" wrote:
| > |
| > | > Steven,
| > | >
| > | > Thanks again, hopefully the following will answer your questions:
| > | >
| > | > 1. test page and uc was fine, although mine is more complex with
| > several
| > | > options in the drop down: option 0 performs hide, option 1 shows
| > button
| > 1,
| > | > option 2 shows button 2, options 3 showns button 1 & 3 etc...I
| > shall
| > try to
| > | > adjust your example and see if I can recreate my problem with
| > this.
| > | >
| > | > 2. I am pretty sure that I am loading the control correctly using
| > my
| > | > CreateTree() method.
| > | >
| > | > 3. The user control also has labels placed within the panel and
| > images
| > | > generated to show the links between the different labels. ( I have
| > tried
| > | > commenting these out and the problem still exists).
| > | >
| > | > 4. The image buttons are all dynamically loaded as per the code
| > after
| > point
| > | > 2 in my last post.
| > | >
| > | > 5. the main page does contain two other linkbutton controls which
| > change the
| > | > scaling of the controls within the user control (except for the
| > image
| > | > buttons) as follws:
| > | >
| > | > private void LinkButton1_Click(object sender, System.EventArgs e)
| > | > {
| > | > zoom = (int)(zoom * 1.25);
| > | > Trace.Write("zoom = " + zoom.ToString() );
| > | > this.Panel1.Controls.Remove(tree);
| > | > this.Session.Add("zoom",zoom);
| > | > CreateTree();
| > | >
| > | > }
| > | >
| > | > Again I have tried commenting out all of the code related to these
| > buttons
| > | > and this makes no difference to my problem. It is however the case
| > that
| > if
| > | > the zoom is selected the image buttons do not work on first click.
| > | >
| > | > As I say I shall try to build a simple example which I can attach
| > to
| > the
| > | > post which demonstrates the same behaviour unless there is
| > anything
| > which you
| > | > can think of which is likely to cause me these problems.
| > | >
| > | > Thanks, Richard
| > | >
| > | >
| > | >
| > | > "Steven Cheng[MSFT]" wrote:
| > | >
| > | > > Hi Richard,
| > | > >
| > | > > Thanks for your detailed response.
| > | > > So does the test page and usercontrol I sent you works as you
| > expected?
| > | > > Also, from your further description, I have some question on the
| > user
| > | > > control, as for the main page and dropdownlist, I don't think
| > there
| > will
| > | > > have any particular problems in them as long as we all Load the
| > Usercontrol
| > | > > correctly each time in Page's Load or Init event. Something I
| > still
| > not
| > | > > sure is about the sub controls in your Usercontrol(which is
| > dynamically
| > | > > loaded), are the controls in the usercontrol (imagebuttons )
| > also
| > | > > dynamically created and added ? Also, seems you mentioned that
| > there're
| > | > > also some other controls (buttons) in addition to the
| > DropDownlist on
| > the
| > | > > main page?
| > | > > If so, the problem will be abit more complex than I have
| > expected.
| > I'll
| > | > > need some further tests.
| > | > >
| > | > > Thanks,
| > | > >
| > | > > Steven Cheng
| > | > > Microsoft Online Support
| > | > >
| > | > > Get Secure! www.microsoft.com/security
| > | > > (This posting is provided "AS IS", with no warranties, and
| > confers no
| > | > > rights.)
| > | > >
| > | > >
| > | > >
| > | > > --------------------
| > | > > | Thread-Topic: user control - image button only works on second
| > select
| > | > > | thread-index: AcW+GdakPHhJiWoOTDCOWHuZhfpqPA==
| > | > > | X-WBNR-Posting-Host: 86.132.141.218
| > | > > | From: "=?Utf-8?B?cmljaEI=?=" <[email protected]>
| > | > > | References:
| > <[email protected]>
| > | > > <[email protected]>
| > | > > | Subject: RE: user control - image button only works on second
| > select
| > | > > | Date: Tue, 20 Sep 2005 12:31:03 -0700
| > | > > | Lines: 240
| > | > > | Message-ID:
| > <[email protected]>
| > | > > | MIME-Version: 1.0
| > | > > | Content-Type: text/plain;
| > | > > | charset="Utf-8"
| > | > > | Content-Transfer-Encoding: 8bit
| > | > > | X-Newsreader: Microsoft CDO for Windows 2000
| > | > > | Content-Class: urn:content-classes:message
| > | > > | Importance: normal
| > | > > | Priority: normal
| > | > > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| > | > > | Newsgroups:
| > microsoft.public.dotnet.framework.aspnet.webcontrols
| > | > > | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| > | > > | Path:
| > TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA03.phx.gbl
| > | > > | Xref: TK2MSFTNGXA01.phx.gbl
| > | > > microsoft.public.dotnet.framework.aspnet.webcontrols:10935
| > | > > | X-Tomcat-NG:
| > microsoft.public.dotnet.framework.aspnet.webcontrols
| > | > > |
| > | > > | Thanks Steven, I have had a look through the articles which
| > you
| > have
| > | > > | referred, and particularly through your example and compared
| > this
| > to my
| > | > > own
| > | > > | code. I have found no fundamental differences. However the
| > following
| > | > > pieces
| > | > > | are different:
| > | > > |
| > | > > | 1. I am using Page.Session to maintain details of the
| > information
| > in
| > | > > order
| > | > > | to recreate the user control. With the CreateTree() method
| > used
| > when I
| > | > > select
| > | > > | from the dropdown (on first and subsequent occasions):
| > | > > |
| > | > > | private void DropDownList1_SelectedIndexChanged(object sender,
| > | > > | System.EventArgs e)
| > | > > | {
| > | > > | this.Panel1.Controls.Clear();
| > | > > | this.Session.Clear();
| > | > > |
| > | > > | if (this.DropDownList1.SelectedValue != "0")
| > | > > | {
| > | > > | int personId =
| > Int32.Parse(this.DropDownList1.SelectedValue);
| > | > > |
| > | > > | int [] parent = GetData.GetParents(personId);
| > | > > |
| > | > > | familytree = new Tree(parent[0]== 0 ? personId :
| > parent[0]);
| > | > > |
| > | > > | people = familytree.GetTreePeople();
| > | > > | this.Session.Add("people",people);
| > | > > | this.Session.Add("zoom",zoom);
| > | > > | this.CreateTree();
| > | > > | this.Panel1.Visible = true;
| > | > > | }
| > | > > | else
| > | > > | {
| > | > > | this.Panel1.Visible = false;
| > | > > | }
| > | > > |
| > | > > |
| > | > > | private void CreateTree()
| > | > > | {
| > | > > | //original code
| > | > > | /*tree = this.LoadControl("DynamicTree.ascx");
| > | > > | ((DynamicTree)tree).people= people;
| > | > > | ((DynamicTree)tree).familytree = familytree;
| > | > > | ((DynamicTree)tree).multiple = zoom;
| > | > > | //tree.EnableViewState = true;
| > | > > | this.Panel1.Controls.Add(tree);*/
| > | > > |
| > | > > | //After post to MSDN
| > | > > | tree = Page.LoadControl("DynamicTree.ascx") as DynamicTree;
| > | > > | tree.people = people;
| > | > > | tree.familytree = familytree;
| > | > > | tree.multiple = zoom;
| > | > > | this.Panel1.Controls.Add(tree);
| > | > > |
| > | > > |
| > | > > | }
| > | > > | 2. I have multiple selections of people within my drop down.
| > Each
| > person
| > | > > has
| > | > > | a number of related people all of which are used to build a
| > tree of
| > | > > | relationships within the user control. Each related person has
| > an
| > | > > information
| > | > > | button which is used to pop-up the window. Page load contains
| > a
| > foreach
| > | > > loop
| > | > > | which creates an ImageButton for each person:
| > | > > |
| > | > > | ImageButton informationButton = new ImageButton();
| > | > > | informationButton.Style["POSITION"] = "absolute";
| > | > > | informationButton.Style["LEFT"] = (int)boxX-15+"px";
| > | > > | informationButton.Style["TOP"] = (int)boxY+"px";
| > | > > | informationButton.ImageUrl = "info.gif";
| > | > > | informationButton.ID = "Info"+ pers.id.ToString();
| > | > > | informationButton.Click += new
| > | > > |
| > System.Web.UI.ImageClickEventHandler(this.informationButton_Click);
| > | > > | Trace.Warn(informationButton.ID.ToString()+ "event handler
| > added");
| > | > > | informationButton.ToolTip = "information";
| > | > > | Panel1.Controls.Add(informationButton);
| > | > > |
| > | > > | private void informationButton_Click(object sender,
| > | > > | System.Web.UI.ImageClickEventArgs e)
| > | > > | {
| > | > > | Trace.Warn("sender: "+((ImageButton)sender).ID.ToString());
| > | > > |
| > | > > |
| > | > > | string sUrl =
| > | > > "PersonInformation.aspx?button="+((ImageButton)sender).ID;
| > | > > |
| > | > > | string sScript = "<script language =javascript> ";
| > | > > |
| > | > > | sScript += "window.open('" + sUrl +
| > | > > |
| > | > >
| > "',null,'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resiz
| > able=0
| > | > > ,width=800,height=600,left=100,top=100');";
| > | > > |
| > | > > | sScript += "</script> ";
| > | > > |
| > | > > | Page.RegisterStartupScript("newpage",sScript);
| > | > > |
| > | > > | }
| > | > > |
| > | > > | I think that this is all the relevant code, is there any
| > reason
| > which
| > | > > either
| > | > > | of these two differences would cause the results that I have
| > described.
| > | > > I.e.
| > | > > | the image buttons working on first selecting a person form the
| > drop
| > down,
| > | > > but
| > | > > | only working on selecting the button twice. (Interestingly I
| > have
| > just
| > | > > | discovered that if I select the default option â�index 0 ?
¢â?and then
| > | > > select
| > | > > | another dropdown item then the button works on first press)
| > | > > |
| > | > > | Any further help greatly appreciated.
| > | > > |
| > | > > | Thanks,
| > | > > | Richard.
| > | > > |
| > | > > |
| > | > > | "Steven Cheng[MSFT]" wrote:
| > | > > |
| > | > > | > Hi Richard,
| > | > > | >
| > | > > | > Welcome to ASPNET newsgroup.
| > | > > | > Regarding on the problem in creating dynamic webuser
| > control,
| > here are
| > | > > some
| > | > > | > of my understanding and suggestions:
| > | > > | >
| > | > > | > In the ASP.NET webform page model, all the controls are
| > created
| > in each
| > | > > | > page request, not only the initial one, but also all the
| > sequential
| > | > > post
| > | > > | > back requests. Also, for dynamic created webcontrols, we
| > need to
| > add
| > | > > them
| > | > > | > into the parent (container) control's child collection in
| > each
| > request,
| > | > > not
| > | > > | > only in a certain postback event so as to make sure they'll
| > appear in
| > | > > the
| > | > > | > page correctly. So generally, Page's Init or Load event is
| > the
| > most
| > | > > proper
| > | > > | > place for creating dynamic web server/user controls. Here
| > are
| > some tech
| > | > > | > articles on creating dynamic webcontrols;
| > | > > | >
| > | > > | > #HOW TO: Dynamically Create Controls in ASP.NET by Using
| > Visual
| > C# .NET
| > | > > | >
| > http://support.microsoft.com/default.aspx?scid=kb;en-us;317794
| > | > > | >
| > | > > | > #Creating Dynamic Data Entry User Interfaces
| > | > > | >
| > | > >
| > http://msdn.microsoft.com/library/en-us/dnaspp/html/dynamicui.asp?fram
| > e=true
| > | > > | >
| > | > > | > So for your scenario, we'd suggest you put the dynamic web
| > user
| > | > > | > control(ascx) 's creating code in a function and call this
| > function in
| > | > > | > Page's Init or Load event according to a certain
| > variable's
| > value(
| > | > > you
| > | > > | > can consider using ViewState or a <input type="hidden" ...>
| > element to
| > | > > | > store this value). Also, to make it eaiser, I've made a
| > simple
| > demo
| > | > > page
| > | > > | > which dynamically display a UserControl according to a
| > DropDownlist's
| > | > > | > selected value on the page . I've attached the code in the
| > attachement
| > | > > of
| > | > > | > this message, you can get it through OE. (please let me know
| > if
| > you
| > | > > have
| > | > > | > any problem accessing them).
| > | > > | >
| > | > > | > In addition ,here are some other good articles on
| > Usercontrol and
| > | > > asp.net
| > | > > | > page model, event lifecycle:
| > | > > | >
| > | > > | >
| > | > > | > #Events in ASP.NET Server Controls
| > | > > | >
| > | > >
| > http://msdn.microsoft.com/library/en-us/cpguide/html/cpconeventsinwebf
| > ormsco
| > | > > | > ntrols.asp?frame=true
| > | > > | >
| > | > > | > #Control Execution Lifecycle
| > | > > | >
| > | > >
| > http://msdn.microsoft.com/library/en-us/cpguide/html/cpconControlExecu
| > tionLi
| > | > > | > fecycle.asp?frame=true
| > | > > | >
| > | > > | > #An Extensive Examination of User Controls
| > | > > | >
| > | > >
| > http://msdn.microsoft.com/library/en-us/dnaspp/html/usercontrols.asp?f
| > rame=t
| > | > > | > rue
| > | > > | >
| > | > > | > Hope helps. Thanks,
| > | > > | >
| > | > > | > Steven Cheng
| > | > > | > Microsoft Online Support
| > | > > | >
| > | > > | > Get Secure! www.microsoft.com/security
| > | > > | > (This posting is provided "AS IS", with no warranties, and
| > confers no
| > | > > | > rights.)
| > | > > | >
| > | > > | >
| > | > > | >
| > | > > | >
| > | > > | >
| > | > > | >
| > | > > | >
| > | > > | > --------------------
| > | > > | > | Thread-Topic: user control - image button only works on
| > second
| > select
| > |

|
|
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top