473,856 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

postback problem in creation of dynamic dropdownlist.

30 New Member
Respected Sir,
I have to create multiple dynamic dropdownlist boxes and add items dynamically in <asp:table> server control but problem occurs , i.e. except of fist dropdown list no dropdownlist boxes are generating a postback.here is a code .
Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.         int selected_question = (int)Session["selected_question"];
  4.         if (!Page.IsPostBack)
  5.         {
  6.             display_blueprint();
  7.             string[] Question_Label =(string[]) ViewState["DefficultyLabel"];
  8.             string[] Block1 = (string[])ViewState["Block"];
  9.             checkdropdownlist(selected_question, Block1, Question_Label);
  10.         }
  11.     }
  12.  private void checkdropdownlist(int selcted_question, string[] Block1, string[] Question_Label)
  13.     {
  14.         int max_quest_average = Convert.ToInt32(0.14 * selcted_question);
  15.         int normal_quest_average = Convert.ToInt32(0.13 * selcted_question);
  16.         int normal_quest_def = Convert.ToInt32(0.04 * selcted_question);
  17.         int max_quest_easy = Convert.ToInt32(0.10 * selcted_question);
  18.         ViewState["max_quest_easy"] = max_quest_easy;
  19.         int normal_quest_easy = Convert.ToInt32(0.08 * selcted_question);
  20.         //     Table dtb = new Table();
  21.  
  22.         for (int i = 0; i < Block1.Length; i++)
  23.         {
  24.             TableRow tr = new TableRow();
  25.             TableCell cell1 = new TableCell();
  26.             DropDownList dpdn_block = new DropDownList();
  27.             dpdn_block.ID = "Dropdownlist" + i;
  28.             for (int j = 0; j < Block1.Length; j++)
  29.             {
  30.                 dpdn_block.Items.Add(new ListItem("" + Block1[j] + "", "" + Block1[j] + ""));
  31.             }
  32.             cell1.Controls.Add(dpdn_block);
  33.             tr.Cells.Add(cell1);
  34.  
  35.             //Dropdown_Table.Rows.Add(tr);
  36.             for (int k = 0; k < Question_Label.Length; k++)
  37.             {
  38.                 TableCell cell2 = new TableCell();
  39.  
  40.                 if (Question_Label[k] == "ESY")
  41.                 {
  42.                     DropDownList dpdnlst_qlabel_esy = new DropDownList();
  43.                     dpdnlst_qlabel_esy.ID = "dpdn_easy'" + i + "' ";
  44.                     for (int l = 1; l <= max_quest_easy; l++)
  45.                     {
  46.                         dpdnlst_qlabel_esy.Items.Add(new ListItem("" + l + "", "" + l + ""));
  47.                     }
  48.                     cell2.Controls.Add(dpdnlst_qlabel_esy);
  49.                     tr.Cells.Add(cell2);
  50.                     dpdnlst_qlabel_esy.AutoPostBack = true;
  51.                     dpdnlst_qlabel_esy.SelectedIndexChanged += new EventHandler(dpdnlst_qlabel_esy_SelectedIndexChanged);
  52.                 }
  53.  
  54.                 if (Question_Label[k] == "AVG")
  55.                 {
  56.                     DropDownList dpdnlst_qlabel_avg = new DropDownList();
  57.                     dpdnlst_qlabel_avg.ID = "dpdn_avg'" + i + "' ";
  58.                     dpdnlst_qlabel_avg.Attributes["runat"] = "server";
  59.                     dpdnlst_qlabel_avg.AutoPostBack = true;
  60.                     for (int l = 1; l <= max_quest_average; l++)
  61.                     {
  62.                         dpdnlst_qlabel_avg.Items.Add(new ListItem("" + l + "", "" + l + ""));
  63.                     }
  64.                     cell2.Controls.Add(dpdnlst_qlabel_avg);
  65.                     tr.Cells.Add(cell2);
  66.                 }
  67.  
  68.                 if (Question_Label[k] == "DEF")
  69.                 {
  70.                     DropDownList dpdnlst_qlabel_def = new DropDownList();
  71.                     dpdnlst_qlabel_def.ID = "dpdn_def'" + i + "' ";
  72.                     dpdnlst_qlabel_def.AutoPostBack = true;
  73.                     for (int l = 1; l <= normal_quest_def; l++)
  74.                     {
  75.                         dpdnlst_qlabel_def.Items.Add(new ListItem("" + l + "", "" + l + ""));
  76.                     }
  77.  
  78.                     cell2.Controls.Add(dpdnlst_qlabel_def);
  79.                     tr.Cells.Add(cell2);
  80.                 }
  81.             }
  82.             dpdn_block.SelectedIndexChanged += new EventHandler(dpdn_block_SelectedIndexChanged);
  83.             dpdn_block.AutoPostBack = true;
  84.  
  85.             Dropdown_Table.Rows.Add(tr);
  86.         }
  87.     }
  88.  
  89.     void dpdnlst_qlabel_esy_SelectedIndexChanged(object sender, EventArgs e)
  90.     {
  91.         DropDownList droplist = (DropDownList)sender;
  92.         int selectedvalue = Convert.ToInt32(droplist.SelectedValue);
  93.         int max_quest_easy = (int)ViewState["max_quest_easy"];
  94.         int count = 0;
  95.         if (selectedvalue > max_quest_easy)
  96.         {
  97.             count++;
  98.         }
  99.         if (count > 1)
  100.         {
  101.             MessageBox.Show("You can  select only one time max question in any one block");
  102.  
  103.         }
  104.     }
  105.  
  106.     void dpdn_block_SelectedIndexChanged(object sender, EventArgs e)
  107.     {
  108.         DropDownList droplist = (DropDownList)sender;
  109.         Label1.Text += droplist.SelectedValue;
  110.     }
  111.  
  112.  
  113.  
  114.  
  115.  
  116.     private void display_blueprint()
  117.     {
  118.         SqlConnection con = new SqlConnection("Data Source=server;initial catalog=database;uid=;pwd=;");
  119.         con.Open();
  120.         SqlDataAdapter da_block = new SqlDataAdapter("select distinct BLOCK_CODE from OMT_New order by BLOCK_CODE", con);
  121.         DataSet ds_block = new DataSet();
  122.         da_block.Fill(ds_block);
  123.         int count_totalblock = ds_block.Tables[0].Rows.Count;
  124.         string[] block = new string[count_totalblock];
  125.         ArrayList arraylist = new ArrayList();
  126.         for (int i = 0; i < count_totalblock; i++)
  127.         {
  128.             block[i] = ds_block.Tables[0].Rows[i]["BLOCK_CODE"].ToString();
  129.  
  130.             arraylist.Add(block[i]);
  131.  
  132.         }
  133.         int arraycount = arraylist.Count;
  134.         ViewState["Block"] = block.ToArray();
  135.         arraylist.Clear();
  136.         con.Close();
  137.         SqlConnection con_def = new SqlConnection("Data Source=server;initial catalog=database;uid=;pwd=;");
  138.         con_def.Open();
  139.         SqlDataAdapter da_def = new SqlDataAdapter("select distinct DefficultyLabel from OMT_New order by DefficultyLabel", con_def);
  140.         DataSet ds_def = new DataSet();
  141.         da_def.Fill(ds_def);
  142.         int count_Def = ds_def.Tables[0].Rows.Count;
  143.         string[] Deff_Label = new string[ds_def.Tables[0].Rows.Count];
  144.         for (int k = 0; k < count_Def; k++)
  145.         {
  146.             Deff_Label[k] = ds_def.Tables[0].Rows[k]["DefficultyLabel"].ToString();
  147.  
  148.             arraylist.Add(Deff_Label[k]);
  149.  
  150.         }
  151.         ViewState["DefficultyLabel"] = Deff_Label.ToArray();
  152.  
  153.     }
