2013-11-25 54 views
0

我構建了一個多文本框生成器的用戶控件。 它給你創建儘可能多的文本框,因爲你需要的能力..在回發之前獲取TextBox文本

我的問題是,每次我使用btnAdd添加一個新的文本框,.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

通過獲取文本「回發之前」是什麼意思?你不能在客戶端使用C#?要麼? –

回答

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/>")); 
    } 
} 
+0

偉大的我沒有意識到在這種情況下Request.Form的存在 –