Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Dynamic Controls...

Dynamic Controls...

Scheduled Pinned Locked Moved ASP.NET
helptutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Illegal Operation
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • I 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

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      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...!!

      I 1 Reply Last reply
      0
      • D dan sh

        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...!!

        I Offline
        I Offline
        Illegal Operation
        wrote on last edited by
        #3

        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 =
        
        S D 2 Replies Last reply
        0
        • I Illegal Operation

          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 =
          
          S Offline
          S Offline
          saini arun
          wrote on last edited by
          #4

          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();

          1 Reply Last reply
          0
          • I Illegal Operation

            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 =
            
            D Offline
            D Offline
            Dinesh Mani
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups