我正在构建一个多文本框生成器的用户控件。 它使您能够根据需要创建尽可能多的文本框。
我的问题是,每次我使用btnAdd添加新的TextBox时,.Net都会将帖子发送回服务器 - 这会删除当前文本框中的所有文本。
有没有一种方法可以在回调之前捕获文本框中的文本,以便用内部文本重新呈现用户控件?
HTML部分:
<%@ 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>
代码:
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 很想用纯C#和.Net来回答。如果可能的话,不是JavaScript ..
答案 0 :(得分:1)
您应该能够从Request.Form获取文本框值。尝试将ShowTextboxes方法更改为
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/>"));
}
}