Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace Dynamic
- {
- public partial class DynamicControls : System.Web.UI.Page
- {
- //private property to access the ControlsList in ViewState
- private List<string> ControlsList
- {
- get
- {
- if (ViewState["controls"] == null)
- ViewState["controls"] = new List<string>();
- return (List<string>)ViewState["controls"];
- }
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- configTextBox.Width = 400;
- if (IsPostBack)
- {
- string config = configTextBox.Text;
- foreach (string labelID in ControlsList)
- {
- Label label = new Label();
- label.ID = label.Text = labelID;
- label.CssClass = "LabelClass";
- label.Style.Add(HtmlTextWriterStyle.BackgroundColor, "lime");
- label.Style.Add(HtmlTextWriterStyle.Position, "absolute");
- label.Style.Add(HtmlTextWriterStyle.Top, getFirstNumber(ref config).ToString() + "px");
- label.Style.Add(HtmlTextWriterStyle.Left, getFirstNumber(ref config).ToString() + "px");
- label.Style.Add(HtmlTextWriterStyle.Height, getFirstNumber(ref config).ToString() + "px");
- label.Style.Add(HtmlTextWriterStyle.Width, getFirstNumber(ref config).ToString() + "px");
- Container.Controls.Add(label); // add it to the page
- }
- }
- }
- protected void addLabelButton_Click(object sender, EventArgs e)
- {
- Label label = new Label();
- label.ID = label.Text = "Label" + NextID.ToString();
- label.CssClass = "LabelClass";
- label.Style.Add(HtmlTextWriterStyle.BackgroundColor, "lime");
- Container.Controls.Add(label); // add it to the page
- ControlsList.Add(label.ID); // save the id
- }
- protected int NextID
- {
- get
- {
- return ControlsList.Count + 1;
- }
- }
- protected int getFirstNumber(ref string line)
- {
- int position = 0;
- int value = 0;
- while (position < line.Length && line[position] != ' ')
- value = 10 * value + line[position++] - '0';
- if (position + 1 < line.Length)
- line = line.Substring(position + 1);
- return value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement