473,395 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Create Dynamic Dropdownlist Controls and related event

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
0 3453

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

Similar topics

1
by: Donal | last post by:
I have 3 related dropdowns. When the 1st is changed, the 2nd is updated, and when the 2nd is changed, the 3rd is updated. When i change the 1st dropdown (sites), the SelectedIndexChanged fires...
0
by: Ashish Sharma | last post by:
I have a drop down list inside a data list on a form like this : <asp:DataList ID="DataList1" runat="server" EnableViewState="False"> <ItemTemplate> <asp:Label ID="lblname" Runat="server"...
0
by: Slam via DotNetMonster.com | last post by:
I want to create a couple of dynamic controls and the controls definitions are stored in a database. Once the the controls are created, I want to fill any dropdownlist with database values. And...
3
by: Carlos Lozano | last post by:
Hello, I am having a problem getting the selectedValue from a dropdownlist that is populated with a dataReader and just can't see the problem. I did the following: dim dr as DataReader dr...
0
by: cindy | last post by:
I have a dynamic datagrid. I have custom classes for the controls public class CreateEditItemTemplateDDL : ITemplate { DataTable dtBind; string strddlName; string strSelectedID; string...
13
by: rn5a | last post by:
In a shopping cart app, suppose a user has placed 5 orders, I want to show him 5 LinkButtons (one for each order) so that when he clicks the first LinkButton, he would be shown the details of his...
1
by: =?Utf-8?B?Z29yaWxsYQ==?= | last post by:
As we know , the view state is not responsible for having TextBoxes, CheckBoxes, DropDownLists, and other Web controls remember their values across postback. When I dynamically add a...
1
by: MaryamSh | last post by:
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...
3
balabaster
by: balabaster | last post by:
I've got a user control that builds a table of dynamic data based on a :LINQ class holding the data. The data is loaded using the LoadData(DataInstance) method. The table it builds contains a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.