سؤال

Thanks for all who reads. I have a code issue that has been discussed many times on stackoverflow, but I can't make the provided solutions work.

It's about server control that contain other controls and I can't fire their events.

My issue is simple, i need to add any numbers ok LinButton into my server control. this is my code:

protected override void CreateChildControls()
{
    for (int i = 0; i < IndiceValue; i++)
    {
        LinkButton imageTemp = new LinkButton() { CommandName = String.Format("{1}_{0}", CommandNames, i), Text = i.ToString(), ID = Guid.NewGuid().ToString()};
        imageTemp.Click += ImageTempOnClick;
        this.Controls.Add(imageTemp);
    }
    base.CreateChildControls();
}

I have a custom event in my control:

public delegate void IndiceFiabiliteCommand(object sender, IndiceFiabiliteCommandEventArg e);
public event IndiceFiabiliteCommand Command;

And theorically, when any of the LinkButton is clicked, I want the user to be warn that any of the LinkButton has been clicked. I give him all the needed infos but i don't think this part is the problem.

Last thing to know is that I implement INamingContainer and I tried to implement both, IPostBackDataHandler and IPostBackEventHandler but it didn't worked.

I hope it's understandable enough

thanks!

هل كانت مفيدة؟

المحلول

The problem is ID = Guid.NewGuid().ToString()

You cannot have dynamically created control with dynamic id. Change the code to

... i.ToString(), ID = i.ToString()

Basically, dynamic control's ID must be same as the ID originally created.

Otherwise, new control is created on every post-back, and it cannot find the control which causes the post-back.

FYI: if you need to catch an event, I would like to suggest to use CompositeControl which already have INamingContainer.

Note: it is just what I found based on your code; it might be other issue too.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top