views:

1282

answers:

4

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.

I initially declared the button in the CreateChildControls method, and wired the event handler:

    {
        Button submitButton;
        submitButton = new Button();
        submitButton.Text = "Go!";
        submitButton.Click += new EventHandler(submitButton_Click);
        Controls.Add(submitButton);
    }

I have declared the functionality of the "submitButton_Click" EventHandler:

    void submitButton_Click(object sender, EventArgs e)
    {
        submitButton.Text = "Good!";
    }

I render the controls:

protected override void RenderContents(System.Web.UI.HtmlTextWriter output)
{ 
        RenderChildren(output);
}

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.

EDIT: I opened the SharePoint page with the web part on it and the button has been created like so:

<input type="submit" name="ctl00$PlaceHolderMain$ctl00$ctl04" value="Go!" />

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.

+2  A: 

Look at this link:

http://msdn.microsoft.com/en-us/library/ms452873.aspx

You're overriding the RenderContents method incorrectly, and wiring the click event in the wrong place.

Bravax
I see that this is definitely part of my problem. I am now wiring the click event in the CreateChildControls method, and I am rendering the controls as they have described in the tutorial, by calling RenderControls(HtmlTextWriter) in the RenderContents method. However, I am still getting no joy, which leads me to believe that the logic in my submitButton_Click method is the problem. Am I attempting to change the text property of the button incorrectly?
Geo Ego
Is submitButton a property of your class? Try adding this.SaveProperties = True after submitButton.Text = "Good!";
Bravax
Where are you deploying the web part DLL to? If it's in the /bin directory of the web application you will need to recycle the app pool before changes will take effect.
OedipusPrime
I tried using the SaveProperties method and I still get nothing. Currently, I am deploying my web part to the local bin of the web app. I recycle the app pool each time I deploy (actually, I recycle IIS entirely).
Geo Ego
You probably need to debug into it next. Put a breakpoint in the CreateChildControls method, and look at the EventArgs value and see what it's set to.
Bravax
Thanks for your help, everyone. I have been working on this off and on today and discovered that the solution was mainly due to a bonehead move on my part: the functionality does not work when previewing the Web Part, but when it is actually placed on the page, it works fine. However, it wouldn't have worked at all the way I had coded it originally, so Bravax's answer was correct. I have found also that I don't need to set SaveProperties because the Web Part personalization is not being changed. Thanks again for everyone's help!
Geo Ego
A: 

Use the CreateChildControls override instead, this gets called at the right moment in the ASP.NET Control rendering pipeline.

Colin
+1  A: 

Are you inheriting from System.Web.UI.WebControls.WebParts.WebPart or the SharePoint WebPart (Sorry, I don't recall the namespace). It is recommended to inherit from "System.Web.UI.WebControls.WebParts.WebPart" so that it is a simple ASP.NET Web Part and it can also be used in SharePoint.

Once you inherit from that class, you will need to override the "Render" method which is what I always do and it works out fine. I think your CreatechildControls method is correct. So your Render method will look like

protected override void Render(System.Web.UI.HtmlTextWriter output)
{ 
        submitButton.RenderControl(write);
}

You will also need to declare your button outside the "CreateChildControls" method.When writing SharePoint WebParts, I always take this approach

  1. Inherit from System.Web.UI.WebControls.WebParts.WebPart
  2. Create a partial class which will be used to declare all the controls used by the WebPart and to override the following methods: "CreateChildControls", "OnInit", and "Render".

Hope it helps.

iHeartDucks
I had been inheriting from System.Web.UI.WebControls.WebParts.WebPart, but I am now inheriting from the SharePoint namespace, as I didn't have access to SaveControls when inheriting from the System WebPart namespace.
Geo Ego
A: 

I had a similar problem. The event handler was always firing. I checked the view source of the SharePoint page and discovered the HTML control rendered was the input type = submit. To solve the issue, I set the 'button.UseSubmitBehavior = false' in code and set the ID. UseSubmitBehavior by default in ASP.NET 2.0 is true, if not set in code.

Tim Henning