I need to know if it is possible to call a control and also attach it’s events from a class. I have been researching the internet for a some valuable information but to no avail. Below is a simple illustration of what I intend achieving.
PAGE
protected void Page_Load(object sender, EventArgs e)
{
DynamicControls(IsPostBack);
}
public void DynamicControls(bool posting_back)
{
ControlHandler ch = new ControlHandler();
CreateTextbox(item.id, item.value, item.textMode, item.mandatoryInput, item.maxLength,int.Parse(item.rowNumber), int.Parse(item.colNumber), item.visible, item.autoPostBack,item.enable, table);
}
CLASS
public void CreateTextbox(String id, String value, String textMode, bool mandatoryInput
, String maxLength, int rowNumber, int colNumber, bool visible, bool autopostBack, bool enable, Table table)
{
TextBox tb = new TextBox();
tb.ID = id;
tb.Text = value == null ? "" : value;
tb.TextMode = textMode == null ? TextBoxMode.SingleLine : textMode.ToLower() == "multiline" ? TextBoxMode.MultiLine : TextBoxMode.SingleLine;
tb.MaxLength = maxLength == null ? 32500 : int.Parse(maxLength);
tb.Visible = visible;
tb.Style.Add("width", "80%");
tb.Enabled = enable;
tb.AutoPostBack = autopostBack;
tb.Font.Bold = true;
tb.ForeColor = System.Drawing.Color.Chocolate;
tb.TextChanged += new EventHandler(tb_TextChanged);
}
protected void tb_TextChanged(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
tb.Text = //some values or display in another control in the form
}
Thanks
It is possible to listen to events yes. Though dynamic controls should always be created during page preinit or page init, not load.