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

Create Dynamic Dropdownlist Controls and related event - ASP.NET

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


DrBunchman
Expert 100+
P: 979
Hi MaryamSh,

You've posted this question on the ASP forum which only deals with classic ASP questions. To receive help to your question you'll need to post it on the .NET forum.

Good luck

Dr B
Mar 3 '08 #2

Post your reply

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