2010-03-21 4 views
0

메신저, 난 분명이 코드가 얼마나에 실험, 아무 의미도하지 않습니다이ASP.Net 라디오 버튼 목록

for (int i = 1; i < 3; i++) 
    { 
     TableRow tr = new TableRow(); 
     Table1.Rows.Add(tr); 
     TableCell QuestionCell = new TableCell(); 

     // get the text for the question and stick it in the cell 
     QuestionCell.Text = "<b>"+i+".</b> Question " + i; 
     tr.Cells.Add(QuestionCell); 

     TableRow tr2 = new TableRow(); 
     Table1.Rows.Add(tr2); 

     // create a cell for the choice 

     TableCell ChoicesCell = new TableCell(); 
     ChoicesCell.Width = 1000; 

     // align the choices on the left 
     ChoicesCell.HorizontalAlign = HorizontalAlign.Left; 
     tr2.Cells.Add(ChoicesCell); 

     RadioButtonList rbl = new RadioButtonList(); 
     rbl.ID = "Radio1_" + i; 
     ChoicesCell.Controls.Add(rbl); 

     rbl.Items.Add("1"); 
     rbl.Items.Add("2"); 
     rbl.Items.Add("3"); 
     rbl.Items.Add("4"); 
    } 

와 비슷한 몇 가지 모의 코드를 나는이 피드백 양식을 할 수있다, 문제는 지금 한 번 누군가가 제출 버튼을 누른다. (양식에 제출 버튼이있다.) 나는 어떻게 테이블을 지나가고 사용자가 선택한 라디오 버튼에서 텍스트를 얻는다? from page_load!에서 생성 된 피드백!

도움을 주셔서 감사합니다. 메신저 잘못가는 경우 버튼이 작동하지 않는 것

protected void Button1_Click(object sender, EventArgs e) 
{ 
    foreach (TableCell cell in Table1.Rows) 
    { 
     foreach (Control ctrl in cell.Controls) 
     { 
      if (ctrl is RadioButtonList) 
      { 
       if (ctrl.selected) // this doesnt works 
       { 
        string selected = ctrl.text // this doesnt work either 
       } 
      } 
     } 
    } 
} 

이 을 누르면 한 번

편집 그래서 나는이 코드를 가지고 ... 난 몰라!

+0

대신 SelectedValue 속성을 사용해보십시오. 무슨 소리 야? 특정 오류가 발생 했습니까? – jessegavin

답변

3

나는 루프 내에서 Control을 캐스팅하여 RadioButtonList이되어야한다고 생각합니다. 또한

, 당신은 '이 나던 작품'말할 때 selected

protected void Button1_Click(object sender, EventArgs e) 
{ 
    foreach (TableCell cell in Table1.Rows) 
    { 
     foreach (Control ctrl in cell.Controls) 
     { 
      if (ctrl is RadioButtonList) 
      { 
       string selected = ((RadioButtonList)ctrl).SelectedValue; 
      } 
     } 
    } 
} 
+1

+1 이것은 포스터에서 반드시 작동합니다. .NET 언어는 대/소문자를 구분하며 RadioButtonList 또는 CheckBox와 같은 컨트롤의 속성에는 기본 형식을 통해 액세스 할 수 없다는 점이 중요합니다. 그래서,'ctrl.selected' 나'ctrl.Selected'와 같은 것은 없지만'((Checkbox) ctrl) .Selected' 또는 게시 한 RadioButtonList로서 존재합니다. –

0

해당 컨트롤에 EnableViewState = false를 설정 했습니까? ViewState가 false로 설정된 경우 서버는 다시 게시 한 후 컨트롤의 사용자 상태에 의해 변경된 내용을 볼 수 없습니다.

0

컨트롤을 만드는 방식으로 (거의 즉석에서) 컨트롤을 다시 작성해야하므로 Page_Init의 모든 페이지 요청을 처리하고 상태를 직접 유지 관리해야합니다.

관련 문제