Thursday, July 22, 2010

How to create controls in a button event -

If you add dropdownlist and texbox in button click, then when you fire up another event, inside the second event first button event (control creation logic) wont get fired. So there will not be textbox and dropdownlist in the next postback. In-order to use viewstate you need to create your dynamic controls at least in the page init method.
For this kind of senario you can use hidden field to determine the control creation button clicked and you need to find wihich button clicked in the page init method

<%@ Page Language="C#" %>
<html xmlns="">
<head id="Head1" runat="server">
    <script runat="server">
        protected bool ControlsCreated { getset; }
        protected override void OnInit(EventArgs e)
            string controlsCreated = this.Request.Form[this.hdnControlsCreated.UniqueID];
            if (!string.IsNullOrEmpty(controlsCreated))
                this.ControlsCreated = bool.Parse(controlsCreated);
            if (this.IsPostBack)
                string uniqueId = this.Request.Form["__EVENTTARGET"];
                if (!string.IsNullOrEmpty(uniqueId))
                    string[] parts = uniqueId.Split("$".ToCharArray());
                    string id = parts[parts.Length - 1];
                    if (this.btnCreateControls.ID.Equals(id))
                        this.ControlsCreated = true;                       
            if (this.ControlsCreated)
                List<string> items = new List<string>();
                items.Add("Item 1");
                items.Add("Item 2");               
                DropDownList ddlItems = new DropDownList();
                ddlItems.ID = "ddlItems";
                ddlItems.AutoPostBack = true;
                ddlItems.SelectedIndexChanged += new EventHandler(SelectedIndexChanged);
                ddlItems.DataSource = items;
                TextBox txtName = new TextBox() { ID = "txtName" };
        private void SelectedIndexChanged(object sender, EventArgs e)
        private void GetValues(object sender, EventArgs e)
            string result = string.Empty;
            TextBox txtNames = this.FindControl("txtName"as TextBox;
            DropDownList ddlItems = this.FindControl("ddlItems"as DropDownList;
            if (txtNames != null)
                result += string.Format(" Textbox Value: {0}", txtNames.Text);
            if (ddlItems != null)
                result += string.Format(" Dropdownlist Value: {0}", ddlItems.SelectedItem.Value);
            this.lblResults.Text = result;
        protected override void OnLoad(EventArgs e)
            this.hdnControlsCreated.Value = this.ControlsCreated.ToString();
    <form id="form1" runat="server">
        <asp:Panel runat="server" ID="pnlContainer">
                Text="Create Controls"
                UseSubmitBehavior="false" />
                Text="Get Values"
                OnClick="GetValues" />
            <hr />
            <asp:PlaceHolder runat="server" ID="phControls"></asp:PlaceHolder>
            <asp:Label runat="server" ID="lblResults" />
            <asp:HiddenField runat="server" ID="hdnControlsCreated" />

No comments:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...