Dynamically adding controls

admin

Administrator
Staff member
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

<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)

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"];
}
}Why are you doing Response.Redirect? In doing so you are losing your ViewState as well as your Request data.
 
Back
Top