ASP C#Datalist OnItemDataBound事件更改另一个单元格内容

时间:2017-04-06 15:34:53

标签: javascript c# asp.net datalist

我是asp / c#的新手,我正在研究一个项目,当运行一个sql存储过程时会生成一个基于数据的报告,而我现在正在填充一个数据表表格。前5个sql记录'。

我遇到了访问数据列表数据的问题,我想根据拉取的字段值更改拉出字段的对齐单元格。

我有一个带有OnItemBound程序的datalist设置。

网页代码:

<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
        }

“In Tolerance”单元格/字符串内容我想根据“as_found_entered”值进行更改,这是一个double。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

if(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事件中添加。

答案 1 :(得分: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;
                }
            }
        }