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

问题描述 投票:0回答:2

我对 asp/c# 相当陌生,我正在开发一个项目,当运行 sql 存储过程时将根据数据生成一个报告,并且我现在正在使用“前 5 个”填充数据列表sql记录'。

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

我有一个带有 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”值(双精度值)更改“In Tolerance”单元格/字符串内容。如有任何帮助,我们将不胜感激。

javascript c# asp.net datalist
2个回答
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
事件中放入的内容。


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;
                }
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.