RE: user control - image button only works on second select
- From: stcheng@xxxxxxxxxxxxxxxxxxxx (Steven Cheng[MSFT])
- Date: Wed, 21 Sep 2005 08:33:35 GMT
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=?=" <richb@xxxxxxxxxxxxxxxx>
| References: <371E2BFA-4A95-49FA-97F3-749D0D6D13B4@xxxxxxxxxxxxx>
<JtWtUgYvFHA.780@xxxxxxxxxxxxxxxxxxxxx>
| Subject: RE: user control - image button only works on second select
| Date: Tue, 20 Sep 2005 12:31:03 -0700
| Lines: 240
| Message-ID: <4A4DCA40-3876-4A0E-BF5D-DE459D6EDD8C@xxxxxxxxxxxxx>
| 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=?=" <richb@xxxxxxxxxxxxxxxx>
| > | Subject: user control - image button only works on second select
| > | Date: Mon, 19 Sep 2005 03:56:04 -0700
| > | Lines: 39
| > | Message-ID: <371E2BFA-4A95-49FA-97F3-749D0D6D13B4@xxxxxxxxxxxxx>
| > | 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
| > |
|
.
- Follow-Ups:
- References:
- Prev by Date: Re: Repeater Control Problem
- Next by Date: Re: How we can creat this?
- Previous by thread: RE: user control - image button only works on second select
- Next by thread: RE: user control - image button only works on second select
- Index(es):
Relevant Pages
|