2009-12-10 7 views
0

데이터 목록에 바인딩되어 있으므로 컨트롤에 사용자 지정 특성을 설정해야합니다. 이벤트 인수에는 컨트롤 모음이 있지만 참조 이름이 표시되지 않습니다. 어떻게 할 수 있습니까? 데이터리스트의 OnItemBound에서 컨트롤에 대한 참조를 얻으려면 어떻게해야합니까?

나는이 때 :

(e.Item.FindControl("autoChartChkBox") as CheckBox).Attributes.Add("CompanyToken", "CompanyToken"); 

제어는 '널'항상. 찾을하려는 컨트롤이 내 데이터 템플릿에 추가되었습니다. 이것은 나의 ItemTemplate 임무이며 아래는 실제 사원입니다. 알림 protected CheckBox autoChartChkBox; 이것은 OnDataItemBound 이벤트를 통해 조작하려고하는 컨트롤입니다.

alertList.ItemTemplate = new AlertItemTemplate(groupTracker); 


    private class AlertItemTemplate : ItemTemplateBase 
    { 
     private readonly GroupHeaderTracker groupTracker; 
     protected CheckBox autoChartChkBox; 


     public override void DataBind() 
     { 

      Label autoChartLbl; 


      Alert item = (Alert)((DataListItem)this.NamingContainer).DataItem; 

      CultureInfo info = Thread.CurrentThread.CurrentCulture; 
      titleText.Text = String.Format("{0} - {1}", item.DateCreated.ToString(info.DateTimeFormat.ShortDatePattern), item.ID); 
      this.bodyText.Text = item.Text; 

      Color color = GetAlertColor(item.AlertType.Color); 
      colorDisplay.BackColor = color; 

      this.groupTracker.SetCurrentAlertTypeId(item.AlertType.ID); 

      if(this.groupTracker.IsNewGroup()) 
      { 
       this.alertTypeNameLabel.Text = item.AlertType.Name; 
       this.alertTypeNameRow.Visible = true; 
       this.alertTypeNameRow.Cells[0].Style.Add("border-top", string.Format("solid thin {0}",GetColorHexValue(color))); 
       this.alertTypeNameRow.Cells[0].Style.Add("border-bottom", string.Format("solid thin {0}",GetColorHexValue(color))); 

       //Auto Chart 
       TableCell autoChartCell; 
       autoChartCell = new TableCell(); 
       autoChartCell.Width = 50; 
       autoChartCell.BorderStyle = BorderStyle.Solid; 
       autoChartCell.VerticalAlign = VerticalAlign.Top; 
       autoChartCell.Controls.Add(autoChartChkBox = new CheckBox()); 
       autoChartCell.Controls.Add(autoChartLbl = new Label()); 
       Rows[1].Cells.Add(autoChartCell); 
       autoChartLbl.Text = "AutoChart"; 
       autoChartChkBox.Checked = item.IncludeInChartNotes; 

       alertTypeNameCell.ColumnSpan = Rows[1].Cells.Count; 

      } 

답변

2
(e.Item.FindControl("yourControlName") as YourControlType).Attributes.Add("onClick","DoSomethingHere()"); 
+0

이것은 나를 위해 작동하지 않습니다. 컨트롤은 항상 null입니다. 컨트롤이 내 데이터 템플릿에 추가되고 있습니다. 나는 내 질문을 업데이트했다. – Nick

+0

aspx/ascx도 게시 할 수 있습니까? 그리고 데이터리스트를 채우려는 합리적으로 재현 가능한 (그 클래스의) 컬렉션? – ram

0

나는 건물입니다 내 뒤에서 코드 (나의 선택)에서 ItemTemplate. 어떤 경우에는 찾으려는 컨트롤을 명시 적으로 명명해야합니다.

autoChartChkBox.ID = "autoChartChkBox"; 

그런 다음 OnItemDataBound 경우에 당신은 FindControl()의 인수로이 ID를 사용합니다. 내 경우

이 다른 사람 도움

protected void Data_ItemBound(object sender, DataListItemEventArgs e) 
{ 
    if (e.Item.DataItem != null) 
    { 
     if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
     { 
      CheckBox control = (CheckBox)e.Item.FindControl("autoChartChkBox"); 
     } 
    } 
} 

희망.

관련 문제