dynamically generate buttons

Discussion in 'ASP.NET / ASP.NET Core' started by nature, Jan 6, 2004.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. I'm trying to generate buttons dynamically AND set the OnClick event. dynamically as well. I can generate the buttons but I get the error message when I try to set the OnClick event. I want to use the same event no matter which button is clicked, so knowing the default name would suffice. Thanks much! Carl

    Compilation Error
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: BC30390: 'System.Web.UI.WebControls.Button.Protected Overridable Sub OnClick(e As System.EventArgs)' is not accessible in this context
    because it is 'Protected'.

    Source Error:

    Line 24: btnButton.id = dtrLinkTypes("lt_link_type_cd")
    Line 25: btnButton.text = dtrLinkTypes("lt_link_type_cd_desc")
    Line 26: btnButton.OnClick="btn_ButtonClick"
    Line 27: plhLinkTypes.controls.add(btnButton)
    Line 28: litLabel = new LiteralControl
     
  2. How did you declare btn_ButtonClick? Is it public or private? There is one thing about Visual Studio(not sure if thats what you are using), but anyways, every so often it doesnt like private procedures even though they are only called within the same page.
     
  3. Button.OnClick is a method, not a property. I think what you want to do is set the onclick attribute of the button tag in the HTML. To do that, you will have to use the attributes property of the button.

    E.g. btnButton.Attributes("onclick") = "btn_ButtonClick"

    Keep in mind that setting this attribute overrides the Button.Click event, which might affect the code that you plan on writing in the btn_ButtonClick Sub. For instance, there is no EventArgs parameter that would normally contain a reference to the button that was clicked. You will need a different Sub to handle each button.

    Keith Payne
    Technical Marketing Solutions
     
  4. I didn't specify public or private. I thought public was the default but maybe I'm wrong.

    quote:Originally posted by bebemau

    How did you declare btn_ButtonClick? Is it public or private? There is one thing about Visual Studio(not sure if thats what you are using), but anyways, every so often it doesnt like private procedures even though they are only called within the same page.
    </blockquote id="quote"></font id="quote">
     
  5. Ok,
    I think what you really want to do is Add an event handler to your new dynamic button. Define your event handler for clicks, in this case btn_Click. Then when you create your new buttons just add the handle to the new button with the Buttonx.Click line.

    Just a tip too, you'll need to add the buttons to an existing control on your aspx page, I've used a Panel for simplicity.

    hth

    Major D

    Button Buttonx = new Button();
    Buttonx.Text = "Hello";
    Buttonx.Click += new System.EventHandler(this.btn_Click);
    this.Panel1.Controls.Add(Buttonx);

    private void btn_Click(object sender, System.EventArgs e)
    {
    }

    XBOXRacing.net

    Tournaments, Leagues & Prizes
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page