Member Avatar for ragnarok511

Hi everyone,

I am working on a form and I am trying to add buttons back and submit when I go to another page, but I keep getting the error "Collection was modified; enumeration operation may not execute." whenever it tries to create the buttons and add them. I am generating these buttons from a C# script on my web page.

<asp:TableCell>
              <asp:DropDownList ID="DropDownList1" runat="server">
              <asp:ListItem>1</asp:ListItem>
              <asp:ListItem>2</asp:ListItem>
              <asp:ListItem>3</asp:ListItem>
              <asp:ListItem>4</asp:ListItem>
              </asp:DropDownList>
          </asp:TableCell>
      </asp:TableRow>
      </asp:Table>
      
      <% if (questionIndex > 0) //it is this and the other script that are giving me the problems
         {
          
             Button back = new Button();
             back.Text = "Back >";
             Page.Controls.Add(back);
         }
          %>
      
          <asp:Button ID="next" runat="server" Text="Next >" onclick="next_Click" />
          
              <asp:Button ID="cancel" runat="server" Text="Cancel" />
              <% if (questionIndex > 0)
                                {
                                   
                                    Button submit = new Button();
                                    submit.Text = "Submit";
                                    Page.Controls.Add(submit);
                               }
                                 %>

Thank you,
Brian Wimpsett

I guess you are experiencing loading and handling problems with the dynamically added buttons for Next and submit.
Not exactly but below i have listed the possible way for dynamically populating Next and Back buttons and setting up event handler for them. You can copy the code and make corrosponding changes to reflect your settings.

<form id="form1" runat="server">
    <div>
	Page: <asp:DropDownList ID="ddlPagination" AutoPostBack="true" runat="server">
		<asp:ListItem Text="one" Value="1" />
		<asp:ListItem Text="two" Value="2" />
		<asp:ListItem Text="three" Value="3" />
		<asp:ListItem Text="four" Value="4" />
		<asp:ListItem Text="five" Value="5" />
	</asp:DropDownList>
	<asp:Panel ID="pnlNavigation" runat="server" />
    </div>
    </form>
#region "Members"

	public int pageNumber
	{
		get {  return (int)ViewState["pageNumber"]; }
		set { ViewState["pageNumber"] = value; }
	}

	#endregion

	#region "Page Load"
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			pageNumber = 1;
		}
		Rendercontrols(pageNumber);

	}
	#endregion

	#region "Events"

	protected void ddlPagination_SelectedIndexChanged(object sender, System.EventArgs e)
	{
		string value = ddlPagination.SelectedValue;
		pageNumber = Convert.ToInt32(value);
		GoToPage(pageNumber);
	}

	protected void btnBack_Click(object sender, EventArgs e)
	{
		Button btnBack = (Button)sender;
		int number = 1;
		int.TryParse(btnBack.CommandArgument, out number);
		pageNumber = number;
		GoToPage(pageNumber);
	}

	protected void btnNext_Click(object sender, EventArgs e)
	{
		Button btnNext = (Button)sender;
		int number = 5;
		int.TryParse(btnNext.CommandArgument, out number);
		pageNumber = number;
		GoToPage(pageNumber);
	}

	#endregion

	#region "Methods"


	public void Rendercontrols(int pageNumber)
	{
		if (pageNumber > 1)
		{
			Button btnBack = new Button();
			{
				btnBack.Text = "<< Back";
				btnBack.ID = "btnBack";
				btnBack.CommandArgument = Convert.ToString( pageNumber - 1);
			}
			btnBack.Click += btnBack_Click;
			pnlNavigation.Controls.Add(btnBack);
		}
		if (pageNumber < 5)
		{
			Button btnNext = new Button();
			{
				btnNext.Text = ">> Next";
				btnNext.ID = "btnNext";
				btnNext.CommandArgument = Convert.ToString(pageNumber + 1);
			}
			btnNext.Click += btnNext_Click;
			pnlNavigation.Controls.Add(btnNext);
		}

	}

	public void GoToPage(int pageNumber)
	{
		pnlNavigation.Controls.Clear();
		ddlPagination.SelectedIndex = -1;
		ddlPagination.Items.FindByValue(Convert.ToString(pageNumber)).Selected = true;
		Rendercontrols(pageNumber);
	}
	#endregion

Hope this works :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.