2014-05-09 2 views
0

나는 repeater를 통해 동적으로 생성되는 레이블을 가지고 있으며, rollNo는 itemTemplate의 일부인 레이블입니다. l의 값을 확인하면 if 블록으로 이동하지만 l.Text는 여전히 비어 있습니다. check.Text는 "d"만 반환합니다. 왜? 그것에서 더 rollNo이 없습니다로레이블 ASP가 설정되지 않음

 Label l = (Label)item.FindControl("rollNo"); 
     TextBox t = (TextBox)item.FindControl("quiz1"); 
     if (l != null) 
     { 
      string a = l.Text; 
      check.Text = "d"+a; 
     } 
+0

Oh 및이 rollNo 레이블은 data.Eval 매개 변수입니다. 페이지를로드 할 때 리피터에 데이터를로드합니다. –

+0

리피터 마크 업을 게시 할 수 있습니까? –

답변

1

귀하의 코드 샘플이 완료되지 않았습니다,하지만 난 당신은 리피터를 사용하는 ... 당신에게 무언가를 말할 수있는 그 사용하여 템플릿으로 ... ID가 사용 템플릿 안에는 결코 컨트롤의 런타임 ID가 없습니다! 그것에 대해 생각 해봐! 템플릿의 요소 중 하나에 rollNo을 할당하고 리피터로 전달할 행이 10 개 있다고 가정 해 보겠습니다. 동일한 ID가 인 10 개의 컨트롤이 있어야합니까? 아니요! 내가하지 희망! 당신은 다시 생각 당신이 무엇을 원하는가 또는 (루프) 컨트롤을 찾기 위해 다른 접근법을 사용해야 할 템플릿 내부 ID를 사용하는 동안의 FindControl는 아무 것도 반환하지 않습니다 그 이유를 ... 들어 ...

+0

예, Repeater에서 OnItemCommand를 사용하여 해결했습니다. 아래 코드는 답변으로 게시되었습니다. 감사합니다. –

+0

당신은 환영합니다 ... –

0

리피터 마크 업 : 뒤에

<asp:Repeater id="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand"> 
    <ItemTemplate> 
     <tr onclick="rowReturn(this)"> 
      <td><asp:Label CssClass="form-control" runat="server" ID="rollNo"><%# DataBinder.Eval(Container.DataItem, "sid") %></asp:Label></td> 
      <td><asp:TextBox CssClass="form-control" runat="server" ID="quiz1" required></asp:TextBox></td> 
      <td><asp:TextBox CssClass="form-control" runat="server" ID="quiz2" required></asp:TextBox></td> 
      <td><asp:Button CssClass="btn btn-success btn-sm form-control" ID="add" CommandName="add" runat="server" Text="Add" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "sid") %>' /></td> 
     </tr> 
    </ItemTemplate> 

코드 :

TextBox t1; 
TextBox t2; 
string rollNumber, T1, T2; 
if (e.CommandName == "add") 
{ 
    // get CommandArgument you have selected on the button 
    string roll = e.CommandArgument.ToString(); 
    rollNumber = roll; 
    foreach (RepeaterItem item in Repeater1.Items) 
    { 
     t1 = (TextBox)item.FindControl("quiz1"); 
     t2 = (TextBox)item.FindControl("quiz2"); 
     T1 = t1.Text; 
     T2 = t2.Text; 
     //...DB code or any other code 
    } 
} 
+0

ASP가 있습니다 : Repeater closing tag가 답을 쓰는 동안 텍스트에 삽입되지 않았습니다. 희망을 읽는 사람이 이것을 얻는다. –

+0

아,이 코드는 리피터의 OnItemCommand 함수 안에있다. –