Dynamic Controls...
-
Hi! I ran into a bit of a problem. I have managed to successfully Create a row of 7 textboxes in a Table at runtime. I used the CalendarExtender on the first Textbox and it works perfect. The trouble begins when I click the "ADD" button to create a new row of 7 Textboxes. I have set the calendarExtender targetId = "txtCalendar" + j meaning that the targetID is txtCalendar0. When I hit the "ADD" button to create a second row I get the error that the TargetId has to be unique. I understand that I am currently targeting the txtCalendar0 textbox and that works but when I click add to create a second row the first textbox is named txtcalendar0 as well. Any suggestions on how to get around this?? Please let me know if I should post the piece of code for this. Thank you!
Illegal Operation
-
Hi! I ran into a bit of a problem. I have managed to successfully Create a row of 7 textboxes in a Table at runtime. I used the CalendarExtender on the first Textbox and it works perfect. The trouble begins when I click the "ADD" button to create a new row of 7 Textboxes. I have set the calendarExtender targetId = "txtCalendar" + j meaning that the targetID is txtCalendar0. When I hit the "ADD" button to create a second row I get the error that the TargetId has to be unique. I understand that I am currently targeting the txtCalendar0 textbox and that works but when I click add to create a second row the first textbox is named txtcalendar0 as well. Any suggestions on how to get around this?? Please let me know if I should post the piece of code for this. Thank you!
Illegal Operation
-
How are you generating the IDs? Post the code.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
Here is the code as it works now. Everytime you click the add button it should just duplicate the existing row.
private int numOfRows = 1; protected void Page\_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GenerateTable(numOfRows); } } private void GenerateTable(int numOfRows) { Table tblTimesheet = new Table(); tblTimesheet.ID = "tblTimesheet"; placeHolder.Controls.Add(tblTimesheet); const int colsCount = 12; for (int i = 0; i < numOfRows; i++) { TableRow HeaderRow = new TableRow(); TableRow newRow = new TableRow(); for (int j = 0; j < colsCount; j++) { TableCell newCell = new TableCell(); newCell.HorizontalAlign = HorizontalAlign.Center; TextBox txtWeekday = new TextBox(); txtWeekday.ID = "txtWeekday" + j; txtWeekday.Width = 80; CheckBox chkLunchCrib = new CheckBox(); chkLunchCrib.ID = "chkLunchCrib" + j; chkLunchCrib.Checked = false; if (txtWeekday.ID == "txtWeekday11" || txtWeekday.ID == "txtWeekday10" || txtWeekday.ID == "txtWeekday9") { txtWeekday.Visible = false; } if (chkLunchCrib.ID == "chkLunchCrib0" || chkLunchCrib.ID == "chkLunchCrib1" || chkLunchCrib.ID == "chkLunchCrib2" || chkLunchCrib.ID == "chkLunchCrib3" || chkLunchCrib.ID == "chkLunchCrib4" || chkLunchCrib.ID == "chkLunchCrib5" || chkLunchCrib.ID == "chkLunchCrib6" || chkLunchCrib.ID == "chkLunchCrib7" || chkLunchCrib.ID == "chkLunchCrib8") { chkLunchCrib.Visible = false; } #region Date Selection. TextBox txtCalendar = new TextBox(); txtCalendar.ID = "txtCalendar" + j; txtCalendar.Width = 155; txtCalendar.Text = DateTime.Today.ToShortDateString(); ImageButton imgbtnCalendar = new ImageButton(); imgbtnCalendar.ID = "imgbtnCalendar"; imgbtnCalendar.ImageUrl = "~/Resources/Calendar\_scheduleHS.png"; imgbtnCalendar.ImageAlign =
-
Here is the code as it works now. Everytime you click the add button it should just duplicate the existing row.
private int numOfRows = 1; protected void Page\_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GenerateTable(numOfRows); } } private void GenerateTable(int numOfRows) { Table tblTimesheet = new Table(); tblTimesheet.ID = "tblTimesheet"; placeHolder.Controls.Add(tblTimesheet); const int colsCount = 12; for (int i = 0; i < numOfRows; i++) { TableRow HeaderRow = new TableRow(); TableRow newRow = new TableRow(); for (int j = 0; j < colsCount; j++) { TableCell newCell = new TableCell(); newCell.HorizontalAlign = HorizontalAlign.Center; TextBox txtWeekday = new TextBox(); txtWeekday.ID = "txtWeekday" + j; txtWeekday.Width = 80; CheckBox chkLunchCrib = new CheckBox(); chkLunchCrib.ID = "chkLunchCrib" + j; chkLunchCrib.Checked = false; if (txtWeekday.ID == "txtWeekday11" || txtWeekday.ID == "txtWeekday10" || txtWeekday.ID == "txtWeekday9") { txtWeekday.Visible = false; } if (chkLunchCrib.ID == "chkLunchCrib0" || chkLunchCrib.ID == "chkLunchCrib1" || chkLunchCrib.ID == "chkLunchCrib2" || chkLunchCrib.ID == "chkLunchCrib3" || chkLunchCrib.ID == "chkLunchCrib4" || chkLunchCrib.ID == "chkLunchCrib5" || chkLunchCrib.ID == "chkLunchCrib6" || chkLunchCrib.ID == "chkLunchCrib7" || chkLunchCrib.ID == "chkLunchCrib8") { chkLunchCrib.Visible = false; } #region Date Selection. TextBox txtCalendar = new TextBox(); txtCalendar.ID = "txtCalendar" + j; txtCalendar.Width = 155; txtCalendar.Text = DateTime.Today.ToShortDateString(); ImageButton imgbtnCalendar = new ImageButton(); imgbtnCalendar.ID = "imgbtnCalendar"; imgbtnCalendar.ImageUrl = "~/Resources/Calendar\_scheduleHS.png"; imgbtnCalendar.ImageAlign =
Illegal Operation wrote:
for (int i = 0; i < numOfRows; i++)
Above loop will run number of times equal to the value of i. In this case
Illegal Operation wrote:
"txtCalendar" + j;
will create the multiple target control id for ajaxCalExt. You may try this, ajaxCalExt.TargetControlID = "txtCalendar" + j.ToString() + i.ToString();
-
Here is the code as it works now. Everytime you click the add button it should just duplicate the existing row.
private int numOfRows = 1; protected void Page\_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GenerateTable(numOfRows); } } private void GenerateTable(int numOfRows) { Table tblTimesheet = new Table(); tblTimesheet.ID = "tblTimesheet"; placeHolder.Controls.Add(tblTimesheet); const int colsCount = 12; for (int i = 0; i < numOfRows; i++) { TableRow HeaderRow = new TableRow(); TableRow newRow = new TableRow(); for (int j = 0; j < colsCount; j++) { TableCell newCell = new TableCell(); newCell.HorizontalAlign = HorizontalAlign.Center; TextBox txtWeekday = new TextBox(); txtWeekday.ID = "txtWeekday" + j; txtWeekday.Width = 80; CheckBox chkLunchCrib = new CheckBox(); chkLunchCrib.ID = "chkLunchCrib" + j; chkLunchCrib.Checked = false; if (txtWeekday.ID == "txtWeekday11" || txtWeekday.ID == "txtWeekday10" || txtWeekday.ID == "txtWeekday9") { txtWeekday.Visible = false; } if (chkLunchCrib.ID == "chkLunchCrib0" || chkLunchCrib.ID == "chkLunchCrib1" || chkLunchCrib.ID == "chkLunchCrib2" || chkLunchCrib.ID == "chkLunchCrib3" || chkLunchCrib.ID == "chkLunchCrib4" || chkLunchCrib.ID == "chkLunchCrib5" || chkLunchCrib.ID == "chkLunchCrib6" || chkLunchCrib.ID == "chkLunchCrib7" || chkLunchCrib.ID == "chkLunchCrib8") { chkLunchCrib.Visible = false; } #region Date Selection. TextBox txtCalendar = new TextBox(); txtCalendar.ID = "txtCalendar" + j; txtCalendar.Width = 155; txtCalendar.Text = DateTime.Today.ToShortDateString(); ImageButton imgbtnCalendar = new ImageButton(); imgbtnCalendar.ID = "imgbtnCalendar"; imgbtnCalendar.ImageUrl = "~/Resources/Calendar\_scheduleHS.png"; imgbtnCalendar.ImageAlign =
Illegal Operation wrote:
private void GenerateTable(int numOfRows) { Table tblTimesheet = new Table(); tblTimesheet.ID = "tblTimesheet"; placeHolder.Controls.Add(tblTimesheet); . . .
As per the above lines, the Table tblTimesheet would be added to the place holder as many times as you click the Add row button. You would need to change the logic to first check if the place holder already has a the desired table and based on availability you have to either create a new instance or get an handle to the existing instance and add rows. To do this you have to set the runat property of the table that you create to "server" so that the table is visible on the server after postback. You would also have to change the logic to add rows based on the above availability check. Alternately you can clear the control array before you add the new table, but if you decide to do so then you'll have to ensure that you copy the data from the existing table's textboxes to the new table's textboxes. For the replication logic for the date textbox you can use the logic suggested by arun$aini. In fact it would be advisable to do it for all the controls that you add to the table. I assume you need to get the data from the textboxes/checkboxes on some submit button click. For that to happen you need to set the runat property of all the dynamically created controls too.