0

I have added controls dynamically to a table in ASP .NET by letting user enter the required no of rows. The sample code of the addRow() function is:

HtmlTableCell cell_1 = new HtmlTableCell();
HtmlTableCell cell_2 = new HtmlTableCell();
l = new Label();
nomenclature = new TextBox();
unit = new TextBox();

row[i] = new HtmlTableRow();

label.Text = "" +(i+ 1);           
cell_1.Controls.Add(l);
row[i].Controls.Add(cell_1);

nomenclature.TextMode = TextBoxMode.MultiLine;
nomenclature.Rows = 5;
nomenclature.Columns = 40;
nomenclature.ID="nomenclature"+i;
nomenclature.Text = "NA";
cell_2.Controls.Add(nomenclature);
row[i].Controls.Add(cell_2);

table_items.Controls.Add(row[i]);

Now after successfully adding the rows, when i refresh the page and again try to add new Rows, It gives me sameID 'nomenclature0' error.

Please help me regarding this,

Thanks in Advance,

3
  • can you add complete error in your question ? Commented Jun 23, 2014 at 12:00
  • I am guessing for the solution - Initially, when i=0, ID is 'Nomenclature0'. Please check if you are not incrementing value of i, you will have same value of i and ID will be Nomenclature0. Commented Jun 23, 2014 at 12:12
  • @KrunalPatil: the complete error: Multiple controls with the same ID 'nomenclature0' were found. FindControl requires that controls have unique IDs Commented Jun 24, 2014 at 2:06

1 Answer 1

1

I havent enough info about your code.

Two possible solutions:

1) Try this "table_items.Controls.Clear();" before call addRows Function.

2) if ispostback, dont fill table.

Sign up to request clarification or add additional context in comments.

6 Comments

Problem: I have added control in the row successfully and got data from them in Database. Now when i have the same page, i have to add rows dynamically from database. It showing all the rows with data, but when iam clicking the update option, its giving "object not instantiated" error for the same nomenclature0. Please help regarding this.
What solution are you chose? 1 o 2?
1st one worked but when updating the data of the same table, its throwing the error
If you will work with field updated and postback you should implement the second solution. Make sure your htmlTable has attribute EnableViewstate = true and make sure the cells and textbox are saved between postbacks. If some control isn't saved between postbacks you must load at Page_Load Event.
I have several radiobuttons on forms which controls the enabling and disabling of several textboxes through javascript. Problems occurs when i click the addrow button as discussed above, all the changes due to radiobuttons are reverted back i.e. is if the textbox on page load was enabled itremains enabled although radiobutton disabled it. As there are several combinations of radiobutton and textboxes, how to tackle this problem??
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.