简体   繁体   中英

Removing validators of dynamically added controls

Hello i got this problem.

I have table that got rows filled with dynamically generated controls (TextBox) in first cell , button (MyCustomButton) in second and validation control in last cell (RegularExpressionValidator). The validation controller checks if data in the TextBox are correct. Function of the button is to remove row containing this button, textbox and validator.

My problem is when i click that button to remove row that it belongs to exception will pop up saying that " Unable to find control id 'MyTextBoxId' referenced by the 'ControlToValidate' ".

Problem here is that validator cant find TextBox to validate because it was removed and this exception pop up. I tried to first remove this validator and after that rest of the row, clear incorrect data in TextBox, turn off validation of that TextBox but im still getting this exception even after the validator was removed.

Removing from table method

    protected void DeleteMemberRow_Click(object sender, EventArgs e)
    {
        //Find row to remove
        TableRow row = (TableRow)((MyCustomButton)sender).Parent.Parent;

        //Custom list of controls - works fine
        ControlsList.RemoveAll(x => x.id == row.ID.Replace("row", ""));

        //MyTable is basic Table type
        MyTable.Rows.Remove(row);             
    }

Adding table rows

        Guid guid = Guid.NewGuid();

        TextBox txt = new TextBox();
        MyCustomButton btn = new MyCustomButton();
        btn.Click += new System.EventHandler(DeleteMemberRow_Click);
        btn.ID = "TeamMember" + guid + "btn";
        txt.ID = "TeamMember" + guid;
        RegularExpressionValidator validate = new RegularExpressionValidator();
        validate.ValidationExpression = @"(\d{5}, ?)*\d{5}";
        validate.ErrorMessage = "My error message";
        validate.Attributes.Add("runat", "server");
        validate.ControlToValidate = "TeamMember" + guid;
        validate.Attributes.Add("Display", "none");

        TableRow tRow = new TableRow();
        tRow.ID = "Teammember" + guid + "row";
        TableCell tCell2 = new TableCell();
        TableCell tCell = new TableCell();
        TableCell tCell1 = new TableCell();

        tCell2.Controls.Add(validate);
        tCell1.Controls.Add(btn);
        tCell.Controls.Add(txt);
        tRow.Cells.Add(tCell);
        tRow.Cells.Add(tCell1);
        tRow.Cells.Add(tCell2);

        MyTable.Rows.Add(tRow);

Any help would be appreciated. Thank you

Ok its fixed there wasnt problem with code but with me. I didnt noticed that i got another validator checking same TextBox so after i deleted it the other one failed to find TextBox to validate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM