ASP C# Datalist OnItemDataBound 事件更改另一个单元格内容
ASP C# Datalist OnItemDataBound event To Change Another Cell Contents
我是 asp/c# 的新手,我正在做一个项目,当运行 sql 存储过程时将根据数据生成报告,我正在填充数据列表 table 现在与 'the top 5 sql records'。
我在访问数据列表数据时遇到问题,我想根据拉取字段的值更改拉取字段的对齐单元格。
我有一个带有 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
}
"In Tolerance" cell/string 内容我想根据 "as_found_entered" 值进行更改,它是一个双精度值。感谢任何帮助。
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 事件中的你有所帮助。
这终于奏效了。标签引用是关键,要加倍的标签有点乱。
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;
}
}
}
我是 asp/c# 的新手,我正在做一个项目,当运行 sql 存储过程时将根据数据生成报告,我正在填充数据列表 table 现在与 'the top 5 sql records'。
我在访问数据列表数据时遇到问题,我想根据拉取字段的值更改拉取字段的对齐单元格。
我有一个带有 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
}
"In Tolerance" cell/string 内容我想根据 "as_found_entered" 值进行更改,它是一个双精度值。感谢任何帮助。
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 事件中的你有所帮助。
这终于奏效了。标签引用是关键,要加倍的标签有点乱。
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;
}
}
}