如何在asp.net中的if else语句中使用的DataBinder.Eval [英] How to use IF ELSE statement in DataBinder.Eval in asp.net
问题描述
我不得不使用多个若网格视图如果code ==1,测试1,如果它的2,然后Test2的,如果它的3,然后Test3的,如果else语句它的4中的TEST4..
I have to use multiple if else statement in Grid view if code == "1" then "Test1" and if its "2" then "Test2" and if its "3" then "Test3" and if its "4" the "Test4"..
<asp:Label ID="lblCode" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Code")%>'></asp:Label>
于是,我就包含这样的
so i tried to include like this
<asp:Label ID="lblCode" runat="server" Text='<%# if((DataBinder.Eval(Container, "DataItem.Code")).ToString())==" 1" then "Test1" %>'></asp:Label>
它为我的错误无效前pression术语'如果'
..现在该怎么写,请帮帮忙
it shows me error Invalid expression term 'if'
.. now how to write this , Please help
推荐答案
这是你应该使用的控件 OnDataBinding
事件,而不是和移动任何逻辑您的标记之外到code的后面。
This is where you should be using the controls OnDataBinding
event instead and moving any logic outside of your markup into your code behind.
<asp:Label ID="lblCode" runat="server" Text='' OnDataBinding="lblCode_DataBinding" />
然后实现事件:
protected void lblCode_DataBinding(object sender, System.EventArgs e)
{
Label lbl = (Label)sender;
string code = Eval("Code");
switch (code)
{
case "1":
lbl.Text = "Test1";
break;
case "2":
lbl.Text = "Test2";
break;
case "3":
lbl.Text = "Test3";
break;
case "4":
lbl.Text = "Test4";
break;
default:
lbl.Text = "Unknown";
break;
}
}
这是保持你的逻辑在code后面的最佳实践。
It's best practice to keep your logic in your code behind.
希望有所帮助。
这篇关于如何在asp.net中的if else语句中使用的DataBinder.Eval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!