Monday, March 26, 2012

Dyanmically created linkbuttons, within UpdatePanel, is not working

Hello Folks,

I am updating a placeholder, to create some form controls, from my code behind.

I am successful. Now, I want to create a linkbutton, next to each form control i create, that when clicked on, go to the same generic event and read the control's ID (passed by the linkbutton somehow) and then I will do the logic to remove the control from the placeholder. Right now it creates the linkbuttons, but to ensure i don't have duplicate ID's, I add a unique int.tostring on the end of the ID of the linkbuttons. And I added the handler that should send all clicks, from all the linkbuttons to the same event. But I put a breakpoint in that event and when I click any of the linkbuttons, it just refreshes and no change to the page and the breakpoint in the event is never reached. Please let me know if you have any ideas, questions.. a portion of my code, with notes, below...

I am able to create all form elements except buttons, link buttons, etc.

I think the link button creation should be as simple as:

victorylinkBTN.ID ="victorylinkBTN" & VictoryReplicantCount.ToString

AddHandler victorylinkBTN.Click,AddressOf victorylinkBTN_Click

DCP.Controls.Add(victorylinkBTN)

victorylinkBTN.Text ="Del"

Dim vicEventAsNew System.EventArgs()

Is that right? It does create the linkbuttons, but the click fails. It submits, but nothing happens, it just refreshes with nothing changed, and doesn't land on my breakpoint, in the sub below.

Sub victorylinkBTN_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)

Dim linkbutAs LinkButton =CType(sender, LinkButton)

Dim linkbutIDAsString = linkbut.ID.ToString

'do more work……

EndSub

If you have any idea what I am doing wrong, please let me know.

Also, if you have experience with update panels, if I make the update panel conditional, then how would I set each of these linkbuttons to have trigger rights to the related update panel?

Again, thanks for any help ...

Folks,

I have found a cheesy way to do this, by adding javascript on each linkButton created that inserts the linkButton ID into a hidden field's value on the page and then on that linkButton submit, in pageLoad, I read the value of the hidden field to decide to perform the action.

This works, and would take some serious time to explain. Please feel free to email me via the forum contact ability, if you want details.

I am leaving this open, hoping someone in the asp.net team will have a better solution.


Since you dynamically add thelink buttons, you should add it to the PlaceHolder in the Page_Load event every time instead of adding it just the page first load. Because when you only add it the first time the page load, after post back, thelink buttonswill disappear. And itdoesn't land on your breakpoint invictorylinkBTN_Click,There is non victorylinkBTN when your postback.

Try this:

publicpartialclassDefault2 : System.Web.UI.Page

{

protectedvoid Page_Load(object sender,EventArgs e)

{

AddControl(PlaceHolder_ContentRight,"~/WebUserControl.ascx",true);

}

privatevoid AddControl(PlaceHolder PlaceHolder,string ControlPath,bool Clear)

{

Control Control_ToAdd;

Control_ToAdd = LoadControl(ControlPath);

if (Clear)

{

PlaceHolder.Controls.Clear();

}

PlaceHolder.Controls.Add(Control_ToAdd);

}

}

If this help you,don't forget mark it as a answer.Thanks!

Best Regards

Jin-Yu Yin

No comments:

Post a Comment