Jump to content

Web Form. How to remove button that fired the event? (NullReferenceException)

- - - - -

  • Please log in to reply
No replies to this topic

#1
jon4231

jon4231

    Newbie

  • Members
  • Pip
  • 1 posts
I'm working on a simple form that dynamically creates textboxes for First and Last names and should be able to remove any specific row. To do this, I add a button at the end of each row and scan for that button event in the OnLoad method when it is clicked. When this happens, I remove the associated textboxes as well as the button itself. I think this is causing an exception to be thrown, which is "[NullReferenceException: Object reference not set to an instance of an object.]" How do I get around this?

protected override void OnInit(EventArgs e)

        {


            base.OnInit(e);

            if (Page.IsPostBack)

            {

                adultFN = (List<TextBox>)Session["adultFN"];

                adultLN = (List<TextBox>)Session["adultLN"];

                CreateAdultTextBoxes();

                CreateChildrenTextBoxes();

                cause = GetPostBackControl(Page);

            }

        }

        protected override void OnLoad(EventArgs e)

        {

            base.OnLoad(e);

            //CreateAdultTextBoxes();

           // CreateChildrenTextBoxes();

            if (!Page.IsPostBack)

            {

                InitializeAdultTextBoxes();

                CreateAdultTextBoxes();

                CreateChildrenTextBoxes();

            }

            else if (Page.IsPostBack)

            {

                

                if ("Button1".Equals(cause.UniqueID))

                {

                    adultFN = (List<TextBox>)Session["adultFN"];

                    adultLN = (List<TextBox>)Session["adultLN"];

                    Label1.Text = adultFN.ElementAt(0).ID;

                    Count++;

                    AddAdultTB();

                    CreateAdultTextBoxes();

                    CreateChildrenTextBoxes();

                }[B]else if(cause.UniqueID.StartsWith("RemoveLine")){

                    adultFN = (List<TextBox>)Session["adultFN"];

                    adultLN = (List<TextBox>)Session["adultLN"];

                    String a = cause.UniqueID.ToString();

                    a = a.Substring(a.Length-1);

                    Label1.Text = "";

                    

                    for (int i=0; i < adultFN.Count; i++ )

                    {

                        if (i == Convert.ToInt32(a))

                        {

                            adultFN.RemoveAt(i);

                            adultLN.RemoveAt(i);

                            continue;

                        }

                        

                    }

                    Label1.Text += adultFN.ToArray().ToString();

                    Count -= 1;

                    CreateAdultTextBoxes();

                    CreateChildrenTextBoxes();[/B]

                    

                }else

                {

                    CreateAdultTextBoxes();

                    CreateChildrenTextBoxes();

                }

                //     Count += 1;

            }


        }

private void InitializeAdultTextBoxes()

        {

            adultFN = new List<TextBox>();

            adultLN = new List<TextBox>();


            AddAdultTB();

            

        }


        private void AddAdultTB()

        {

            TextBox tb = new TextBox();

            TextBox tb2 = new TextBox();

            adultFN.Add(tb);

            adultLN.Add(tb2);

        }


        private void CreateAdultTextBoxes()

        {

            PlaceHolder1.Controls.Clear();

            int stop = Count;

            Button1.Click += new EventHandler(Button1_Click);

            //m_dynamicTextBoxes = new TextBox[stop, 2];

            //listAdult = new List<TextBox>[stop,2];

            

            //Label1.Text = "";


            for (int i = 0; i < adultFN.Count; i++)

            {

                adultFN.ElementAt(i).ID = "AdultFNTB" + i.ToString();


                PlaceHolder1.Controls.Add(new LiteralControl("<p>First name:  "));

                PlaceHolder1.Controls.Add(adultFN.ElementAt(i));

                rf = new RequiredFieldValidator();

                rf.ControlToValidate = adultFN.ElementAt(i).ID;

                rf.ErrorMessage = "First name required";

                rf.Text = "*";

                PlaceHolder1.Controls.Add(rf);


                PlaceHolder1.Controls.Add(new LiteralControl("Last name:  "));

                adultLN.ElementAt(i).ID = "AdultLNTB" + i.ToString();

                PlaceHolder1.Controls.Add(adultLN.ElementAt(i));

 //this is the button that creates the event

                [B]if (i != 0)

                {

                    Button b = new Button();


                    b.ID = "RemoveLine" + i;

                    b.Text = b.ID.ToString();

                    b.Click += new EventHandler(removeBclick);

                    b.ValidationGroup = "None";

                    PlaceHolder1.Controls.Add(b);

                }[/B]

                PlaceHolder1.Controls.Add(new LiteralControl("</p>"));


                Session["adultFN"] = adultFN;

                Session["adultLN"] = adultLN;

            }





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users