2017-04-06 17 views
0

저는 asp/C#에 상당히 익숙합니다. 그리고 SQL 저장 프로 시저를 실행하면 데이터를 기반으로 보고서가 생성 될 것입니다. 데이터 목록 테이블에 '상위 5 개의 sql 레코드'를 채 웁니다.ASP C# Datalist OnItemDataBound 이벤트 다른 셀 내용을 변경하려면

데이터 목록 데이터에 액세스하는 데 문제가 있습니다. 끌어온 필드의 값에 따라 끌어온 필드의 정렬 셀을 변경하고 싶습니다.

데이터 목록 설정에 OnItemBound 프로 시저가 있습니다.

페이지 코드 : 뒤에

<asp:DataList ID="DataList2" runat="server" onitemdatabound="DataList2_ItemDataBound"> 
      <HeaderTemplate> 
      <table> 
       <tr> 
        <th>Certification Date</th> 
        <th colspan="2">As Found Potential</th> 
        <th>As Found Tolerance</th> 
        <th colspan="2">As Left Potential</th> 
        <th>As Left Tolerance</th>  
       </tr> 
      </HeaderTemplate> 
      <ItemTemplate> 
        <tr>                 
         <td class="td04">     
          <asp:Label ID="certificationDate" runat="server" Text='<%# Eval("as_left_date", "{0:MM/dd/yyy}") %>' /> <!-- Certification Date Always As Left --> 
         </td> 
         <td class="td05">     
          <asp:Label ID="asFoundPotential" runat="server" Text='<%# Convert.ToDouble(Eval("as_found_entered")) %>' /> <!-- As Found Entered --> 
         </td> 
         <td class="td06">     
          <div>mV</div> 
         </td> 
         <td class="td04">     
          <asp:Label ID="asFoundTolerance" runat="server" Text='In Tolerance' /> <!-- ItemDataBound to Change Contents --> 
         </td> 
         <td class="td05">     
          <asp:Label ID="asLeftPotential" runat="server" Text='<%# Eval("as_left_entered") %>' /> <!-- As Left Entered --> 
         </td> 
         <td class="td06">     
          <div>mV</div> 
         </td> 
         <td class="td04">     
          <asp:Label ID="asLeftTolerance" runat="server" Text='In Tolerance' /> <!-- ItemDataBound to Change Contents --> 
         </td>    
        </tr>      
      </ItemTemplate> 
      <FooterTemplate> 
       </table> 
      </FooterTemplate> 
     </asp:DataList> 

코드 :

 protected void dataTable2() 
     { 
      string constr = ConfigurationManager.ConnectionStrings["Records"].ConnectionString; 
      SqlConnection conn = new SqlConnection(constr); 
      SqlCommand myCommand = new SqlCommand("report_page", conn); 
      myCommand.CommandType = CommandType.StoredProcedure; 
       myCommand.Parameters.AddWithValue("@serial_number", serial_number.Text); 

      DataSet ds = new DataSet(); 
      SqlDataAdapter adp = new SqlDataAdapter(myCommand); 

      try 
      {     
       conn.Open(); 

       adp.Fill(ds); 
       DataList2.DataSource = ds.Tables[0]; 
       DataList2.DataBind();    

      } 

      catch (SqlException ex) 
      { 
       writeToFile(Environment.NewLine + "SQL DataTable2 Error: " + ex.Message); 
      } 

      finally 
      { 
       conn.Close(); 
       myCommand.Dispose(); 
      } 
     } 

OnDataBound 이벤트 :

 protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e) 
     { 
       //cell change code goes here 
     } 

셀/문자열 내용 "공차에서"나는에 따라 변경하고 싶습니다 "as_found_entered"값은 double입니다. 모든 도움을 주시면 감사하겠습니다.

답변

0

(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem는)

 { 

     // Retrieve the Label control in the current DataListItem. 
     Label asFoundPotential = (Label)e.Item.FindControl("asFoundPotential"); 


    // You can convert the asFoundPotential lable value in double. 

if (asFoundPotential = Something) 
{ 
    // identify the specific cell of asLeftPotential and assign whatever you want to change  

}      

     } 

희망이 의사 코드는 당신이 둘 필요가 도움이 될 것입니다 경우 Itemdatabound 이벤트입니다.

+0

고맙습니다. 이 스 니펫으로 마침내 작동하게되었습니다! – SDG

0

이것이 결국 효과가 있습니다. 레이블 참조가 중요하며 이중 레이블은 다소 엉망입니다.

 protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e) 
     { 
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
      {        
       Label asFoundTolerance = (Label)e.Item.FindControl("asFoundTolerance"); 
       Label asLeftTolerance = (Label)e.Item.FindControl("asLeftTolerance"); 

       Label asFoundPotential = (Label)e.Item.FindControl("asFoundPotential"); 
       double foundPot = Convert.ToDouble(asFoundPotential.Text);     
       if (foundPot < 305.5 || foundPot > 326.4) 
       {      
        asFoundTolerance.Text = "Out of Tolerance"; 
        be4TestG.Checked = false; 
        be4TestB.Checked = true; 
       } 
       else 
       { 
        asFoundTolerance.Text = "In Tolerance"; 
        be4TestG.Checked = true; 
        be4TestB.Checked = false; 
       } 

       Label asLeftPotential = (Label)e.Item.FindControl("asLeftPotential"); 
       double leftPot = Convert.ToDouble(asLeftPotential.Text); 
       if (leftPot < 305.5 || leftPot > 326.4) 
       { 
        asLeftTolerance.Text = "Out of Tolerance"; 
        aftTestG.Checked = false; 
        aftTestB.Checked = true; 
       } 
       else 
       { 
        asLeftTolerance.Text = "In Tolerance"; 
        aftTestG.Checked = true; 
        aftTestB.Checked = false; 
       } 
      } 
     }