javascriptc#asp.netdatalist

ASP C# Datalist OnItemDataBound event To Change Another Cell Contents


I'm fairly new to asp/c# and I'm working on a project that when runs a sql stored procedure will produce a report based on the data, and I'm populating a datalist table right now with with 'the top 5 sql records'.

I'm having an issue of accessing the datalist data and I'd like to change the aligning cell of a pulled field according to the pulled field's value.

I have a datalist setup with an OnItemBound procedure.

Page code:

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

Code Behind:

        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 event:

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

The "In Tolerance" cell/string contents I would like to change based on the "as_found_entered" value which is a double. Any assistance is appreciated.


Solution

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

    Hope this pseudo code will help you with what you need to put in the ItemDataBound event.