Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I save the controls that are created on button click in an list. The and then create them on loadviewstate but i Cannot figure out how to create the controls in one line in the place holder. I dont want it to create a control on a line. So in the foreach loop the both controls must me added to the same line in the placeholder but I cant seem to figure it out any help would be appreciated.


C#
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;

public partial class test : System.Web.UI.Page
{


    List<string> controlIdList = new List<string>();//to store control Id on each postback
    int counter = 0;//count Number of Comboboxes

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);

        controlIdList = (List<string>)ViewState["controlIdList"];

        Table tb = new Table();
        TableRow tr = new TableRow();
        TableCell tc = new TableCell();
        TableCell tc2 = new TableCell();

        foreach (string id in controlIdList)
        {
         

            counter++;
        
           
                ASPxComboBox Cb = new ASPxComboBox();
                Cb.ID = id;
                Cb.DataSource = sdSku;
                Cb.TextField = "SkU";
                Cb.ValueField = "SkU";
                Cb.ValueType = typeof(string);
                Cb.Columns.Add("SKU");
                Cb.Columns.Add("Model");
                Cb.Columns.Add("Name");
                Cb.Attributes.Add("runat", "server");
                Cb.DataSource = sdSku;
                Cb.DataBind();
               // PlaceholderSku.Controls.Add(Cb);
               // PlaceholderSku.Controls.Add(new LiteralControl("<br/>"));
                tc2.Controls.Add(Cb);
                tr.Cells.Add(tc2);
                tb.Rows.Add(tr);
             

            
         
                ASPxLabel Lb = new ASPxLabel();
                Lb.Text = "Select SKU";
                Lb.ID = id;
                //PlaceholderSku.Controls.Add(Lb);
                tc.Controls.Add(Lb);
                tr.Cells.Add(tc);
                tb.Controls.Add(tr); tb.ID = "table" + counter;                
                
          

        }
        PlaceholderSku.Controls.Add(tb);
       
        PlaceholderSku.Controls.Add(new LiteralControl("<br/>"));

    }


    protected void Page_Load(object sender, EventArgs e)
    {
              
    }
    protected void btnAddSku_Click(object sender, EventArgs e)
    {
        counter++;
        ASPxComboBox Cb = new ASPxComboBox();
        Cb.ID = "SkuComboBox" + counter;

        ASPxLabel Lb = new ASPxLabel();       
        Lb.ID = "label" + counter;

        controlIdList.Add(Lb.ID);
        controlIdList.Add(Cb.ID);

        ViewState["controlIdList"] = controlIdList;//storing ids
       

    }
}


--Edit: shouting removed, code block formatted with pre tags
Posted
Updated 18-Feb-14 23:35pm
v4
Comments
Pheonyx 19-Feb-14 5:26am    
I have edited your question to remove the capitals, on the internet all capitals is deemed to be shouting and is considered rude. I've also added pre tags around your code to make it easier for someone to read.
mrDivan 19-Feb-14 5:29am    
thanks
Nicholas Marty 19-Feb-14 5:29am    
Might be best to add what exactly you can't figure out. What doesn't work, how exactly should it work. Use <code> blocks to format the code and make it more readable. And please don't shout. All uppercase is considered shouting on the internet (while using all lowercase is considered childish). Oh and if we're at it: Add some punctuation so that one might at least hope to be able to understand what you're trying to do :)

1 solution

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;

public partial class eOPS_Orders_FNB_Stock_PurchaseOrder : System.Web.UI.Page
{


    List<string> controlIdList = new List<string>();//to store control Id on each postback
    int counter = 0;//count Number of Comboboxes

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);

        controlIdList = (List<string>)ViewState["controlIdList"];

      

        foreach (string id in controlIdList)
        {
         

            counter++;
        
                //create new Combobox
                ASPxComboBox Cb = new ASPxComboBox();
                Cb.ID = "SkuComboBox" + counter;
                Cb.DataSource = sdSku;
                Cb.TextField = "SkU";
                Cb.ValueField = "SkU";
                Cb.ValueType = typeof(string);
                Cb.Columns.Add("SKU");
                Cb.Columns.Add("Model");
                Cb.Columns.Add("Name");
                Cb.Attributes.Add("runat", "server");
                Cb.DataSource = sdSku;
                Cb.DataBind();

                //create new Label     
                ASPxLabel Lb = new ASPxLabel();
                Lb.Text = "Select SKU";
                Lb.ID = "Label"+counter;

                //Create new Table add the above created controls to the table
                Table tb = new Table();
                TableRow tr = new TableRow();
                TableCell tc = new TableCell();
                TableCell tc2 = new TableCell();
                tc.Controls.Add(Lb);
                tr.Cells.Add(tc);
                tb.Controls.Add(tr); 
                tb.ID = id;
                tc2.Controls.Add(Cb);
                tr.Cells.Add(tc2);
                tb.Rows.Add(tr);


              //add table to the Pade 
                PlaceholderSku.Controls.Add(tb);
                PlaceholderSku.Controls.Add(new LiteralControl("<br/>"));
        }
       

    }


    protected void Page_Load(object sender, EventArgs e)
    {
              
    }
    protected void btnAddSku_Click(object sender, EventArgs e)
    {
        counter++; 
        Table tb = new Table();
        tb.ID = "Table" + counter;
        controlIdList.Add(tb.ID);       
        ViewState["controlIdList"] = controlIdList;//storing ids
       
    }
}
 
Share this answer
 
Comments
mrDivan 19-Feb-14 6:01am    
I fixed it just need to rethink the process

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900