Web User Control Event prob.

Discussion in 'ASP .Net' started by Big D, Jan 2, 2004.

  1. Big D

    Big D Guest

    I have a class that inherits from System.Web.Ui.Usercontrol... all is
    basically is is a button which is created progammatically such as:

    public abstract class BaseFrontEndEditor : System.Web.UI.UserControl
    {
    protected Button btnEdit = new Button();

    private void Page_Load(object sender, System.EventArgs e)
    {

    if( ! this.IsPostBack )
    {
    btnEdit.Text="Edit";
    btnEdit.ID = "btnEdit";
    this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
    }
    }

    The button is then added to the class's control collection.

    I created another usercontrol that inherrits from this class, and placed it
    on a page. This works, and when I view the aspx page the control is on, I
    see the button. However, clicking the button never fires the event handler
    ("btnEdit_Click"). I have tried adding the event handler in the controls
    "initializeComponent", as well as immediately after it is added (as seen
    above). I have placed a simple Response.Write("hello") in the btnEdit_Click
    function and nothing happens when the button is clicked. In fact, even the
    button disappears. What's happening?

    Thanks for any help!

    -D
     
    Big D, Jan 2, 2004
    #1
    1. Advertising

  2. Big D

    Marina Guest

    You need to add the button every time the control loads - not just the first
    time. I am guess you are checking the IsPostBack property and only doing it
    the first time.

    Please remember that HTTP is a stateless protocol. The page and all tis
    contents are recreated every time there is a request. So if you only add
    the button if the request is not a postback - after the button is clicked,
    it is not created. And since it doesn't exist, there is no event to fire for
    it.

    "Big D" <> wrote in message
    news:...
    > I have a class that inherits from System.Web.Ui.Usercontrol... all is
    > basically is is a button which is created progammatically such as:
    >
    > public abstract class BaseFrontEndEditor : System.Web.UI.UserControl
    > {
    > protected Button btnEdit = new Button();
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    >
    > if( ! this.IsPostBack )
    > {
    > btnEdit.Text="Edit";
    > btnEdit.ID = "btnEdit";
    > this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
    > }
    > }
    >
    > The button is then added to the class's control collection.
    >
    > I created another usercontrol that inherrits from this class, and placed

    it
    > on a page. This works, and when I view the aspx page the control is on, I
    > see the button. However, clicking the button never fires the event

    handler
    > ("btnEdit_Click"). I have tried adding the event handler in the controls
    > "initializeComponent", as well as immediately after it is added (as seen
    > above). I have placed a simple Response.Write("hello") in the

    btnEdit_Click
    > function and nothing happens when the button is clicked. In fact, even

    the
    > button disappears. What's happening?
    >
    > Thanks for any help!
    >
    > -D
    >
    >
     
    Marina, Jan 2, 2004
    #2
    1. Advertising

  3. Big D

    Big D Guest

    Thanks, however it's still not working. You were, of course, right about
    the stateless nature, so it is no longer only being adding if not postback.
    The button is now still there after postback, but it still does not fire the
    event.

    I've updated it a bit now, and it works as follows

    onInit : The button is created and its handler is added. I am specifying a
    unique id for the button, and the id is the same after postback.

    onPreRender: The control is added to the page.

    Clicking the button has no effect! Why is this?

    -D
    "Marina" <> wrote in message
    news:...
    > You need to add the button every time the control loads - not just the

    first
    > time. I am guess you are checking the IsPostBack property and only doing

    it
    > the first time.
    >
    > Please remember that HTTP is a stateless protocol. The page and all tis
    > contents are recreated every time there is a request. So if you only add
    > the button if the request is not a postback - after the button is clicked,
    > it is not created. And since it doesn't exist, there is no event to fire

    for
    > it.
    >
    > "Big D" <> wrote in message
    > news:...
    > > I have a class that inherits from System.Web.Ui.Usercontrol... all is
    > > basically is is a button which is created progammatically such as:
    > >
    > > public abstract class BaseFrontEndEditor : System.Web.UI.UserControl
    > > {
    > > protected Button btnEdit = new Button();
    > >
    > > private void Page_Load(object sender, System.EventArgs e)
    > > {
    > >
    > > if( ! this.IsPostBack )
    > > {
    > > btnEdit.Text="Edit";
    > > btnEdit.ID = "btnEdit";
    > > this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
    > > }
    > > }
    > >
    > > The button is then added to the class's control collection.
    > >
    > > I created another usercontrol that inherrits from this class, and placed

    > it
    > > on a page. This works, and when I view the aspx page the control is on,

    I
    > > see the button. However, clicking the button never fires the event

    > handler
    > > ("btnEdit_Click"). I have tried adding the event handler in the

    controls
    > > "initializeComponent", as well as immediately after it is added (as seen
    > > above). I have placed a simple Response.Write("hello") in the

    > btnEdit_Click
    > > function and nothing happens when the button is clicked. In fact, even

    > the
    > > button disappears. What's happening?
    > >
    > > Thanks for any help!
    > >
    > > -D
    > >
    > >

    >
    >
     
    Big D, Jan 2, 2004
    #3
  4. Big D

    Marina Guest

    On prerender is too late to add it to the page. The event handler would
    have already fired by this point. So the second time the page loads - the
    point at which the event would have fired is gone, and then you are just
    adding the button to the page before the HTML is sent to the client.

    You need to add the button to the page in page_init or page_load

    "Big D" <> wrote in message
    news:%...
    > Thanks, however it's still not working. You were, of course, right about
    > the stateless nature, so it is no longer only being adding if not

    postback.
    > The button is now still there after postback, but it still does not fire

    the
    > event.
    >
    > I've updated it a bit now, and it works as follows
    >
    > onInit : The button is created and its handler is added. I am specifying

    a
    > unique id for the button, and the id is the same after postback.
    >
    > onPreRender: The control is added to the page.
    >
    > Clicking the button has no effect! Why is this?
    >
    > -D
    > "Marina" <> wrote in message
    > news:...
    > > You need to add the button every time the control loads - not just the

    > first
    > > time. I am guess you are checking the IsPostBack property and only doing

    > it
    > > the first time.
    > >
    > > Please remember that HTTP is a stateless protocol. The page and all tis
    > > contents are recreated every time there is a request. So if you only

    add
    > > the button if the request is not a postback - after the button is

    clicked,
    > > it is not created. And since it doesn't exist, there is no event to fire

    > for
    > > it.
    > >
    > > "Big D" <> wrote in message
    > > news:...
    > > > I have a class that inherits from System.Web.Ui.Usercontrol... all is
    > > > basically is is a button which is created progammatically such as:
    > > >
    > > > public abstract class BaseFrontEndEditor : System.Web.UI.UserControl
    > > > {
    > > > protected Button btnEdit = new Button();
    > > >
    > > > private void Page_Load(object sender, System.EventArgs e)
    > > > {
    > > >
    > > > if( ! this.IsPostBack )
    > > > {
    > > > btnEdit.Text="Edit";
    > > > btnEdit.ID = "btnEdit";
    > > > this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
    > > > }
    > > > }
    > > >
    > > > The button is then added to the class's control collection.
    > > >
    > > > I created another usercontrol that inherrits from this class, and

    placed
    > > it
    > > > on a page. This works, and when I view the aspx page the control is

    on,
    > I
    > > > see the button. However, clicking the button never fires the event

    > > handler
    > > > ("btnEdit_Click"). I have tried adding the event handler in the

    > controls
    > > > "initializeComponent", as well as immediately after it is added (as

    seen
    > > > above). I have placed a simple Response.Write("hello") in the

    > > btnEdit_Click
    > > > function and nothing happens when the button is clicked. In fact,

    even
    > > the
    > > > button disappears. What's happening?
    > > >
    > > > Thanks for any help!
    > > >
    > > > -D
    > > >
    > > >

    > >
    > >

    >
    >
     
    Marina, Jan 2, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vatech1993
    Replies:
    4
    Views:
    5,719
    =?Utf-8?B?U8Opcmdpbw==?=
    Dec 11, 2004
  2. ABC
    Replies:
    1
    Views:
    2,673
    Larry
    Oct 4, 2005
  3. ABC
    Replies:
    0
    Views:
    504
  4. Carl Howarth

    User Control Prob

    Carl Howarth, Jul 16, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    95
    Carl Howarth
    Jul 16, 2003
  5. ABC
    Replies:
    0
    Views:
    225
Loading...

Share This Page