473,695 Members | 2,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Create Dynamic Dropdownlist Controls and related event - ASP.NET

2 New Member
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 selectedvalecha nge 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 4653
DrBunchman
979 Recognized Expert Contributor
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
9089
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 and the 2nd dropdown (spaces) is updated. However, 'spaces' no longer has a SelectedIndexChanged event. Where did it go to? If I change 'spaces' (2nd dropdown) before it gets updated by the 1st (sites), its SelectedIndexChanged is fired and the...
0
1626
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" text='<%# DataBinder.Eval(Container.DataItem,"Name") %>'> </asp:Label> <asp:DropDownList id="DropDownList1" runat="server" DataTextField="Name" DataValueField="UID" OnSelectedIndexChanged="CallMySub" AutoPostBack="True"></asp:DropDownList>...
0
1175
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 when a user selects or enter inputs from the dynamic controls those values will then be inserted into a database in comma delimited format. Then, when that user returns to view their selections I need those controls (textbox, dropdownlist, etc.)...
3
2489
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 = DataReader(sSQLcmd) Me.DropDownList1.DataSource = dr
0
2907
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 strDataValueField; string strDataTextField; public CreateEditItemTemplateDDL(string DDLName,string DataValueField,string
13
10175
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 first order. Likewise if he clicks the second LinkButton, he will be shown the details of the second order he had placed. The Text of the LinkButtons will be 1 2 3 etc. So this user would see 1 2 3 4 5 as the LinkButtons. The problem is...
1
2510
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 DropDownList in the web,and dynamically add the items in it. such as: void Page_Load(Object sender, EventArgs e)
0
3496
by: MaryamSh | last post by:
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...
3
4363
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 number of dynamic controls that themselves have postback/autopostback so the display of the control needs to be built at latest in the Page.Load event or the event handlers for the controls don't get wired up. Now if I have a page that uses this user...
0
8631
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8984
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6491
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5839
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4340
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3003
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2272
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.