2017-04-06 82 views
0

我是相当新的ASP/C#和我工作的一个项目,运行时SQL存储过程会产生基于数据的报告,我现在使用'前5个sql记录'填充数据列表。ASP C#Datalist中OnItemDataBound事件要更改其他单元格内容

我在访问数据列表数据的问题,我想根据拉字段的值更改拉到现场的对准单元。

我有一个DataList设置与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”值。任何援助表示赞赏。

回答

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

感谢你为这个。有了这个snipet,我终于开始工作了! – 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; 
       } 
      } 
     } 
相关问题