Advertisement
Guest User

C#

a guest
May 7th, 2011
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. namespace Dynamic
  9. {
  10.     public partial class DynamicControls : System.Web.UI.Page
  11.     {
  12.         //private property to access the ControlsList in ViewState
  13.         private List<string> ControlsList
  14.         {
  15.             get
  16.             {
  17.                 if (ViewState["controls"] == null)
  18.                     ViewState["controls"] = new List<string>();
  19.  
  20.                 return (List<string>)ViewState["controls"];
  21.             }
  22.         }
  23.  
  24.         protected void Page_Load(object sender, EventArgs e)
  25.         {
  26.             configTextBox.Width = 400;
  27.  
  28.             if (IsPostBack)
  29.             {
  30.                 string config = configTextBox.Text;
  31.  
  32.                 foreach (string labelID in ControlsList)
  33.                 {
  34.                     Label label = new Label();
  35.                     label.ID = label.Text = labelID;
  36.                     label.CssClass = "LabelClass";
  37.                     label.Style.Add(HtmlTextWriterStyle.BackgroundColor, "lime");
  38.  
  39.                     label.Style.Add(HtmlTextWriterStyle.Position, "absolute");
  40.                     label.Style.Add(HtmlTextWriterStyle.Top, getFirstNumber(ref config).ToString() + "px");
  41.                     label.Style.Add(HtmlTextWriterStyle.Left, getFirstNumber(ref config).ToString() + "px");
  42.                     label.Style.Add(HtmlTextWriterStyle.Height, getFirstNumber(ref config).ToString() + "px");
  43.                     label.Style.Add(HtmlTextWriterStyle.Width, getFirstNumber(ref config).ToString() + "px");
  44.  
  45.                     Container.Controls.Add(label);  // add it to the page
  46.                 }
  47.             }
  48.         }
  49.  
  50.         protected void addLabelButton_Click(object sender, EventArgs e)
  51.         {
  52.             Label label = new Label();
  53.             label.ID = label.Text = "Label" + NextID.ToString();
  54.             label.CssClass = "LabelClass";
  55.             label.Style.Add(HtmlTextWriterStyle.BackgroundColor, "lime");
  56.  
  57.             Container.Controls.Add(label);  // add it to the page
  58.             ControlsList.Add(label.ID); // save the id
  59.         }
  60.  
  61.         protected int NextID
  62.         {
  63.             get
  64.             {
  65.                 return ControlsList.Count + 1;
  66.             }
  67.         }
  68.  
  69.         protected int getFirstNumber(ref string line)
  70.         {
  71.             int position = 0;
  72.             int value = 0;
  73.  
  74.             while (position < line.Length && line[position] != ' ')
  75.                 value = 10 * value + line[position++] - '0';
  76.  
  77.             if (position + 1 < line.Length)
  78.                 line = line.Substring(position + 1);
  79.  
  80.             return value;
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement