1 Replies - 1399 Views - Last Post: 19 June 2013 - 12:19 PM Rate Topic: -----

#1 jace75  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 11-June 13

Passing dynamically constructed asp HyperLink - Click event miss-fire

Posted 15 June 2013 - 06:18 PM

I have an asp Table element. This element is populated primarily through partial postback using an update panel. When an event is triggered, an appropriate set of values is passed into a factory class, which in turn constructs a series of TableRow objects based on these values and adds them to the table. One of these row objects contains an asp HyperLink object.

           //*****************************
           // View Link
           //*****************************
           TableCell link = new TableCell();
           LinkButton articleLink = new LinkButton();
           articleLink.Text = "View";
           articleLink.OnClientClick = "GetFullArticle(" + a.GetID() + ")";
           link.Controls.Add(articleLink);



The goal: this link must trigger the GetFullArticle(int id) method in my default.aspx.cs file.
The problem: it does not.

What I know: My web form does populate with data, the article link does appear, and it does contain the correct a.GetID value.

What I know: When clicked, a postback is initiated(breaking my ajax sequence) and my form is repopulated based on a set of values which was saved in Session. Nothing effectively is changed. If the GetFullArticle event were fired, it would not.

What I don't know: where is my app looking for GetFullArticle when the onclick event is passed into the web form from factory? It should look for this event in default.aspx.cs. If this isnt the case, how do I find out where it's going?

What I don't know: when a link is created in the aspx.cs file, is it registered differently than when registered in a normal cs? Would it make a difference?

If anyone has encountered this issue before, or if I'm making an amateur mistake, please help me back on the right path! B)

Is This A Good Question/Topic? 0
  • +

Replies To: Passing dynamically constructed asp HyperLink - Click event miss-fire

#2 jace75  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 11-June 13

Re: Passing dynamically constructed asp HyperLink - Click event miss-fire

Posted 19 June 2013 - 12:19 PM

Solved! The problem was in my understanding of how dynamically created controls function. The click event of the dynamically created link button must be passed a reference to another event handler like so...

articleLink.Command += new CommandEventHandler(ViewArticle);
articleLink.CommandArgument = a.GetID().ToString();



...

//****************************************************************
// Event Handler
//****************************************************************


public void ViewArticle(object sender, CommandEventArgs e) {

//Light em up

}



Not over yet! Because the Control class collection is added to view state before the page load, all dynamically created controls must be built in the page init...

protected void Page_Init(object sender, EventArgs e) {

//Add controls to page, and dont take "no" for an answer!

}



also, I should note that all controls need to be assigned a unique ID. This is an especially delicate operation for multiple instances of the same control...

foreach(Article a in allArticles){
            //*****************************
            // Title
            //*****************************
           
            //Parameters parameters...

            articleLink.ID = "Lnk_Article" + a.GetID();
            
            //And more parameters...

}



So why go through all that trouble when Microsoft can give you a rigid version of the same thing with a Drag-n-Drop Gui?

#1 Flexibility is power. The more dynamic and intuitive an application can become, the more easily new features are added, and changes made.

#2 If you're a developer, you don't need a reason to code, all you need is an enabler B)

Happy coding!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1