Thursday, July 22, 2010

How to create controls in a button event - Asp.net

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="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <script runat="server">
        protected bool ControlsCreated { getset; }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(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;
                this.phControls.Controls.Add(ddlItems);
                TextBox txtName = new TextBox() { ID = "txtName" };
                this.phControls.Controls.Add(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)
        {
            base.OnLoad(e);
            this.DataBind();
            this.hdnControlsCreated.Value = this.ControlsCreated.ToString();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel runat="server" ID="pnlContainer">
            <asp:Button
                runat="server"
                ID="btnCreateControls"
                Text="Create Controls"
                UseSubmitBehavior="false" />
            <asp:Button
                runat="server"
                ID="Button1"
                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" />
        </asp:Panel>
    </div>
    </form>
</body>
</html>
 
 

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...