Re: Dynamic WebControl events...

Mike Andrews <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.web
Message-ID <[email protected]>
We have an UpdatePanel and that contains the Repeater.
It's becoming a real problem for this page we need to make.


On 2/14/07, Mark Brackett <[email protected]> wrote:
>
> Perhaps try without the UpdatePanel? I don't see anything that jumps out
> at me as being a problem with normal ASP.NET...but, I haven't really
> worked with Atlas.
>
> --MB
>
> -----Original Message-----
> From: Discussion of building .NET applications targeted for the Web
> [mailto:[email protected]] On Behalf Of Mike Andrews
> Sent: Wednesday, February 14, 2007 1:06 PM
> To: [email protected]
> Subject: Re: [DOTNET-WEB] Dynamic WebControl events...
>
> Hey guys...
> Thank you so much for the responses...
> However, after some further research into this problem here's what's
> going
> on.  Perhaps one of you know what the issue could be.  We are using a
> repeater (actually 3 nested repeaters) to dynamically build some forms.
> I've been able to deduce these things with dynamic check boxes contained
> within repeaters:
>
> 1)  When you check a checkbox, all previously checked checkboxes fire
> their
> CheckedChanged event.
> 2)  When you uncheck a checkbox, all previously checked checkboxes fire
> their CheckedChanged event and the one unchecked does not fire the
> CheckedChanged event.
>
> Any assistance would be most appreciated.
>
> Thanks,
> Mike
>
> Here's the markup code for the sample page:
>
> <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test2.aspx.cs"
> Inherits="Test2" %>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
> http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" >
> <head runat="server">
>    <title>Untitled Page</title>
> </head>
> <body>
>    <form id="form1" runat="server">
>    <div>
>        <asp:ScriptManager ID="scriptManater"
> runat="server"></asp:ScriptManager>
>        <asp:UpdatePanel ID="pnlUpdate" runat="server">
>            <ContentTemplate>
>                <asp:Repeater ID="rpt1" runat="server"
> EnableViewState="false">
>                    <ItemTemplate>
>                        <asp:PlaceHolder ID="ph1"
> runat="server"></asp:PlaceHolder>
>                    </ItemTemplate>
>                </asp:Repeater>
>            </ContentTemplate>
>        </asp:UpdatePanel>
>    </div>
>    </form>
> </body>
> </html>
>
> Here's the codebehind for the sample page:
>
> using System;
> using System.Data;
> using System.Configuration;
> using System.Collections;
> using System.Web;
> using System.Web.Security;
> using System.Web.UI;
> using System.Web.UI.WebControls;
> using System.Web.UI.WebControls.WebParts;
> using System.Web.UI.HtmlControls;
> using System.Collections.Generic;
>
> public partial class Test2 : System.Web.UI.Page {
>    protected void Page_Load(object sender, EventArgs e) {
>
>        List<System.Web.UI.Control> controls = new List<Control>();
>
>        CheckBox cb = null;
>        cb = new CheckBox();
>        cb.ID = "test";
>        cb.Text = "Click Here!";
>        cb.AutoPostBack = true;
>        cb.CheckedChanged += new EventHandler(cb_CheckedChanged);
>        controls.Add(cb);
>
>        cb = new CheckBox();
>        cb.ID = "test2";
>        cb.Text = "Click Here Again!";
>        cb.AutoPostBack = true;
>        cb.CheckedChanged += new EventHandler(cb_CheckedChanged);
>        controls.Add(cb);
>
>        rpt1.ItemCreated += new
> RepeaterItemEventHandler(rpt1_ItemCreated);
>        rpt1.DataSource = controls;
>        rpt1.DataBind();
>
>    }
>
>    void rpt1_ItemCreated(object sender, RepeaterItemEventArgs e) {
>        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
> ListItemType.AlternatingItem) {
>            PlaceHolder ph = (PlaceHolder)e.Item.FindControl("ph1");
>            CheckBox cb = (CheckBox)e.Item.DataItem;
>            ph.Controls.Add(cb);
>        }
>    }
>
>    void cb_CheckedChanged(object sender, EventArgs e) {
>
>    }
> }
>
>
>
> On 2/14/07, Mark Brackett <[email protected]> wrote:
> >
> > I agree with most of this, but not about the Checkbox needing to be
> > "created even before the load event".
> >
> > The Page class (at least in ASP.NET 2.0) actually attempts to load
> post
> > data TWICE (presumably for issues such as this). The first time is
> after
> > the InitComplete event, but before PreLoad (trace shows this as
> > Begin/End ProcessPostData). The second time is after the Load event,
> > which loads any post data that couldn't be loaded on the first pass
> > (trace shows this as Begin/End ProcessPostData Second Try"). After the
> > SECOND run is when the IPostBackDataHandler.RaisePostChangedEvent gets
> > called (aka CheckedChanged for the Checkbox) (trace show this as
> > Begin/End RaiseChangedEvents).
> >
> > A quick test also proves this, as I get the CheckedChanged event with
> a
> > Checkbox created in Page_Load.
> >
> > Of course, the safest and easiest route is to create dynamic controls
> in
> > PreInit to make sure you catch all aspects of the Page lifecycle.
> > Unfortunately, this won't work out for the OP's code because he's
> using
> > the Page.Form property (not a control reference) which doesn't get
> > populated until AFTER PreInit. So, in that case, Init is the earliest
> it
> > can be added (but it'd probably be easiest to just change it to
> > reference the form control, so this.form1.Controls.Add).
> >
> > --MB
> >
> > -----Original Message-----
> > From: Discussion of building .NET applications targeted for the Web
> > [mailto:[email protected]] On Behalf Of Ryan Heath
> > Sent: Wednesday, February 14, 2007 11:39 AM
> > To: [email protected]
> > Subject: Re: [DOTNET-WEB] Dynamic WebControl events...
> >
> > To backup Paul, if you add a control in the PreRender event, you will
> > not receive its events at postback.
> >
> > Simply because, at postback, the time the control events are raised,
> > the methods that will handle the control events are not bounded yet,
> > since you would have bound the method at the PreRender event ...
> >
> > protected void Page_PreRender(object sender, EventArgs e)
> > {
> >       CheckBox cb = new CheckBox();
> >       cb.ID = "test";
> >       cb.Text = "Click Here!";
> >       cb.CheckedChanged += new EventHandler(cb_CheckedChanged);
> >       this.Form.Controls.Add(cb);
> > }
> >
> > void cb_CheckedChanged(object sender, EventArgs e)
> > {
> > //is never called!
> > }
> >
> > The order of page events are, partially,
> > - init
> > - load
> > - change evts
> > - click evts
> > - prerender
> >
> > Normally, creating and binding a control is ok with in the load event,
> > but the Checkbox, in particular, needs to be created even before the
> > load event...
> >
> > // Ryan
> >
> > On 2/14/07, Efran Cobisi <[email protected]> wrote:
> > > This is incorrect.
> > > Control's life cycle begins after they have been added to their
> parent
> > > controls; so, even if you add a Control to another Controls
> collection
> > > in the Page PreRender event, it will still fire its postback
> events...
> > > As Stacey pointed out, setting the AutoPostback property to true
> will
> > > solve this problem.
> > >
> > > HTH,
> > >
> > > Efran Cobisi
> > > http://www.cobisi.com
> > >
> > > Paul van Brenk wrote:
> > > > You should create the checkbox in the Init event... on the
> postback
> > the
> > > > checkbox is created after the events are handled (ie. Too late).
> > > >
> > > > Paul
> > > >
> > > > -----Original Message-----
> > > > From: Discussion of building .NET applications targeted for the
> Web
> > > > [mailto:[email protected]] On Behalf Of Mike Andrews
> > > > Sent: Wednesday, February 14, 2007 16:51
> > > > To: [email protected]
> > > > Subject: [DOTNET-WEB] Dynamic WebControl events...
> > > >
> > > > Guys,
> > > >
> > > > I'm having problems getting an aspx page to process an event for a
> > > > control,
> > > > namely a checkbox, that was dynamically created and added to the
> > form.
> > > > Any suggestions?
> > > >
> > > > Thanks,
> > > > Mike
> > > >
> > > > Here's a sample of the code:
> > > >
> > > >     protected void Page_Load(object sender, EventArgs e) {
> > > >
> > > >         CheckBox cb = new CheckBox();
> > > >         cb.ID = "test";
> > > >         cb.Text = "Click Here!";
> > > >         cb.CheckedChanged += new EventHandler(cb_CheckedChanged);
> > > >         this.Form.Controls.Add(cb);
> > > >
> > > >     }
> > > >
> > > >     void cb_CheckedChanged(object sender, EventArgs e) {
> > > >         //Do something here
> > > >     }
> > > >
> > > > ===================================
> > > > This list is hosted by DevelopMentor(r)  http://www.develop.com
> > > >
> > > > View archives and manage your subscription(s) at
> > > > http://discuss.develop.com
> > > >
> > > > ===================================
> > > > This list is hosted by DevelopMentor(r)  http://www.develop.com
> > > >
> > > > View archives and manage your subscription(s) at
> > http://discuss.develop.com
> > > >
> > >
> > > ===================================
> > > This list is hosted by DevelopMentor(r)  http://www.develop.com
> > >
> > > View archives and manage your subscription(s) at
> > http://discuss.develop.com
> > >
> >
> > ===================================
> > This list is hosted by DevelopMentor(r)  http://www.develop.com
> >
> > View archives and manage your subscription(s) at
> > http://discuss.develop.com
> >
> > ===================================
> > This list is hosted by DevelopMentor(r)  http://www.develop.com
> >
> > View archives and manage your subscription(s) at
> > http://discuss.develop.com
> >
>
> ===================================
> This list is hosted by DevelopMentor(r)  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
>
> ===================================
> This list is hosted by DevelopMentor(r)  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
>

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.