2013-06-25 69 views
0

請參見下面的代碼:丟失動態創建的文本框的值

protected void btnAddField_click(Object sender, EventArgs e) { 
     int FieldCount = 0; 
     if (ViewState["FieldCount"] != null) 
     { 
      FieldCount = (int)ViewState["FieldCount"]; 
     } 

     Table tbl = new Table(); 
     if (Session["DynamicTable"] != null) 
     { 
      tbl = (Table)Session["DynamicTable"]; 
     } 

     CheckBox chkNewField = new CheckBox(); 
     chkNewField.ID = "chkNewField" + FieldCount.ToString(); 
     chkNewField.Checked = true; 

     Label LblNewLabel = new Label(); 
     LblNewLabel.ID = "lblNewLabel" + FieldCount.ToString(); 
     LblNewLabel.Text = "New Lable"; 

     TextBox TxtNewLabel = new TextBox(); 
     TxtNewLabel.ID = "TxtNewLabel" + FieldCount.ToString(); 

     Label LblNewValue = new Label(); 
     LblNewValue.ID = "lblNewValue" + FieldCount.ToString(); 
     LblNewValue.Text = "New Value"; 

     TextBox TxtNewValue = new TextBox(); 
     TxtNewValue.ID = "TxtNewValue" + FieldCount.ToString(); 

     TableRow tRow = new TableRow(); 

     TableCell tCell1 = new TableCell(); 
     TableCell tCell2 = new TableCell(); 
     tCell2.Attributes.Add("class", "medium"); 
     TableCell tCell3 = new TableCell(); 
     tCell3.Attributes.Add("class", "medium"); 
     TableCell tCell4 = new TableCell(); 
     TableCell tCell5 = new TableCell(); 
     tCell5.Attributes.Add("class", "medium"); 
     TableCell tCell6 = new TableCell(); 
     tCell6.Attributes.Add("class", "medium"); 

     tCell1.Controls.Add(chkNewField); 
     tCell2.Controls.Add(LblNewLabel); 
     tCell3.Controls.Add(TxtNewLabel); 
     tCell4.Controls.Add(new LiteralControl("")); 
     tCell5.Controls.Add(LblNewValue); 
     tCell6.Controls.Add(TxtNewValue); 

     tRow.Cells.Add(tCell1); 
     tRow.Cells.Add(tCell2); 
     tRow.Cells.Add(tCell3); 
     tRow.Cells.Add(tCell4); 
     tRow.Cells.Add(tCell5); 
     tRow.Cells.Add(tCell6); 

     tbl.Rows.Add(tRow); 
     placeHolderTable.Controls.Remove(tbl); 
     placeHolderTable.Controls.Add(tbl); 
     Session["DynamicTable"] = tbl; 
     FieldCount++; 
     ViewState["FieldCount"] = FieldCount; 
} 

protected void BtnPublish_click(object sender, EventArgs e) { 
    TextBox tb = (TextBox)placeHolderTable .FindControl("TxtNewLabel1"); 
} 

動態添加字段都工作正常。但

  1. 我在文本框中清除每個輸入值後回來
  2. 我無法從文本框中獲取的值。

請幫幫我。 在此先感謝, Manu

+0

沒有聲明爲「TxtNewLabel1」 –

回答

1

雖然有一個棘手的方法,您可以通過它獲取PostBack中的文本框的值。 您可以在下面使用此代碼。

private string GetValue(string ControlID) 
{ 
    string[] keys = Request.Form.AllKeys; 
    string value = string.Empty; 
    foreach (string key in keys) 
    { 
     if (key.IndexOf(ControlID) >= 0) 
     { 
      value = Request.Form[key].ToString(); 
      break; 
     } 
    } 

    return value; 
} 

然後在PostBack中使用此方法。

protected void BtnPublish_click(object sender, EventArgs e) 
{ 
    string TxtNewLabel1Val = GetValue("TxtNewLabel1"); 
} 
+0

感謝您的幫助很大Debajit控制。它的讚賞。你的代碼工作得很好。我期待這些技巧。你能幫我解釋我的第一點嗎? - > 1.我在每個帖子後面的文本框中輸入的值 – Manu

1

關於動態創建控件的簡單規則是,如果您想要接收任何值,則必須在Init上重新創建它們。

參見:Page life cycle

回傳數據在功能ProcessPostData處理,如果不創建您的控件在此之前,他們的用戶輸入不會被髮送。

我會將控件創建放入方法中,追蹤動態控件添加到會話(或ViewState)中,然後在每個Init之後添加它們,直到我需要頁面上的控件。