3

I’m trying to dynamically declare an ImageButton.

I declare it and assign an ID and Image to it as follows:

ImageButton btn = new ImageButton();
btn.ImageUrl = "img/Delete.png";
btn.ID = oa1[i] + "_" + i;
btn.OnClick = "someMethod";

But when I try to assign an OnClick handler for the button it throws the following exception:

System.Web.UI.WebControls.ImageButton.OnClick is inaccessible due to protection level

4 Answers 4

5

You couldn't assign a value to a method like that, even if it were accessible. You need to subscribe to the event:

btn.Click += ClickHandlingMethod;
Sign up to request clarification or add additional context in comments.

Comments

4

Take a look at this answer, it is related with dynamic controls and events

As Jon commented you cannot add a string to the event, in this case you need to add a handler for the event:

    protected void Page_Init(object sender, EventArgs e)
    {
        var i = new ImageButton();
        i.Click += new ImageClickEventHandler(i_Click);
        this.myPanel.Controls.Add(i);
    }

    void i_Click(object sender, ImageClickEventArgs e)
    {
        // do something
    }

Alternativeley

    protected void Page_Init(object sender, EventArgs e)
    {
        var i = new ImageButton();
        i.Click += (source, args) =>
        {
            // do something
        };
        this.myPanel.Controls.Add(i);
    }

8 Comments

Lambdas for this has a bad code smell. I would encourage the use of a named function.
@jcolebrand: Why? If there's only a small amount of code here which doesn't need to be called from anywhere else (e.g. it may just call a method with a more suitable signature) I see nothing wrong with using a lambda here.
Note that I didn't mention adding strings to events - I said that the OP was trying to assign a value (which happened to be a string, but that's actually irrelevant) to a method.
Oh, there's nothing inherently wrong with it @JonSkeet but by the same token I would encourage the use of a named function in javascript instead of an inline clickhandler.
@jcolebrand: Why? If it's just doing one simple thing, then keeping it at the point of subscription: a) means you can see exactly what it's doing; b) doesn't "pollute" the rest of the class with a method you don't want to use from anywhere else. If there are legitimate reasons for having it separate - e.g. testing - then that's a different matter, but I certainly don't think it's a "bad code smell".
|
4

An example:

private void CreateAButton()
{
    var button = new ImageButton();
    button.ImageUrl = "yourimage.png";
    button.ID = "Button1";
    button.Click += ButtonClick;

    Page.Form.Controls.Add(button);
}

private void ButtonClick(object sender, ImageClickEventArgs e)
{
    // Do stuff here
    // ...
}

Comments

0

You can use this code (one significant change) :

private void CreateAButton()
    {
        var button = new ImageButton();
        button.ImageUrl = "yourimage.png";
        button.ID = "Button1";           
        button.PostBackUrl = "http://www.towi.lt";
        Page.Form.Controls.Add(button);
    }

Trick is in "PostBackUrl". If you write correct link it will redirects to it (as in example). In other cases this will add original server name, '/' and text you entered. For example 'xxx' will be turned to "http://yourservername/xxx". It is very useful, when you working with redirects to same ISS, but different sites and dynamically creating buttons for users.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.