Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ImageButton is inaccessible due to its protection level

Status
Not open for further replies.

blueark

Technical User
Apr 16, 2002
1,212
IE
I'm trying to create ImageButtons dynamically. Something like this:

Code:
ImageButton myButton = new ImageButton();
myButton.ImageUrl = "images/arrow_collapse.gif";
.
.
.
myContainer.Controls.Add (myButton);

So far so good. Now I want to create a function to handle click events, say, myClick(). Something like:

Code:
void myClick(Object Src, CommandEventArgs e) {
	...do something...
}

So... how do I link the two together? I've tried:
Code:
myButton.OnCommand = "myClick";

... but it doesn't seem to work. Any ideas?
 
At the end of your event declaration, try adding Handles myButton.Click

In VB I know you need to have that declared for any subs/functions you want to act as the event for a control.

So in VB your sub would look like:

Public Sub MyClick(source, e) Handles myButton.Click

End Sub

I'm sure C# should be similar.

hth

D'Arcy
 
Thanks for the reply. Got over that hurdle now - Here's sort of what I have:

Code:
for (i=0; i<5; i++) {
ImageButton myButton = new ImageButton();
myButton.ID = &quot;myButton&quot; + i.ToString();
myButton.ImageUrl = &quot;someimage.gif&quot;;
myContainer.Controls.Add (myButton);
myButton.Command += new CommandEventHandler(myClick);
myButton.CommandName=&quot;Click&quot;;
myButton.CommandArgument=i.ToString();
}

Seems to work!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top