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

postback problem in creation of dynamic dropdownlist.

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 4248
tlhintoq
3,525 Expert 2GB
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 Expert Mod 8TB
Is this Really a Server Control????
It looks like an aspx page to me.

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

-Frinny
Dec 8 '09 #3
Yes I have set Autopostback=true 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.AutoPostBack 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 Expert Mod 8TB
Yes I have set Autopostback=true 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
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 Expert Mod 8TB
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
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...
4
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...
5
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;...
6
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...
1
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...
1
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...
5
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...
2
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...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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
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
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...

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.