1

I am trying to create a list of DDL using code behind as you can see here :

public List<DropDownList> ddll = new List<DropDownList>();
for (int i = 0; i < 15; i++)
{
    DropDownList ansList = new DropDownList();
    ansList.AutoPostBack = false;
    ansList.DataSource = values1;
    ansList.DataBind();
    ddll.Add(ansList);
 }

As you can see i set the autopostback attribute on false .But it doesn't work any my pages are refreshed when the user changes the selectedindex.

I added DDL using this :

Span1.Controls.Add(ddll[0]);
Span2.Controls.Add(ddll[1]);
Span3.Controls.Add(ddll[2]);
Span4.Controls.Add(ddll[3]);
Span5.Controls.Add(ddll[4]);
Span6.Controls.Add(ddll[5]);
Span7.Controls.Add(ddll[6]);
Span8.Controls.Add(ddll[7]);
Span9.Controls.Add(ddll[8]);
Span10.Controls.Add(ddll[9]);
Span11.Controls.Add(ddll[10]);
Span12.Controls.Add(ddll[11]);
Span13.Controls.Add(ddll[12]);
Span14.Controls.Add(ddll[13]);
Span15.Controls.Add(ddll[14]);

In html code i have this :

<span style="color:#ea0000;padding:0 10px;" id="Span6" runat="server"></span>
Adil
  • 146,340
  • 25
  • 209
  • 204
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180

2 Answers2

1

Look at your code

       for (int i = 0; i < 15; i++)
        {
            DropDownList ansList = new DropDownList();
            ansList.AutoPostBack = false; // Here You have set it false
            ansList.DataSource = values1;
            ansList.DataBind();
            ansList.AutoPostBack = true; // Here You have set it true again
            ddll.Add(ansList);
        }
Rajeev Kumar
  • 4,901
  • 8
  • 48
  • 83
1

You first set AutoPostBack to false and later to true right after two statements. The false is overwritten by true and it should do postback now when the selected index is changed.

ansList.AutoPostBack = false;
   //...    

ansList.AutoPostBack = true;

Edit You can also use loop to add the list in the spans using FindControl(string id) to get the spans by id.

for(int i=0; i < 15; i++)
   this.FindControl("Span"+i).Add(ddll[i]);
Adil
  • 146,340
  • 25
  • 209
  • 204