Reputation: 161
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>
it shows me error Invalid expression term 'if'
.. now how to write this , Please help
Upvotes: 1
Views: 7832
Reputation: 47766
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" />
Then implement the event:
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;
}
}
It's best practice to keep your logic in your code behind.
Hope that helps.
Upvotes: 2