Note that there are some explanatory texts on larger screens.

plurals
  1. POasp:button Created Programmatically: EventHandler does not fire
    primarykey
    data
    text
    <p>I am writing a SharePoint web part which will have a simple ASP.NET form. I am using HtmlTextWriter to render the controls. The problem I have is that my button does not seem to be triggering the EventHandler I have assigned it.</p> <p>I initially declared the button in the CreateChildControls method, and wired the event handler:</p> <pre><code>{ Button submitButton; submitButton = new Button(); submitButton.Text = "Go!"; submitButton.Click += new EventHandler(submitButton_Click); Controls.Add(submitButton); } </code></pre> <p>I have declared the functionality of the "submitButton_Click" EventHandler:</p> <pre><code>void submitButton_Click(object sender, EventArgs e) { submitButton.Text = "Good!"; } </code></pre> <p>I render the controls:</p> <pre><code>protected override void RenderContents(System.Web.UI.HtmlTextWriter output) { RenderChildren(output); } </code></pre> <p>Finally, I deploy the web part. It shows up fine in the catalog and when I add it to a page, the control shows up. However, I would assume that when I click the button, its text should change from "Go!" to "Good!" Instead, it does nothing. I'm pretty new to all of these technologies -- C#, Sharepoint, and ASP.NET -- so I'm pretty sure it's a problem with my understanding, but trying different steps from articles all over the net and previous questions here haven't fixed my problem. Thanks for taking a look.</p> <p>EDIT: I opened the SharePoint page with the web part on it and the button has been created like so:</p> <pre><code>&lt;input type="submit" name="ctl00$PlaceHolderMain$ctl00$ctl04" value="Go!" /&gt; </code></pre> <p>It looks like the OnClick value has not been added at all, which is what I thought adding the EventHandler would do. Am I trying to add OnClick in a completely wrong way? I also don't understand why the button name does not match what I declared in my code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload