Add dynamic controls & save contols values

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 9040 Views - Last Post: 28 August 2013 - 05:03 AM Rate Topic: -----

#16 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Add dynamic controls & save contols values

Posted 23 August 2013 - 12:03 PM

I get the same NullReferenceException error on:
 protected void btnRemove_Click(object sender, System.EventArgs e)
    {
        
            RemoveUserControl(this, e);
    
    }

This post has been edited by staindromeo: 23 August 2013 - 12:34 PM

Was This Post Helpful? 0
  • +
  • -

#17 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Add dynamic controls & save contols values

Posted 24 August 2013 - 09:27 AM

you're going to want to check that RemoveUserControl isn't null as you said before. Also, when you add the controls back to the page in page load make sure you are adding the event to the controls again. And if none of that helps then when you step through the code using the debugger where is the null exception occurring?
Was This Post Helpful? 0
  • +
  • -

#18 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Add dynamic controls & save contols values

Posted 25 August 2013 - 01:03 PM

I tried add this to my page load in the .cs:
for (int i = 0; i < ControlCount; i++)
{
    MyUserControl control = (MyUserControl) LoadControl("MyUserControl.ascx");
    control.ID = "DynamicControls" + i;
    control.RemoveUserControl += new EventHandler(control_RemoveUserControl);
    PlaceHolder1.Controls.Add(control);
}


and I receive a 'System.Web.UI.Control' doesn't contain a definition for RemoveUserControl on
control.RemoveUserControl += new EventHandler(control_RemoveUserControl);


This is the event:
void control_RemoveUserControl(object sender, EventArgs e)
        {
            Control Delbtn = sender as Control;
            UserControl DynamicUserControl = (UserControl)Delbtn.Parent;
            PlaceHolder1.Controls.Remove(DynamicUserControl);

        }

Was This Post Helpful? 0
  • +
  • -

#19 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Add dynamic controls & save contols values

Posted 25 August 2013 - 05:58 PM

make sure the eventhandler is defined as public in the user control
Was This Post Helpful? 0
  • +
  • -

#20 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Add dynamic controls & save contols values

Posted 26 August 2013 - 12:21 PM

This is what I have in the .ascx.cs:
public partial class Controls_AddCompDDLDP : System.Web.UI.UserControl
{
    public event EventHandler RemoveUserControl;

    public void btnRemove_Click(object sender, System.EventArgs e)
    {
        RemoveUserControl(this, e);
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
  
}



And the button in the .ascx:
<asp:ImageButton runat="server" ID="btnDeleteComp" ImageUrl="~/images/Delete.png" onclick="btnRemove_Click"/>


This post has been edited by staindromeo: 26 August 2013 - 12:22 PM

Was This Post Helpful? 0
  • +
  • -

#21 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Add dynamic controls & save contols values

Posted 28 August 2013 - 05:03 AM

can you repost all the .cs code for the ascx and aspx? Just looking at it piece meal it looks like it should be working. You will definitely want to do a null check where you're calling the event to handle any cases where it's null, but then you need to figure out why it's not getting set. You may need to try moving the code to reload the controls from your Page_Load event to the Page_Init event.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2