please help me out ...it's urgent.........
Dec 8 '09 #1
6 4286
tlhintoq
3,525 Recognized Expert Specialist
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Dec 8 '09 #2
Frinavale
9,735 Recognized Expert Moderator Expert
Is this Really a Server Control????
It looks like an aspx page to me.

Did you set AutoPostBack=Tr ue for all DropDownLists that are supposed to post back to the server?

-Frinny
Dec 8 '09 #3
shashi shekhar singh
30 New Member
Yes I have set Autopostback=tr ue for all the DropDownLists object, but only first one is doing PostBack and remaings DropDownLists are no making PostBack. As shown in my above code I have set all the DropDwnList objects AutoPostback Property to true but only dpdn_block.Auto PostBack occurs.

and
2nd Question is that
I am developing page in which i have taken 2 update panels and all the server controls that i have needed is placed inside the panel and in <script>i have placed window.history. forward(1);</script> in that page and it's previous page script also .But when i clicked the back button of IE Explorer it posted back to the previous state ,I means to say all the controls state are changed and it behaves like a first time page_load occurs.Suppose i have a 50 buttons inside the panel and on each button click it's color has been changed and some database activity also occurs but when i clicked the back button of IE Explorer all the Button colors are changed and behaves like a first time page_load behaves. so please help me out , it's urgent for me .
Dec 9 '09 #4
Frinavale
9,735 Recognized Expert Moderator Expert
Yes I have set Autopostback=tr ue for all the DropDownLists object, but only...
Since this looks like it's a page to me (not a server control) could you please post your ASP code for the DropDownLists? I am specifically wondering if you have set the OnClick property.

