By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

Create Dynamic Dropdownlist Controls and related event

P: 2
Create Dynamic Dropdownlist Controls and related event
--------------------------------------------------------------------------------

Hi,
I am creating a Dynamic Search in my application.
I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove)
By pressing Add button ,another row will be created with the same control (I mean another dropdown and 2 button) and so on.
and by pressing Remove button the selecetd row will be removed.

I used viewstate to keep my value for postback,

I want by changing selectedvalue of dropdownlist ,create another dynamic dropdown and textbox. so far my code is working but when I click "Add" button to create another row ,I lost my second dropdownlist and textbox.

I don not know where should I fire my SelectedValue event,to keep all control on form ,by each postback I have all my control except controls that are created by selectedvalechange event

Here is all my code.

any idea???

Expand|Select|Wrap|Line Numbers
  1.  
  2. using System;
  3. using System.Data;
  4. using System.Configuration;
  5. using System.Collections;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Web.UI.HtmlControls;
  12. using AjaxControlToolkit;
  13. namespace LangleySunWorx.Controls
  14. {
  15.     public partial class WorkOrderSearch : System.Web.UI.UserControl
  16.     {
  17.         int number = 1;
  18.         int j = 0;
  19.         DropDownList[] _SearchByArray = new DropDownList[20]; //SearchBy Dropdown List
  20.         DropDownList[] _FilterByArray = new DropDownList[20]; //FilterBy DropDown List
  21.  
  22.         DropDownList[] _DdlValueArray = new DropDownList[20]; //for fields which are drop and selected from SearchBy
  23.         TextBox[] _TxtValueArray = new TextBox[20];//for fields which are text and selected from SearchBy
  24.         TextBox[] _TxtDateValueArray = new TextBox[20];//for fields which are Date and selected from SearchBy
  25.         Image[] _ImgBtnCalendarArray = new Image[20];//Image for fields which are Date and selected from SearchBy
  26.         CalendarExtender[] _CalendarExtenderArray = new CalendarExtender[20];
  27.         DropDownExtender[] _DropDownExtenderArray = new DropDownExtender[20];
  28.         Button[] _BtnAddArray = new Button[20];
  29.         Button[] _BtnRemoveArray = new Button[20];
  30.  
  31.  
  32.  
  33.         public string [] SearchByText= new string[] { 
  34.                 "Txt_Order#","Cmb_Job#", "Cmb_Status", "Dat_Date Created","Dat_Date Required",
  35.                 "Dat_Date Started","Dat_Date Completed","Txt_Pole#","Txt_House#","Txt_Location",
  36.                 "Txt_Priority","Txt_Parent Order#","Txt_System User","Txt_Originator","Cmb_Originator Type",
  37.                 "Txt_Originator Name","Cmb_Problem Code","Cmb_Billing Rate","Cmb_City Area","Txt_Map Reference",
  38.                 "Txt_Occurence#","Cmb_Request Locates","Cmb_Request Permits","Cmb_Restoration","Cmb_Police Services" };
  39.  
  40.         public string[] SearchByValue= new string[] { 
  41.                 "WorkorderID","JobID", "StatusID", "DateCreated","DateRequired",
  42.                 "DateStarted","DateCompleted","PoleNumber","HouseNumber","Location",
  43.                 "PriorityID","ParentWorkOrderID","SystemUserID","OriginatorID","OriginatorType",
  44.                 "OriginatorName","ProblemCodeID","BillingRateID","CityArea","MapReference",
  45.                 "OccurenceNumber","flagRequestLocates","flagRequestPermits","flagIsRestoration","flagPoliceServices" };
  46.  
  47.         protected void Page_Load(object sender, EventArgs e)
  48.         {
  49.             if (!IsPostBack)
  50.             {
  51.                 InsertComponet(number);
  52.                 ViewState.Add("Counter", number);
  53.             }
  54.         }
  55.  
  56.  
  57.         protected void InsertComponet(int Index)
  58.         {
  59.             InsertRow(Index);
  60.             FillSearchBy(Index);
  61.             _SearchByArraySelectedIndexChanged(_SearchByArray[Index], EventArgs.Empty);
  62.             FillFilterBy(Index);
  63.             FillAddRemoveButton(Index);
  64.             ViewState["controlsadded"] = true;
  65.             ViewState["Counter"] = Index;
  66.  
  67.         }
  68.         protected void InsertRow(int RowNumber)
  69.         {
  70.             HtmlTableRow tRow = new HtmlTableRow();
  71.             HtmlTableCell[] _CellArray = new HtmlTableCell[6];
  72.  
  73.             for (int i = 0; i <= 5; i++)
  74.             {
  75.                 _CellArray[i] = new HtmlTableCell();
  76.  
  77.             }
  78.  
  79.             tRow.Height = "50";
  80.             tRow.VAlign = "Middle";
  81.             Table11.Rows.Insert(RowNumber, tRow);
  82.             Table11.Rows[RowNumber].Cells.Insert(0, _CellArray[0]);
  83.             Table11.Rows[RowNumber].Cells.Insert(1, _CellArray[1]);
  84.             //Table11.Rows[RowNumber].Cells[1].InnerText = "Search By ";
  85.             Table11.Rows[RowNumber].Cells.Insert(2, _CellArray[2]);
  86.             //Table11.Rows[RowNumber].Cells[2].InnerText = "Filter By ";
  87.             Table11.Rows[RowNumber].Cells.Insert(3, _CellArray[3]);
  88.             Table11.Rows[RowNumber].Cells.Insert(4, _CellArray[4]);
  89.             Table11.Rows[RowNumber].Cells.Insert(5, _CellArray[5]);
  90.  
  91.         }
  92.         protected void DeleteRow(int RowNumber)
  93.         {
  94.             HtmlTableRow tRow = new HtmlTableRow();
  95.             Table11.Rows.RemoveAt(RowNumber);
  96.  
  97.         }
  98.         protected void FillSearchBy(int Index)
  99.         {
  100.             _SearchByArray[Index] = new DropDownList();
  101.             _SearchByArray[Index].ID = "_SearchByArray" + Convert.ToString(Index);
  102.             _SearchByArray[Index].Width = 150;
  103.             _SearchByArray[Index].AutoPostBack = true;
  104.             _SearchByArray[Index].Items.Clear();
  105.             for (int i = 0; i <= SearchByText.Length - 1; i++)
  106.             {
  107.                 _SearchByArray[Index].Items.Add(new ListItem(SearchByText[i].Substring(4), SearchByValue[i]));
  108.             }
  109.             _SearchByArray[Index].SelectedIndexChanged += new System.EventHandler(this._SearchByArraySelectedInd  exChanged);
  110.             Table11.Rows[Index].Cells[1].Controls.Add(_SearchByArray[Index]);
  111.  
  112.             //_DropDownExtenderArray[Index] = new AjaxControlToolkit.DropDownExtender();
  113.             //_DropDownExtenderArray[Index].ID = "_DropDownExtenderArray" + Convert.ToString(Index); ;
  114.             //_DropDownExtenderArray[Index].TargetControlID = "_SearchByArray" + Convert.ToString(Index);
  115.             //Table11.Rows[Index].Cells[1].Controls.Add(_DropDownExtenderArray[Index]);
  116.  
  117.  
  118.         }
  119.         protected void FillFilterBy(int Index)
  120.         {
  121.             _FilterByArray[Index] = new DropDownList();
  122.             _FilterByArray[Index].ID = "_FilterByArray" + Convert.ToString(Index);
  123.             _FilterByArray[Index].Width = 150;
  124.             _FilterByArray[Index].AutoPostBack = true;
  125.             _FilterByArray[Index].Items.Clear();
  126.             Table11.Rows[Index].Cells[2].Controls.Add(_FilterByArray[Index]);
  127.         }
  128.         protected void FillAddRemoveButton(int Index)
  129.         {
  130.             _BtnAddArray[Index] = new Button();
  131.             _BtnAddArray[Index].ID = "_BtnAddArray" + Convert.ToString(Index);
  132.             _BtnAddArray[Index].CssClass = "button";
  133.             _BtnAddArray[Index].Text = "ADD";
  134.             _BtnAddArray[Index].Width = 60;
  135.             _BtnAddArray[Index].Click += new System.EventHandler(this._BtnADDClickHandler);
  136.  
  137.             Table11.Rows[Index].Cells[4].Controls.Add(_BtnAddArray[Index]);
  138.  
  139.             _BtnRemoveArray[Index] = new Button();
  140.             _BtnRemoveArray[Index].ID = "_BtnRemoveArray" + Convert.ToString(Index);
  141.             _BtnRemoveArray[Index].Text = "Remove";
  142.             _BtnRemoveArray[Index].Width = 60;
  143.             _BtnRemoveArray[Index].CssClass = "button";
  144.             _BtnRemoveArray[Index].Click += new System.EventHandler(this._BtnRemoveClickHandler);
  145.             Table11.Rows[Index].Cells[4].Controls.Add(_BtnRemoveArray[Index]);
  146.  
  147.  
  148.         }
  149.  
  150.         //Dynamic Events
  151.         protected void _SearchByArraySelectedIndexChanged(object sender, EventArgs e)
  152.         {
  153.             int SelectedItem = 0;
  154.             SelectedItem = Convert.ToInt32(((DropDownList)sender).ID.Substrin  g(((DropDownList)sender).ID.Length - 1, 1));
  155.  
  156.             switch (SearchByText[((DropDownList)sender).SelectedIndex].Substring(0, 3))
  157.             {
  158.                 case "Txt":
  159.                     _TxtValueArray[SelectedItem] = new TextBox();
  160.                     _TxtValueArray[SelectedItem].ID = "_TxtValueArray" + Convert.ToString(SelectedItem);
  161.                     _TxtValueArray[SelectedItem].Text = SearchByText[((DropDownList)sender).SelectedIndex].Substring(4);
  162.                     _TxtValueArray[SelectedItem].Width = 150;
  163.                     Table11.Rows[SelectedItem].Cells[3].Controls.Add(_TxtValueArray[SelectedItem]);
  164.                     break;
  165.                 case "Cmb":
  166.                     _DdlValueArray[SelectedItem] = new DropDownList();
  167.                     _DdlValueArray[SelectedItem].ID = "_DdlValueArray" + Convert.ToString(SelectedItem);
  168.                     _DdlValueArray[SelectedItem].Width = 150;
  169.                     Table11.Rows[SelectedItem].Cells[3].Controls.Add(_DdlValueArray[SelectedItem]);
  170.                     break;
  171.                 case "Dat":
  172.                     //Create TextBox for Date
  173.                     _TxtDateValueArray[SelectedItem] = new TextBox();
  174.                     _TxtDateValueArray[SelectedItem].ID = _TxtDateValueArray + Convert.ToString(SelectedItem);
  175.                     Table11.Rows[SelectedItem].Cells[3].Controls.Add(_TxtDateValueArray[SelectedItem]);
  176.  
  177.                     //Create Image for Calendar Image
  178.                     _ImgBtnCalendarArray[SelectedItem] = new Image();
  179.                     _ImgBtnCalendarArray[SelectedItem].ID = "_ImgBtnCalendarArray" + Convert.ToString(SelectedItem);
  180.                     _ImgBtnCalendarArray[SelectedItem].ImageUrl = "~/imgs/Calendar.ico";
  181.                     Table11.Rows[SelectedItem].Cells[3].Controls.Add(_ImgBtnCalendarArray[SelectedItem]);
  182.  
  183.                     //Create CalendarExtender
  184.                     _CalendarExtenderArray[SelectedItem] = new AjaxControlToolkit.CalendarExtender();
  185.                     _CalendarExtenderArray[SelectedItem].ID = "_CalendarExtenderArray"+ Convert.ToString(SelectedItem);
  186.                     _CalendarExtenderArray[SelectedItem].TargetControlID = _TxtDateValueArray + Convert.ToString(SelectedItem);
  187.                     _CalendarExtenderArray[SelectedItem].CssClass = "RedCalendar";
  188.                     _CalendarExtenderArray[SelectedItem].Format = "d";
  189.                     _CalendarExtenderArray[SelectedItem].PopupButtonID = "_ImgBtnCalendarArray" + Convert.ToString(SelectedItem);
  190.                     Table11.Rows[SelectedItem].Cells[3].Controls.Add(_CalendarExtenderArray[SelectedItem]);
  191.                     break;
  192.             }
  193.             }
  194.         public void _BtnADDClickHandler(object sender, System.EventArgs e)
  195.         {
  196.             number = (int)ViewState["Counter"];
  197.             number += 1;
  198.             InsertComponet(number);
  199.             Label1.Text = Convert.ToString(number);
  200.         }
  201.  
  202.         public void _BtnRemoveClickHandler(object sender, System.EventArgs e)
  203.         {
  204.             number = (int)ViewState["Counter"];
  205.             number -= 1;
  206.             ViewState["Counter"] = number;
  207.  
  208.             int SelectedItem = 0;
  209.             SelectedItem = Convert.ToInt32(((Button)sender).ID.Substring(((Bu  tton)sender).ID.Length - 1, 1));
  210.             DeleteRow(SelectedItem);
  211.             Label1.Text = Convert.ToString(number);
  212.         }
  213.  
  214.  
  215.         protected override void LoadViewState(object savedState)
  216.         {
  217.             base.LoadViewState(savedState);
  218.             if (ViewState["controsladded"] == null)
  219.  
  220.                 j =(int) ViewState["Counter"] ;
  221.                 for (int i = 1; i <=j ; i++)
  222.                 {
  223.                     InsertComponet(i);
  224.                 }
  225.         }
  226.  
  227.  
  228.     }
  229. }
  230.  
Mar 3 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.