dynamic button - does not go to handler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • greg

    #1

    dynamic button - does not go to handler

    Hi

    here is what I am doing - building a table dynamically

    HtmlTableRow objRow = new HtmlTableRow();

    tblStuff.Rows.A dd(objRow);

    HtmlTableCell objCellMain1 = new HtmlTableCell() ;

    ImageButton btn1 = new ImageButton();

    objCellMain1.Co ntrols.Add(btn1 );

    btn1.Click += new
    System.Web.UI.I mageClickEventH andler(this.cha ngeBtn_Click);

    btn1.AlternateT ext = "333";

    objRow.Cells.Ad d(objCellMain1) ;



    and of course I have that method defined



    private void changeBtn_Click (object sender,
    System.Web.UI.I mageClickEventA rgs e)

    {

    // get id

    ImageButton btn = (ImageButton)se nder;

    String vid = btn.AlternateTe xt;

    etc etc.



    IT shows on the screen fine

    But when I click on the button it does not go into changeBtn_Click

    !!!!! HELP

    Thanks

    GSL


  • Kevin Kenny

    #2
    Re: dynamic button - does not go to handler

    greg wrote:
    [color=blue]
    >Hi
    >
    >here is what I am doing - building a table dynamically
    >
    >HtmlTableRow objRow = new HtmlTableRow();
    >
    >tblStuff.Rows. Add(objRow);
    >
    >HtmlTableCel l objCellMain1 = new HtmlTableCell() ;
    >
    >ImageButton btn1 = new ImageButton();
    >
    >objCellMain1.C ontrols.Add(btn 1);
    >
    >btn1.Click += new
    >System.Web.UI. ImageClickEvent Handler(this.ch angeBtn_Click);
    >
    >btn1.Alternate Text = "333";
    >
    >objRow.Cells.A dd(objCellMain1 );
    >
    >
    >
    >and of course I have that method defined
    >
    >
    >
    >private void changeBtn_Click (object sender,
    >System.Web.UI. ImageClickEvent Args e)
    >
    >{
    >
    >// get id
    >
    >ImageButton btn = (ImageButton)se nder;
    >
    >String vid = btn.AlternateTe xt;
    >
    >etc etc.
    >
    >
    >
    >IT shows on the screen fine
    >
    >But when I click on the button it does not go into changeBtn_Click
    >
    >!!!!! HELP
    >
    >Thanks
    >
    >GSL
    >
    >
    >[/color]
    Greg,

    I suspect you are generating the controls in something like:
    private void Page_Load(objec t sender, System.EventArg s e)
    {
    if(!Page.IsPost Back)
    {
    // Create controls
    CreateControls( );
    }
    }

    Dynamic controls need to be recreated *every* time the page is created
    to allow ASP.NET to restore view state, postback data and event wireup
    to the dynamically created controls. So remember to recreate them on the
    postback as well.

    HTH
    Kev


    Comment

    • Tommy

      #3
      Re: dynamic button - does not go to handler

      Because your ImageButton is created dynamically,

      ImageButton btn1 = new ImageButton();

      you need to recreate the button on a postback in order for the button event to fire.

      Tommy,

      "greg" <gregl@insaucti onNOSPAM.com> wrote in message news:<u3x34#vXD HA.1896@TK2MSFT NGP12.phx.gbl>. ..[color=blue]
      > Hi
      >
      > here is what I am doing - building a table dynamically
      >
      > HtmlTableRow objRow = new HtmlTableRow();
      >
      > tblStuff.Rows.A dd(objRow);
      >
      > HtmlTableCell objCellMain1 = new HtmlTableCell() ;
      >
      > ImageButton btn1 = new ImageButton();
      >
      > objCellMain1.Co ntrols.Add(btn1 );
      >
      > btn1.Click += new
      > System.Web.UI.I mageClickEventH andler(this.cha ngeBtn_Click);
      >
      > btn1.AlternateT ext = "333";
      >
      > objRow.Cells.Ad d(objCellMain1) ;
      >
      >
      >
      > and of course I have that method defined
      >
      >
      >
      > private void changeBtn_Click (object sender,
      > System.Web.UI.I mageClickEventA rgs e)
      >
      > {
      >
      > // get id
      >
      > ImageButton btn = (ImageButton)se nder;
      >
      > String vid = btn.AlternateTe xt;
      >
      > etc etc.
      >
      >
      >
      > IT shows on the screen fine
      >
      > But when I click on the button it does not go into changeBtn_Click
      >
      > !!!!! HELP
      >
      > Thanks
      >
      > GSL[/color]

      Comment

      Working...