...

View Full Version : Dynamically adding controls



mat106
01-31-2006, 08:43 PM
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"];
}
}

vinyl-junkie
02-01-2006, 04:31 AM
I'm just guessing, but you might be able to use the Server.Transfer method (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/html/d47bdda3-96e1-46c3-b872-a038c2a61317.asp).

binici
10-18-2006, 05:34 PM
I'm just guessing, but you might be able to use the Server.Transfer method (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/html/d47bdda3-96e1-46c3-b872-a038c2a61317.asp).

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! :confused:

JiltedCitizen
10-19-2006, 06:11 PM
You probably can just use the pre-render event and forget a reload.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum