0

I am trying to use the method described here to emulate an application page in a sandbox solution. The basic premise is to create a web part, and deploy a web part page that has that web part already embedded on it. You don't get the code-behind of the .aspx page, but you do get the server side code of the web part.

If I set up my markup like so

<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
    <WebPartPages:SPUserCodeWebPart
        <!-- all the stuff needed for the web part,
             like assembly name, solution id, etc. -->
        >
    </WebPartPages:SPUserCodeWebPart>
</asp:Content>

the web part loads fine, and I can add controls dynamically in the CreateChildControls() method. And I can add a button and a click handler:

myButton = new Button();
myButton.Text = "Submit";
myButton.Click += myButton_clickHandler;
this.Controls.Add(myButton);

However, when the page posts back, it seems that all the controls get lost, so there is no more button, and the click handler never fires.

On the other hand, if I set up my markup like this:

<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
    <WebPartPages:SPUserCodeWebPart
        <!-- all the stuff needed for the web part,
             like assembly name, solution id, etc. -->
        >
    </WebPartPages:SPUserCodeWebPart>
    <asp:TextBox ID="textBox" runat="server"></asp:TextBox>
    <asp:Button ID="myButton" runat="server" Text="Submit"></asp:Button>
</asp:Content>

then I can't seem to get to the other controls from within the web part's server side code. I've tried looking at this.Page.Controls and this.Parent.Parent etc., but those other controls aren't there.

So what am I missing? How can I get this set up to work, where I can have some controls on the page (dynamically added during CreateChildControls() is fine), and reliably access them to get values and fire event handlers on post-back?

0

During post back you need to recreate controls and attach event as

if(IsPostBack){
    CreateDynamicControlsEvents();
}


void CreateDynamicControlsEvents() {
    myButton = new Button();
    myButton.Text = "Submit";
    myButton.Click += myButton_clickHandler;
    this.Controls.Add(myButton);
}
  • That works, but it seems very counter-intuitive to me. If the original button object was destroyed, and I have to create a new one on post-back, shouldn't the click event somehow be related to the old object? How is the click event that came from the old object now related to the new object I'm creating during the post-back? – Dylan Cristy Jun 15 '15 at 14:34
  • Dynamic objects will not be part of the viewstate hence we need to recreate the click event during postback. I think you don't need to create button.. You can give it a try – Amal Hashim Jun 15 '15 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.