Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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))

share|improve this question
    
when you load the page check ctl00$m$g_e00f473f_0a92_40aa_9805_03114c3f2da5$ctl00$ctl01$ctl00$toolBarTbl$RptC‌​ontrols$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
add comment

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
add comment

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);          
}
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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