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>
0

2 Answers 2

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]);
2
  • Sorry it was my syntax fault ,i changed it and again same error Jul 17, 2014 at 7:24
  • Build the page the refresh it using ctrl+F5 it should not do postback
    – Adil
    Jul 17, 2014 at 7:26
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);
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.