【问题标题】:ASP C# Datalist OnItemDataBound event To Change Another Cell ContentsASP C# Datalist OnItemDataBound 事件更改另一个单元格内容
【发布时间】:2017-04-06 15:34:52
【问题描述】:

我对 asp/c# 还很陌生,我正在做一个项目,当运行 sql 存储过程时,它将根据数据生成报告,我现在正在用“the”填充一个 datalist 表前 5 条 sql 记录。

我在访问 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”值更改“容差”单元格/字符串内容。感谢您提供任何帮助。

【问题讨论】:

    标签: javascript c# asp.net datalist


    【解决方案1】:
    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 事件有所帮助。

    【讨论】:

    • 谢谢你。有了这个片段,我终于让它工作了!
    【解决方案2】:

    这终于奏效了。标签引用是关键,而要加倍的标签有点混乱。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-08
      相关资源
      最近更新 更多