all the controls state are changed and it behaves like a first time page_load
This is normal behaviour.
UpdatePanels preform asynchronous posts to the server. That means that according to the browser only 1 page request has been made: the first load.

-Frinny
Dec 9 '09 #5
shashi shekhar singh
30 New Member
Respected Sir, I am sending you a function which is being called on Page_Load() event.......... ............... ....
Expand|Select|Wrap|Line Numbers
  1.  private void checkdropdownlist(int selcted_question, string[] Block1, string[] Question_Label)
  2.     {
  3.  
  4.         int max_quest_average = Convert.ToInt32(0.14 * selcted_question);
  5.         int normal_quest_average = Convert.ToInt32(0.13 * selcted_question);
  6.         int normal_quest_def = Convert.ToInt32(0.04 * selcted_question);
  7.         int max_quest_easy = Convert.ToInt32(0.10 * selcted_question);
  8.         ViewState["max_quest_easy"] = max_quest_easy;
  9.         int normal_quest_easy = Convert.ToInt32(0.08 * selcted_question);
  10.         //     Table dtb = new Table();
  11.  
  12.         for (int i = 0; i < Block1.Length; i++)
  13.         {
  14.             TableRow tr = new TableRow();
  15.             TableCell cell1 = new TableCell();
  16.             DropDownList dpdn_block = new DropDownList();
  17.             dpdn_block.ID = "Dropdownlist" + i;
  18.             for (int j = 0; j < Block1.Length; j++)
  19.             {
  20.                 dpdn_block.Items.Add(new ListItem("" + Block1[j] + "", "" + Block1[j] + ""));
  21.             }
  22.             cell1.Controls.Add(dpdn_block);
  23.             tr.Cells.Add(cell1);
  24.             dpdn_block.SelectedIndexChanged += new EventHandler(dpdn_block_SelectedIndexChanged);
  25.             dpdn_block.AutoPostBack = true;
  26.             //Dropdown_Table.Rows.Add(tr);
  27.             for (int k = 0; k < Question_Label.Length; k++)
  28.             {
  29.  
  30.  
  31.                 if (Question_Label[k] == "ESY")
  32.                 {
  33.                     TableCell cell2 = new TableCell();
  34.                     DropDownList dpdnlst_qlabel_esy = new DropDownList();
  35.                     dpdnlst_qlabel_esy.ID = "dpdn_easy'" + i + "' ";
  36.                     for (int l = 1; l <= max_quest_easy; l++)
  37.                     {
  38.                         dpdnlst_qlabel_esy.Items.Add(new ListItem("" + l + "", "" + l + ""));
  39.  
  40.                     }
  41.                     cell2.Controls.Add(dpdnlst_qlabel_esy);
  42.                     tr.Cells.Add(cell2);
  43.                     dpdnlst_qlabel_esy.SelectedIndexChanged += new EventHandler(dpdnlst_qlabel_esy_SelectedIndexChanged);
  44.                     dpdnlst_qlabel_esy.AutoPostBack = true;
  45.  
  46.  
  47.  
  48.  
  49.                 }
  50.                 if (Question_Label[k] == "AVG")
  51.                 {
  52.                     TableCell cell2 = new TableCell();
  53.                     DropDownList dpdnlst_qlabel_avg = new DropDownList();
  54.                     dpdnlst_qlabel_avg.ID = "dpdn_avg'" + i + "' ";
  55.                     //dpdnlst_qlabel_avg.Attributes["runat"] = "server";
  56.  
  57.                     for (int l = 1; l <= max_quest_average; l++)
  58.                     {
  59.                         dpdnlst_qlabel_avg.Items.Add(new ListItem("" + l + "", "" + l + ""));
  60.  
  61.                     }
  62.  
  63.                     dpdnlst_qlabel_avg.SelectedIndexChanged += new EventHandler(dpdnlst_qlabel_avg_SelectedIndexChanged);
  64.                     dpdnlst_qlabel_avg.AutoPostBack = true;
  65.                     cell2.Controls.Add(dpdnlst_qlabel_avg);
  66.                     tr.Cells.Add(cell2);
  67.  
  68.  
  69.  
  70.                 }
  71.                 if (Question_Label[k] == "DEF")
  72.                 {
  73.                     TableCell cell2 = new TableCell();
  74.                     DropDownList dpdnlst_qlabel_def = new DropDownList();
  75.                     dpdnlst_qlabel_def.ID = "dpdn_def'" + i + "' ";
  76.  
  77.                     for (int l = 1; l <= normal_quest_def; l++)
  78.                     {
  79.                         dpdnlst_qlabel_def.Items.Add(new ListItem("" + l + "", "" + l + ""));
  80.  
  81.                     }
  82.                     dpdnlst_qlabel_def.SelectedIndexChanged += new EventHandler(dpdnlst_qlabel_def_SelectedIndexChanged);
  83.                     dpdnlst_qlabel_def.AutoPostBack = true;
  84.                     cell2.Controls.Add(dpdnlst_qlabel_def);
  85.                     tr.Cells.Add(cell2);
  86.  
  87.                 }
  88.  
  89.  
  90.  
  91.             }
  92.  
  93.             Dropdown_Table.Rows.Add(tr);
  94.         }
  95.  
  96.     }
  97.  
  98.     void dpdnlst_qlabel_def_SelectedIndexChanged(object sender, EventArgs e)
  99.     {
  100.         //throw new NotImplementedException();
  101.     }
  102.  
  103.     void dpdnlst_qlabel_avg_SelectedIndexChanged(object sender, EventArgs e)
  104.     {
  105.         //throw new NotImplementedException();
  106.     }
  107.  
  108.     void dpdnlst_qlabel_esy_SelectedIndexChanged(object sender, EventArgs e)
  109.     {
  110.         //throw new NotImplementedException();
  111.     }
  112.  
  113.     void dpdn_block_SelectedIndexChanged(object sender, EventArgs e)
  114.     {
  115.  
  116.  
  117.         //throw new NotImplementedException();
  118.     }
