Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2004
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dynamically adding controls

    Hello all,

    I have a form on which the user can dynamically add fields to enter upto 3 email addresses. I've done this by have an "add email" and "remove email" button. Each button calls a method that increments or decrements a session variable and then causes a response.redirect to the current page. In the Page_Load i then call another method, "addemail" that actually dynamically adds the correct number of email fields based in the value of the session variable. The only way i can get this to work is by using a response.redirect but the problem is that after the response.redirect is performed everything in the fields is lost. I've tried re-populating the fields using Request.Form["id_of_textbox"] in the populateFields() method but that doesn't seem to work.

    The .aspx and codebehind pages are below. Any ideas anyone???

    .aspx
    Code:
    <form id="Form1" method="post" action="" runat="server">
    <h3>PERSONAL DETAILS</h3>
    <fieldset>
    <label for="firstName">First Name:</label><input id="firstName" name="firstName" type="text" size="30"  runat="server" />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" CssClass="validationerror" ControlToValidate="firstName"  EnableClientScript="false" Display="Static" runat="server">Required field</asp:RequiredFieldValidator><br />
    <label for="lastName">Last Name:</label><input id="lastName" name="lastName" type="text" size="30"  runat="server" />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" CssClass="validationerror" ControlToValidate="lastName" EnableClientScript="false" runat="server">Required field</asp:RequiredFieldValidator><br />
    </fieldset>
    <h3>CONTACT DETAILS</h3>
    <fieldset>
    <asp:PlaceHolder id="emails" runat="server" />
    <asp:Button ID="add_email" OnClick="addEmailButton" runat="server" Text="Add an email" />
    <asp:Button ID="remove_email" OnClick="removeEmailButton" runat="server" Text="Remove last email" Visible="false" /><br />
    </fieldset>
    </br />
    <h3>WEBSITE</h3>
    <fieldset>
    <label for="webSite">WebSite:</label><input id="webSite" name="webSite" type="text" size="30"  />
    </fieldset>
    <asp:Button ID="Submit" Text="Submit" runat="server" />
    </form>
    codebehind (.aspx.cs)
    Code:
    public partial class WriteToDb : System.Web.UI.Page
    {
        public System.Web.UI.HtmlControls.HtmlInputText firstName;
        public System.Web.UI.WebControls.PlaceHolder emails;
        public System.Web.UI.WebControls.Button add_email;
        public System.Web.UI.WebControls.Button remove_email;
        public System.Web.UI.WebControls.Button Submit;
    
        public void Page_Load(Object sender, EventArgs E)
        {
            if (Session["no_of_emails"] != null && (int)Session["no_of_emails"] == 3)
                {
                    add_email.Visible = false;
                }
                else
                {
                    add_email.Visible = true;
                }
    
            if (Session["no_of_emails"] == null || (int)Session["no_of_emails"] < 1)
                {
                remove_email.Visible = false;
                }
                else
                {
                addEmail();
                remove_email.Visible = true;
                }
            populateFields();
        }
    
        public void addEmailButton(Object sender, EventArgs E)
        {
            if (Session["no_of_emails"] == null)
            {
                Session["no_of_emails"] = 1;
                Response.Redirect("Default.aspx", false);
            }
            else
            {
                Session["no_of_emails"] = ((int)Session["no_of_emails"]) + 1;
                Response.Redirect("Default.aspx", false);
            }
        }
    
        public void removeEmailButton(Object sender, EventArgs E)
        {
            Session["no_of_emails"] = ((int)Session["no_of_emails"]) - 1;
            Response.Redirect("Default.aspx");
        }
    
        public void addEmail(/*int x*/)
        {
            for (int i = 1; i < (int)Session["no_of_emails"] + 1; i++)
            {
                HtmlGenericControl label = new HtmlGenericControl("label");
                label.Attributes.Add("for", "Textbox" + i.ToString());
                label.InnerHtml = "Email" + i.ToString() + ":";
                emails.Controls.Add(label);
    
                TextBox t = new TextBox();
                t.ID = "TextBox" + i.ToString();
                t.Attributes.Add("size", "30");
                emails.Controls.Add(t);
                emails.Controls.Add(new LiteralControl("<br />"));
            }
        }
    
        public void populateFields()
        {
            firstName.Value = Request.Form["firstName"];
        }
    }

  • #2
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,096
    Thanks
    2
    Thanked 23 Times in 23 Posts
    I'm just guessing, but you might be able to use the Server.Transfer method.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #3
    New Coder
    Join Date
    Mar 2006
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by vinyl-junkie View Post
    I'm just guessing, but you might be able to use the Server.Transfer method.
    BTW thanks for trying to help.

    It seems that server.transfer will work in gathering the context from a previous page and adding them to a control on the second page, but my problem would be when I write to a database I don't get the info. Seems as if server.transfer only works on the Page_Load Sub. This is driving me nuts!

  • #4
    New Coder
    Join Date
    Oct 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You probably can just use the pre-render event and forget a reload.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •