Link to home
Start Free TrialLog in
Avatar of troycomp
troycomp

asked on

Attributes.Add not working in composite control

I have a ton of composite controls and i have the Attributes.Add function on a lot of my asp controls inside of them. I noticed something. If i go directly to the page via the menu or typing a url, the attributes are added flawlessly. If i do a response.redirect or server.transfer, these attributes dont get added. If i hit f5 then the attributes appear. What is the problem? Heres the piece of code where im adding the attribute:

  protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            addBtn.Click += new EventHandler(addBtn_Click);
            addBtn.Text = "Add Leg";
            //addBtn.OnClientClick = "MyFunc();"; <--this doesnt work either
            addBtn.Attributes.Add("onclick", "MyFunc();");
}
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

You are using the OnInit which may not be getting called when you use the other methods since the page may already be initialized at that point.  Try moving this code to your page load sub.
Avatar of troycomp
troycomp

ASKER

It is getting called. I put a breakpoint on the line in question and it stoped. I also moved it to the CreateChildControls() method and still a no go. This has to do with the difference between response.redirect (or server.transfer) and accessing the page directly. Thats the only difference in what im doing.
ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I added it to page_load on the aspx page and it works now. I misunderstood you earlier.  Honestly im doing this for another one of my controls. Ive worked almost 30 hours in 2 days so my brain is tired. Thanks

Not a problem at all.

Glad I re-iterated it.  I was actually just trying to let you and other experts know I was out of ideas beyond the Page_Load so you would get the help you needed. :)

Anyway, good luck with remainder of the project.

Regards,
Kevin