473,387 Members | 1,892 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,387 software developers and data experts.

Create Dynamic Dropdownlist Controls and related event - ASP.NET

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
1 4623
DrBunchman
979 Expert 512MB
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

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...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.