Reputation: 11678
I'm building a user control that is a multiple text box generator. it gives you the ability to create as much textboxs as you need..
My problem is that every time i'm using the btnAdd to add a new TextBox, the .Net is sending a post back to the server - which removes all the text inside the current text boxes..
Is there a way that i can catch the text in the textbox before the callback in order to re-render the user control with the text inside ?
HTML part:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MultipleTextBox.ascx.cs"
Inherits="MultipleTextBox" %>
<asp:ScriptManager runat="server" ID="scriptManager">
</asp:ScriptManager>
<asp:UpdatePanel ID="updatePanel" runat="server">
<contenttemplate>
<div id="container" runat="server">
</div>
<asp:Button runat="server" Text="+" ID="btnAdd" OnClick="btnAdd_Click"></asp:Button>
<asp:Button runat="server" Text="-" ID="btnRemove" onclick="btnRemove_Click"></asp:Button>
</contenttemplate>
</asp:UpdatePanel>
Code:
public partial class MultipleTextBox : System.Web.UI.UserControl
{
/// <summary>
/// The Session key for text box list
/// </summary>
private readonly string SESSION_TEXTBOX_LIST = "textboxList";
/// <summary>
/// The Session key for MultipleTextBox name
/// </summary>
private readonly string SESSION_NAME = "multipleTextBoxName";
/// <summary>
/// The List of TextBox instances
/// </summary>
private List<TextBox> textBoxList;
/// <summary>
/// The basic name of this multiple text box
/// </summary>
private string name;
protected void Page_Load(object sender, EventArgs e)
{
//disable the validation using does buttons
btnAdd.CausesValidation = false;
btnRemove.CausesValidation = false;
//if it's not a post back - intialize the page
if (!IsPostBack)
{
Session[SESSION_TEXTBOX_LIST] = null;
Session[SESSION_NAME] = null;
textBoxList = new List<TextBox>();
Session[SESSION_TEXTBOX_LIST] = textBoxList;
btnRemove.Visible = false;
return;
}
//get the name
if (Session[SESSION_NAME] != null)
name = (string)Session[SESSION_NAME];
//get the text box list
if (Session[SESSION_TEXTBOX_LIST] != null)
textBoxList = (List<TextBox>)Session[SESSION_TEXTBOX_LIST];
}
protected void btnAdd_Click(object sender, EventArgs e)
{
//add a new text box to the text box list
textBoxList.Add(createTextBox());
//show the text boxes
ShowTextboxes(false);
if (textBoxList.Count > 0)
btnRemove.Visible = true;
}
protected void btnRemove_Click(object sender, EventArgs e)
{
//show the text boxes
ShowTextboxes(true);
//set the visibility of the remove button
if (textBoxList.Count == 0)
btnRemove.Visible = false;
}
/// <summary>
/// Set the buttons for this control
/// </summary>
private void setButtons()
{
//set the visibility of the remove button
if (textBoxList.Count == 0)
btnRemove.Visible = false;
else
btnRemove.Visible = true;
}
/// <summary>
/// Create a new TextBox
/// </summary>
/// <returns>a new TextBox with the right name attribute</returns>
private TextBox createTextBox()
{
TextBox textbox = new TextBox();
textbox.Attributes["name"] = name + "_" + (textBoxList.Count + 1);
textbox.ID = name + "_" + (textBoxList.Count + 1);
return textbox;
}
/// <summary>
/// Show the TextBox instances
/// </summary>
/// <param name="isRemoveLast">true - use after clicking the remove button - will remove the last TextBox element. false - will show the whole TextBox list</param>
private void ShowTextboxes(bool isRemoveLast)
{
if (textBoxList == null || name == null)
return;
//if remove the last element - remove it
if (isRemoveLast)
textBoxList.RemoveAt(textBoxList.Count - 1);
//add each text box to the container
foreach (TextBox textBox in textBoxList)
{
//RequiredFieldValidator validator = new RequiredFieldValidator();
//validator.ErrorMessage = "*required";
//validator.ControlToValidate = textBox.ID;
container.Controls.Add(textBox);
//container.Controls.Add(validator);
container.Controls.Add(new LiteralControl("<br/>"));
}
}
/// <summary>
/// Get or set the basic name of thei MultipleTextBox
/// </summary>
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}
P.S Would love for answer with pure C# and .Net. not JavaScript if possible..
Upvotes: 0
Views: 1442
Reputation: 286
You should be able to get the textbox values from Request.Form. Try changing your ShowTextboxes method to
private void ShowTextboxes(bool isRemoveLast)
{
if (textBoxList == null || name == null)
return;
//if remove the last element - remove it
if (isRemoveLast)
textBoxList.RemoveAt(textBoxList.Count - 1);
//add each text box to the container
foreach (TextBox textBox in textBoxList)
{
// Populate value
textBox.Text = Request.Form[textBox.Name]
//RequiredFieldValidator validator = new RequiredFieldValidator();
//validator.ErrorMessage = "*required";
//validator.ControlToValidate = textBox.ID;
container.Controls.Add(textBox);
//container.Controls.Add(validator);
container.Controls.Add(new LiteralControl("<br/>"));
}
}
Upvotes: 1