and 2nd question Reply...
can you help me how to overcome this problem, if you have any other idea then please suggest me.....
Dec 10 '09 #6
Frinavale
9,735 Recognized Expert Moderator Expert
Please see this article on how to use dynamic controls in asp.net.

There is a good reason why you aren't able to detect the selected index changed event on the server. It's explained in that article.

As for the second "problem".
Feel free to ask the JavaScript experts in the JavaScript forum but I don't think that you can "fix" this problem because it's not actually a problem to fix. This is normal behaviour.

-Frinny
Dec 10 '09 #7

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

Similar topics

1
5650
by: Mike | last post by:
I create a dynamic control and added it to a panel. I get the selected value of the control via a btn_click() but my problem is everytime the page is postback the selected values go back to the first element in the dropdownlist. but if i call the control creation with if !=postback then the controls disappear. and selected values = null. Any help on this would be great
4
5493
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new dropdownlist to the screen for selection. This continues until there are no children, and then it checks for a help article list based on that last selection and displays actual articles for display. Adding the controls and getting everything...
5
3527
by: Amelyan | last post by:
How can I get state of dynamically created controls (RadioButton, CheckBox, TextBox.Text) on post back when I click submit button? The only way I know is by traversing Response.Form enumberator; Response.Form.GetEnumerator(), etc. while, identifying specific controls by programmatically assigned unique id (e.g. MyButton_AnswerID_123). However, I am not sure if that is the proper way. What is the common practice?
6
2047
by: Alan Silver | last post by:
Hello, I have a placeholder, which gets a user control added in when the page first loads. This user control contains a dropdownlist, and I would like to get hold of the value of this drop down list form the calling page on postback. Can I do this? If I try and use FindControl on the placeholder, it can't find the dropdownlist. I tried...
1
2097
by: Geoffrey van den Ouden | last post by:
I'm converting this project from ASP.NET 1.1 to 2.0, and the thing I came across is that my dropdownlist no returns the content of the Value property in a correct way. I'm not using a direct Database connection or anything, most of my dropdownlists are filled with values from static singletons that are running or just some code that is generated at page_load. The basic thing is that on page_load I always create a ListItemCollection...
1
3022
by: RSH | last post by:
Hi, I am experimenting with the Viewstate and based on a few articles I have read, I put together a test. it is a simple test where I am dynamically creating a DropDownList that contains 25000 list items. When I don't create the control dynamically, I get a viewstate that is roughly 1 mb in size! However, when i use the code below the viewstate is 0 bytes even on postback. Impressive size difference. Now where I get perplexed is even...
5
9553
by: vcuankitdotnet | last post by:
Hi. I have tried to search for this online, but can't find what I'm looking for so I decided to come here. I have a ASP.NET 2.0 website with a masterpage. My .aspx page uses the masterpage. Here is the HTML from my .aspx page: <asp:Content ID="Content" ContentPlaceHolderID="MyContentArea" runat="server"> <asp:PlaceHolder ID="phControlHolder" runat="server"></ asp:PlaceHolder><br /><br /> <asp:Button ID="btnSubmitForm" runat="server"...
2
3933
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite well, so at times it is tempting just to port parts of it over mostly as-is. In fact, one MSDN article I read suggested using straight HTML wherever possible to make the app more efficient and less resource demanding. On one page there are 2...
4
4770
by: =?Utf-8?B?RHlsYW5TbWl0aA==?= | last post by:
I have a WebForm where I'm dynamically creating some controls and I'm having difficulty understanding how the state is being persisted and how to work with it. I've created a simplified example to demonstrate my issues. Lets say I have a WebForm with a DropDownList where the user selects a number from 1 to 10 (the DropDownList is not dynamically created). I also have a button on there that I use to trigger a PostBack. Based on the...
0
9916
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
9762
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11056
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10696
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...
0
9531
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7932
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
5761
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
5958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3201
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.