1

I have an issue with LinkButton in my toolbar, basically i deployed a custom action which adds a link button to the menu, this link button on click should do some task like adding an item, but this method is never called.

That is how the code looks like for the link button:

public class MyCustomControl: WebControl
    {
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            LinkButton linkButton = new LinkButton
            {
                Text="Fun"
            };

            linkButton.Click += new EventHandler(linkButton_Click);
            Controls.Add(linkButton);          

        }

        void linkButton_Click(object sender, EventArgs e)
        {
          //DO SOME ACTIOn
        }
}

So the linkbutton is added and everything is ok, but when i click it, it just refreshes the page and that is it. Nothing happens

Here is the declaration in the custom action:

 <CustomAction
        Id="MyCustomAction"
        Title="Fun"
        Description="Lets have fun"
        RegistrationType="ContentType"
        RegistrationId="0x01"
        GroupId="ActionsMenu"
        Location="DisplayFormToolbar"
        Sequence="1000"
        ControlAssembly="Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ac112ea8ee9f48g0"
        ControlClass="Assembly.MyNamespace.MyCustomControl">

Any idea, what could go wrong?

Update:

This link is rendered as with this href:

javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$m$g_e00f473f_0a92_40aa_9805_03114c3f2da5$ctl00$ctl01$ctl00$toolBarTbl$RptControls$Fun", "", true, "", "", false, true))

  • when you load the page check ctl00$m$g_e00f473f_0a92_40aa_9805_03114c3f2da5$ctl00$ctl01$ctl00$toolBarTbl$RptControls$Fun exists, then click the button and check the same ID exists... if it doesnt then thats pointing to your issue. – Mauro Feb 28 '13 at 15:30
  • But if it does what then? – Shkipper Feb 28 '13 at 19:36
  • I just checked and when the page first loads and without clicking on the link, it already has this kind of information, this href ... – Shkipper Mar 1 '13 at 6:49
2

After a lot of time spending researching this issue, I finally found the solution, basically what i had to do is on OnLoad method, i had to ensure that the controls are bound, basically by only doing this:

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        EnsureChildControls();
    }

This way the link button worked perfectly.

0

give the link button an ID - i've had this issue before where the controls ID is autogenerated and cant map an event back to it on postback.

I.e. change:

LinkButton linkButton = new LinkButton
{
    Text="Fun"
};

To

LinkButton linkButton = new LinkButton
{
    Text="Fun",
    Id="CustomActionLinkButton"
};

Alternatively use the OnInit() method

private LinkButton linkButton;

protected override void OnInit(EventArgs e)
{
   base.OnInit(e);
   linkButton = new LinkButton
   {
       Text="Fun",
       Id="CustomActionLinkButton"
   };
   linkButton.Click += new EventHandler(linkButton_Click);
}

protected override void CreateChildControls()
{
    base.CreateChildControls();
    Controls.Add(linkButton);          
}
  • Yep, I have tried with your approach but still same, just a refresh and my event wasn't called. :( – Shkipper Feb 28 '13 at 14:27
  • tried it, and still no success, really strange, have no idea why :( – Shkipper Feb 28 '13 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.