上面的问题是如果 someFunction(School.Name,"Name") 返回 n-6ren">
gpt4 book ai didi

c# - 使用项目模板在值不为空时追加 tr

转载 作者:太空宇宙 更新时间:2023-11-03 21:21:05 24 4
gpt4 key购买 nike

我正在使用项目模板附加行

<tr>
<td>
<asp:HyperLink ID="HyperLink1" NavigateUrl="bla" CssClass="display:inherit !important;" Text='<%# someFunction(School.Name,"Name") %>' runat="server" />
</td>
</tr>

上面的问题是如果 someFunction(School.Name,"Name") 返回 null 它附加空 tr,所以我只想在 someFunction(School.Name,"Name")!=null

时附加 tr

所以我通过添加 if 条件尝试了下面的代码,但它没有编译!!它说 school.name 未定义

<% if(someFunction(School.Name,"Name")!=null){
<tr>
<td>
<asp:HyperLink ID="HyperLink1" NavigateUrl="bla" CssClass="display:inherit !important;" Text='<%# someFunction(School.Name,"Name") %>' runat="server" />
</td>
</tr>
} %>

最佳答案

您可以从后面的代码中处理它。

<td id="tdAppend" runat="server"> Content </td>


if(School.Name == null)
tdAppend.Visible = false;

更新的答案:

currentItemIndex = -1;
listView.ItemDataBound += listView_ItemDataBound;
CheckQueryString();

//Hide or show the table header based on id
HtmlControl thControl = listView.FindControl("thAppend") as HtmlControl;
if (thControl != null)
{
if (obj.ID == 12)
{
thControl.Visible = true;

}
else
{
thControl.Visible = false;
for (int i = 0; i < listView.Items.Count; i++)
{
HtmlControl tdControl = listView.Items[i].FindControl("tdSchoolName") as HtmlControl;
if (tdControl != null)
{
tdControl.Visible = false;
}
}

}
}

为方便起见:

void listView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
currentItemIndex++;

ListViewDataItem dataitem = (ListViewDataItem)e.Item;

HtmlTableRow trControl = (HtmlTableRow)e.Item.FindControl("MainTableRow");
if (IsOdd(currentItemIndex))
trControl.BgColor = "DarkGray";

}
}

private bool IsOdd(int value)
{
return value % 2 != 0;
}

关于c# - 使用项目模板在值不为空时追加 tr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30669305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com