Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a radlistview on click of list view items i am creating dynamic controls. Present i have two controls text box and drop down. Text box control works fine on every click of list view but drop down is generating only once. On every click i want to generate a control, but fail to do so. Can some suggest me what i have done wrong in my code.

What I have tried:

protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               RadListView1.DataSource = GetDatatable();
               RadListView1.DataBind();
           }

           else
           {
               RecreateControls("rtb", "RadTextBox");
               RecreateControls("rddl", "RadDropDownList");
           }
      }
      public DataTable GetDatatable()
      {
           DataTable dt = new DataTable();
           dt.Columns.Add("Label");
           dt.Rows.Add("RadTextBox");
           dt.Rows.Add("RadDropDownList");
           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 "RadDropDownList":
                   int cnt4 = FindOccurence("rddl") + 1;
                   DynamicControls dcRadDropDown = new DynamicControls();
                   TableCell radDropDownlbl = dcRadDropDown.Controlscreation("RadLabel", "", cnt4, cnt4, "RDDL:", "", 0);
                   TableCell radDropDown = dcRadDropDown.Controlscreation("RadDropDownList", "", cnt4, cnt4, "", "", 0);
                   TableRow dropdownRow = new TableRow();
                   dropdownRow.Cells.Add(radDropDownlbl);
                   dropdownRow.Cells.Add(radDropDown);
                   Table1.Rows.Add(dropdownRow);
                   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 == "RadDropDownList")
                            {
                                DynamicControls dcRadDropDown = new DynamicControls();
                                TableCell radDropDownlbl = dcRadDropDown.Controlscreation("RadLabel", "", k, k, "RDDL :", "", 0);
                                TableCell radDropDown = dcRadDropDown.Controlscreation("RadDropDownList", "", k, k, "", "", 0);
                                TableRow dropdownRow = new TableRow();
                                dropdownRow.Cells.Add(radDropDownlbl);
                                dropdownRow.Cells.Add(radDropDown);
                                Table1.Rows.Add(dropdownRow);
                            }
                            break;
                       }
                   }
               }
           }
       }
        private int FindOccurence(string substr)
       {
           string reqstr = Request.Form.ToString();
           return (((reqstr.Length - reqstr.Replace(substr, "").Length) / substr.Length) / 2);
       }


Markup:

ASP.NET
  <telerik:RadListView ID="RadListView1" runat="server" RenderMode="Lightweight" DataKeyNames="Label" ClientDataKeyNames="Label" ItemPlaceholderID="ListViewPlaceHolder1" EnableViewState="false" Width="50%">
   <EmptyDataTemplate>
   </EmptyDataTemplate>
      <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%">
               <telerik:RadBinaryImage ID="rbimgWizard" runat="server" Width="80px" Height="80px" ResizeMode="Fit" CssClass="image" ImageUrl="../../Images/SCREEN.png" />
              </td>
            </tr>
        </table>
      </ItemTemplate>
     </telerik:RadListView>


  <asp:UpdatePanel ID="Panel1" runat="server" UpdateMode="Conditional" EnableViewState="false">
    <ContentTemplate>
     <asp:UpdatePanel ID="Panel2" runat="server" UpdateMode="Conditional" EnableViewState="false">
      <ContentTemplate>
        <asp:Table ID="Table1" runat="server"></asp:Table>
      </ContentTemplate>
    </asp:UpdatePanel>
  </ContentTemplate>
</asp:UpdatePanel>
Posted
Updated 28-Nov-18 4:53am
v3
Comments
Vincent Maverick Durano 28-Nov-18 17:39pm    
You need to use the debugger to figure out why it's not working for dropdown. Go ahead and set a break-point at the method where you recreate the controls and step into the code.

1 solution

Any controls that are created on the fly(in code behind) will not exist on postback. You will need to create them again.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900