How to fix the order of dynamically generated controls

1 posts, 0 answers
  1. Saifulla
    Saifulla avatar
    15 posts
    Member since:
    Sep 2018

    Posted 02 Jan 2019 Link to this post

    I have a radlistview item. On clicking of radlistview item i am generating dynamic controls as show in the code. Since dynamically generate controls not retain across post back so recreating every time. Controls are working fine but not generating in a proper order. All controls should generate one by one on clicking of list view but those are messing around, please some one suggest me how can i resolve this?
    Thanks.

    MARK UP:

    <div id="divListView" style="overflow: auto; width: 100%;" class="sortable">
     <telerik:RadListView ID="RadListView1" runat="server" RenderMode="Lightweight" DataKeyNames="Label" ClientDataKeyNames="Label" ItemPlaceholderID="ListViewPlaceHolder1">                                 
        <LayoutTemplate>
           <div class="RadListView RadListView_Silk">
              <table id="orgcharttable" class="layoutTable" width="100%">
                 <tr>
                     <td colspan="3" class="nopadding">
                        <asp:PlaceHolder ID="ListViewPlaceHolder1" runat="server"></asp:PlaceHolder>
                     </td>
                 </tr>
              </table>
          </div>
      </LayoutTemplate>
     <ItemTemplate>
          <div id="divHighlight" class="ListViewStyle" onclick="SelectControl(this,event);">
              <div style="vertical-align: top; width: 100%">
                  <table>
                     <tr id="tr1">
                        <td colspan="2" class="tdnormal">
                            <telerik:RadLabel ID="lbl1" runat="server" Text='<%# Bind("Label")%>' Font-Bold="true" CssClass="ItemHeaderStyle"></telerik:RadLabel>
                         </td>
                     </tr>
                     <tr id="tr2">
                         <td rowspan="2" colspan="1" style="width: 5%">
                         </td>
                     </tr>
                 </table>
             </div>
          </div>
         </ItemTemplate>
      </telerik:RadListView>
     
      <asp:Table ID="Table1" runat="server" EnableViewState="false"></asp:Table>

    Code Behind:

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadListView1.DataSource = GetDatatable();
                RadListView1.DataBind();
            }
            else
            {
                RecreateControls("rtb", "RadTextBox");
                RecreateControls("rcb", "RadComboBox");              
            }
        }
        public DataTable GetDatatable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Label");
            dt.Rows.Add("RadTextBox");
            dt.Rows.Add("RadComboBox");
            return dt;
        }
    protected void RadAjaxManager1_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
        {
            string commandText = e.Argument.ToString().Trim();
            string[] splitdata = commandText.Split('&');
            commandText = splitdata[0];
            string controlName = splitdata[1];
            switch (controlName)
            {
                case "RadTextBox":
                    int cnt1 = FindOccurence("rtb") + 1;
                    DynamicControls dcTextBox = new DynamicControls();
                    TableCell txtlblRad = dcTextBox.Controlscreation("RadLabel", "", cnt1, cnt1, "Text Box:", "", 0);
                    TableCell txtRad = dcTextBox.Controlscreation("RadTextBox", "", cnt1, cnt1, "", "", 0);
                    TableRow txtRow = new TableRow();
                    txtRow.Cells.Add(txtlblRad);
                    txtRow.Cells.Add(txtRad);
                    Table1.Rows.Add(txtRow);
                    break;
                case "RadComboBox":
                    int cnt2 = FindOccurence("rcb") + 1;
                    DynamicControls dcComboBox = new DynamicControls();
                    TableCell combolblRad = dcComboBox.Controlscreation("RadLabel", "", cnt2, cnt2, "Combo box:", "", 0);
                    TableCell comboRad = dcComboBox.Controlscreation("RadComboBox", "", cnt2, cnt2, "", "", 0);
                    TableRow comboRow = new TableRow();
                    comboRow.Cells.Add(combolblRad);
                    comboRow.Cells.Add(comboRad);
                    Table1.Rows.Add(comboRow);
                    break;
            }
    }
    private void RecreateControls(string ctrlPrefix, string ctrlType)
        {
            string[] ctrls = Request.Form.ToString().Split('&');
            int cnt = FindOccurence(ctrlPrefix);
            if (cnt > 0)
            {
                for (int k = 1; k <= cnt; k++)
                {
                    for (int i = 0; i < ctrls.Length; i++)
                    {
                        if (ctrls[i].Contains(ctrlPrefix + "_" + k.ToString()))
                        {
                            if (ctrlType == "RadTextBox")
                            {
                                DynamicControls dcTextBox = new DynamicControls();
                                TableCell txtlblRad = dcTextBox.Controlscreation("RadLabel", "", k, k, "TextBox:", "", 0);
                                TableCell txtRad = dcTextBox.Controlscreation("RadTextBox", "", k, k, "", "", 0);
                                TableRow txtRow = new TableRow();
                                txtRow.Cells.Add(txtlblRad);
                                txtRow.Cells.Add(txtRad);
                                Table1.Rows.Add(txtRow);
                            }
                            if (ctrlType == "RadComboBox")
                            {
                                DynamicControls dcComboBox = new DynamicControls();
                                TableCell combolblRad = dcComboBox.Controlscreation("RadLabel", "", k, k, "Combo box:", "", 0);
                                TableCell comboRad = dcComboBox.Controlscreation("RadComboBox", "", k, k, "", "", 0);
                                TableRow comboRow = new TableRow();
                                comboRow.Cells.Add(combolblRad);
                                comboRow.Cells.Add(comboRad);
                                Table1.Rows.Add(comboRow);
                            }
                         }
                     }
                  }
               }
            }
    private int FindOccurence(string substr)
        {
            string reqstr = Request.Form.ToString();         
            return ((reqstr.Length - reqstr.Replace(substr, "").Length) / substr.Length);
        }
    }
    }

    Expected Result: The Controls should generate one by one in order on click of listview, but generating in a group. Textboxes are generating as one group and dropdowns are generating as one group.

     

Back to Top