By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,266 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,266 IT Pros & Developers. It's quick & easy.

postback problem in creation of dynamic dropdownlist.

P: 30
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
Share this Question
Share on Google+
6 Replies


tlhintoq
Expert 2.5K+
P: 3,525
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
Expert Mod 5K+
P: 9,731
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

P: 30
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
Expert Mod 5K+
P: 9,731
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

P: 30
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
Expert Mod 5K+
P: 9,731
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

Post your reply

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