2013-06-25 2 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"큰 도움 Debajit에 대한 –

답변

1

PostBack에서 textBoxes의 값을 가져올 수있는 까다로운 방법이 있지만. 아래에서이 코드를 사용할 수 있습니다.

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; 
} 

그런 다음 다시 게시이 방법을 사용합니다.

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

감사로 선언 통제가 없다. 감사합니다. 코드가 잘 작동합니다. 나는 이런 종류의 속임수를 기대한다. 첫 번째 요점으로 나를 도울 수 있습니까? -> 1.the 각 게시물에 다시 취소 텍스트 상자에 입력 한 값 – Manu

1

동적으로 생성 된 컨트롤에 대한 간단한 규칙은 값을 받고 싶다면 Init에 다시 작성해야한다는 것입니다.

은 참조 : Page life cycle

다시 게시 데이터는 기능 ProcessPostData에서 처리하여 컨트롤이 그 전에, 그들은 사용자 입력이 할당되지 않습니다 생성되지 않은 경우.

제어 생성을 메서드에 넣고 세션 (또는 ViewState)에 동적 컨트롤이 추가 된 것을 추적하고 페이지의 컨트롤을 원할 때까지 모든 Init에 추가합니다.

관련 문제