Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic created controls not firing event handlers

Status
Not open for further replies.

simonchristieis

Programmer
Jan 10, 2002
1,144
GB
I am creating a linkbutton using xml / xslt.

However, when the page renders the method attached to the event handler doesn't fire.

I parse the controls into a placeholder on page init, and can see the controls within the trace.

I am using master pages.

Any ideas ?

Thanks in advance

Simon
 
1st code is always required for debugging a problem.

simonchristieis said:
I am creating a linkbutton using xml / xslt.
I'm pretty sure this won't work. xml/xslt is just text. a LinkButton is a .net object. you need to create the button in the Init event and hook up an event handler.
Code:
protected override void Init(EventArgs e)
{
   LinkButton lb = new LinkButton();
   // set lb properties
   lb.Click += new EventHandler(LinkButton_Click);
   this.Form.Controls.Add(lb);
}

void LinkButton_Click(object sender, EventArgs e)
{
   //process event here
}
note: I don't check for postback. because the control is created dynamically, I must create the control everytime

if you want to create markup via xml/xslt you'll need to have the generated button call a webservice and minipulate the gui on the client.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 

Here is the control creation text from the xslt.

<snipped>
Code:
<asp:LinkButton ID="Test{position()}" runat="server" OnClick="DynamicButtonHandler">Proof</asp:LinkButton>

The xml / xslt is parsed using:
Code:
// oWriter contains the transformed markup
Control ctrl = Page.ParseControl(oWriter.ToString());
plcResults.Controls.Add(ctrl);

The control has been created, cos I can see it in the trace

Code:
ctl00$MainContentPlaceHolder$Test1 System.Web.UI.WebControls.LinkButton 124 0 0

However, when I click the button, the click event is not fired.

Any ideas anyone ?
 
Code:
<asp:LinkButton ID="Test{position()}" runat="server" >Proof</asp:LinkButton>

Code:
LinkButton ctrl = (LinkButton)Page.ParseControl(oWriter.ToString());
ctrl.Click += new EventHandler(LinkButton_Click);
plcResults.Controls.Add(ctrl);

void LinkButton_Click(object sender, EventArgs e)
{
   //process event here
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
ok - fixed it, after the controls are parsed, loop through the controls and add the events handlers.

Code:
Control ctrl = Page.ParseControl(oWriter.ToString());
plcResults.Controls.Add(ctrl);

foreach (Control ctl in plcResults.Controls)
{
 for (int i = 0; i < ctl.Controls.Count; i++)
  {
   if (ctl.Controls[i] is LinkButton)
    {
     LinkButton dynButton = (LinkButton)ctl.Controls[i];
      dynButton.Click += new System.EventHandler(this.DynamicButtonHandler);
     }
   }
}


Thanks jmeckley,

I was under the impression that the event handlers should be attached to the control as the controls were added.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top