admin管理员组文章数量:1334174
I want to use C# function to set text in dataRepeater and use if statement in aspx page.
<asp:Repeater ID="dataRepeater" runat="server">
<ItemTemplate>
<tr>
<%if (condition){ %>
<td>
<%#FunctionName(DataBinder.Eval(Container.DataItem, ""))%>
</td>
<tr>
</ItemTemplate>
</asp:Repeater>
//.cs
protected string FunctionName(){
//
//
//
return string
}
However, any condition always call the C# Function, is that a normal situation or I doing something wrong here, thks.
I want to use C# function to set text in dataRepeater and use if statement in aspx page.
<asp:Repeater ID="dataRepeater" runat="server">
<ItemTemplate>
<tr>
<%if (condition){ %>
<td>
<%#FunctionName(DataBinder.Eval(Container.DataItem, ""))%>
</td>
<tr>
</ItemTemplate>
</asp:Repeater>
//.cs
protected string FunctionName(){
//
//
//
return string
}
However, any condition always call the C# Function, is that a normal situation or I doing something wrong here, thks.
Share Improve this question edited Nov 20, 2024 at 13:26 DarkBee 15.6k8 gold badges72 silver badges117 bronze badges asked Nov 20, 2024 at 10:29 Ansel LiouAnsel Liou 252 bronze badges 2 |1 Answer
Reset to default 0**ASPX :**
<asp:Repeater ID="dataRepeater" runat="server" OnItemDataBound="dataRepeater_ItemDataBound">
<ItemTemplate>
<tr>
<td id="myCell" runat="server">
<!-- Initially, you can set an empty text here or a placeholder -->
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
**(.cs file):**
protected void Page_Load(object sender, EventArgs e)
{
// Bind data to the Repeater here
dataRepeater.DataSource = yourDataSource; // Replace with your data source
dataRepeater.DataBind();
}
protected void dataRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// Check if the item is a data item (not the header or footer)
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Get the data item (e.g., some object, or DataBinder.Eval)
var dataItem = e.Item.DataItem;
// Get a reference to the <td> element
var td = e.Item.FindControl("myCell") as HtmlTableCell;
// Condition based on the dataItem or other logic
if (YourCondition(dataItem))
{
// Call your C# function here
td.InnerText = FunctionName(dataItem); // Modify the text content conditionally
}
else
{
td.InnerText = "Default Value"; // Or leave it empty, depending on your need
}
}
}
// Your C# function that returns the string you want to set
protected string FunctionName(object dataItem)
{
// Perform your logic here
return "Some Result"; // Return the desired value
}
// Example condition method
private bool YourCondition(object dataItem)
{
// Replace with your actual condition logic
return true; // Just an example; you should check dataItem here
}
Avoid mixing conditionals with <%# %> data binding expressions for complex logic. Instead, handle such logic in the C# code for better clarity and maintainability.
本文标签: cFunction set td text in ltif statementgt always be calledStack Overflow
版权声明:本文标题:c# - Function set td text in <%if statement%> always be called - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742365138a2461139.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
}
is on the aspx page. But your aspx code should work fine and means yourif (condition)
always returns true. So look into that. – VDWWD Commented Nov 20, 2024 at 13:18