英文:
Using IF ELSEIF and ELSE Condition with EVAL function in ASP.Net using C#
问题
Here is the translated code:
我正在使用ASP.NET C#,我需要做类似这样的事情:
<asp:TemplateField
HeaderText="文本"
ItemStyle-HorizontalAlign="Center"
ItemStyle-CssClass="ddl_Class_new">
<ItemTemplate>
<% if (Eval("dOR_4").ToString().Equals("R"))
{ %>
<asp:ImageButton ID="btn"
ImageUrl="/Img/bullett/redbul.gif"
runat="server" />
<% }
else if (Eval("dOR_4").ToString().Equals("G"))
{ %>
<asp:ImageButton ID="btn"
ImageUrl="/Img/bullett/goldbul.gif"
runat="server" />
<% }
else if (Eval("dOR_4").ToString().Equals("V"))
{ %>
<asp:ImageButton ID="btn"
ImageUrl="/Img/bullett/forestbul.gif"
runat="server" />
<% } %>
</ItemTemplate>
</asp:TemplateField>
但是我得到错误消息:"数据绑定方法,如Eval(),XPath()和Bind(),只能在数据绑定控件的上下文中使用。"
我尝试了下面的代码,但我看不到 "goldbul.gif":
<asp:TemplateField
HeaderText="文本"
ItemStyle-HorizontalAlign="Center"
ItemStyle-CssClass="ddl_Class_new">
<ItemTemplate>
<asp:ImageButton ID="btn"
ImageUrl='<%# Eval("dOR_4").ToString().Equals("R") ? "/Img/bullett/redbul.gif" : Eval("dOR_4").ToString().Equals("G") ? "/Img/bullett/goldbul.gif" : "/Img/bullett/forestbul.gif" %>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>
非常感谢您的帮助,通过使用EVAL函数来进行IF ELSEIF ELSE条件判断。
Please note that I've provided the translated code, but I haven't addressed the specific issue with the code. If you have any questions or need further assistance with the error message, feel free to ask.
英文:
I'am using ASP.NET C#, and I have to do something like this:
<asp:TemplateField
HeaderText="text"
ItemStyle-HorizontalAlign="Center"
ItemStyle-CssClass="ddl_Class_new">
<ItemTemplate>
<% if (Eval("dOR_4").ToString().Equals("R"))
{ %>
<asp:ImageButton ID="btn"
ImageUrl="/Img/bullett/redbul.gif"
runat="server" />
<% }
else if (Eval("dOR_4").ToString().Equals("G"))
{ %>
<asp:ImageButton ID="btn"
ImageUrl="/Img/bullett/goldbul.gif"
runat="server" />
<% }
else if (Eval("dOR_4").ToString().Equals("V"))
{ %>
<asp:ImageButton ID="btn"
ImageUrl="/Img/bullett/forestbul.gif"
runat="server" />
<% } %>
</ItemTemplate>
</asp:TemplateField>
But I'am getting error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."
I have tried using my code below, but I don't see the goldbul.gif
<asp:TemplateField
HeaderText="text"
ItemStyle-HorizontalAlign="Center"
ItemStyle-CssClass="ddl_Class_new">
<ItemTemplate>
<asp:ImageButton ID="btn"
ImageUrl='<%# Eval("dOR_4").ToString().Equals("R") ?
"/Img/bullett/redbul.gif" :
Eval("dOR_4).ToString().Equals("G") ?
"/Img/bullett/goldbul.gif" : "/Img/bullett/forestbul.gif" %>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>
Any help really appreciated
by making use of IF ELSEIF ELSE condition with EVAL function
答案1
得分: 2
实际上,不要尝试编写或在页面标记中包含该代码。这很混乱,更糟糕的是会让你的生活变得糟糕。
由于网格视图是一个服务器端控件,所以你有“许多”选项来编写一些干净的代码来处理你的示例用例。
无论是网格视图、列表视图、重复器还是其他许多数据绑定控件?
它们都有一个行数据绑定事件。因此,该事件不仅简单,而且是你处理格式、隐藏/显示控件或甚至执行高亮显示、颜色等操作的“首选”代码片段。
甚至更好的是?
该事件在绑定事件期间具有完整的数据库行。换句话说,对于甚至不在网格视图中“显示”或包含的列,你仍然可以使用这些列! 所以,你可能有一个税率计算,但不需要或不想显示税率。
或者甚至是酒店的简单列表,你有一个布尔值“active”列,对于任何活动酒店,你可能会将该数据行颜色设置为“浅蓝色”或类似颜色。(但是,真正的奖励点在于“Active”列不必出现在标记中!!!)。
所以,对于这样的目标,使用行数据绑定事件。你会发现它不仅具有数百万用例,而且意味着你不需要将混乱的标记与一些代码混合在一起。
所以,对于你的情况?
我建议使用行数据绑定事件。
所以,说一下这个标记:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table table-hover" width="45%"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" ItemStyle-Width="120px" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="Status" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" Width="64"
OnClick="ImageButton1_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
所以,我们有一个图像按钮。
我有一个PassedTest列(值2 = 良好,1 = 不好,0 = 无状态)。
所以,在行数据绑定事件中,我有这段代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType== DataControlRowType.DataRow)
{
ImageButton Ibtn = (ImageButton)e.Row.FindControl("ImageButton1");
DataRowView rData = (DataRowView)e.Row.DataItem;
// passed test (room been cleaned) 2 = yes, 1, rejected, 0 - no status
switch ((int)rData["PassedTest"])
{
case 2:
Ibtn.ImageUrl = ResolveUrl(@"~/Content/Approved80.png");
break;
case 1:
Ibtn.ImageUrl = ResolveUrl(@"~/Content/Reject80.png");
break;
default:
Ibtn.ImageUrl = ResolveUrl(@"~/Content/Empty80.png");
break;
}
}
}
所以,要加载的代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadGrid();
}
void LoadGrid()
{
string strSQL =
"SELECT * FROM tblHotelsA ORDER BY HotelName";
GridView1.DataSource = General.MyRst(strSQL);
GridView1.DataBind();
}
结果现在是这样的:
所以,你不会有混乱的标记。
你不需要混合代码和标记。
你有一个干净漂亮的行事件可供使用
该行事件具有完整的数据库/数据源行。
你可以将所有代码都放在页面的代码后台。
英文:
Actually, don't try and write or include that code in the page markup. It is a mess, and worse will make your life miserable anyway.
Since the grid view is a server side control, then you have "many" options to write a bit of clean code to deal with your example use case.
Be it a gridview, listview, repeater, or many of the other data bound controls?
They all have a row data bound event. So, that event is not only simple, but is your "go to" bit of code for things like formatting, hide/show controls, or even doing things like highlights, color.
And EVEN better?
That event has the FULL database row during that binding event. In other words, for columns that you don't even "display" or include in the gridview? You can STILL use those columns! So, you might have a tax calulation, but don't need or want to show the tax rate.
Or maybe even a simple list of hotels, and you have a boolean "active" column, and for any active hotel, you might color that row of data "light blue" or some such. (but, again the real bonus points is that Active column does NOT have to appear in the markup!!!).
So, for such goals here, use the row data bound event. You find it not only has a million use cases, but also means you don't require have messy markup intermixed with some code.
So, for your case?
Then I suggest the row data bound event.
so, say this markup:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table table-hover" width="45%"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" ItemStyle-Width="120px" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="Status" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" Width="64"
OnClick="ImageButton1_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
So, we have a image button.
I have a PassedTest column (value 2 = good, 1 = bad, 0 = no status).
So, I have this code in the row data bound event:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType== DataControlRowType.DataRow)
{
ImageButton Ibtn = (ImageButton)e.Row.FindControl("ImageButton1");
DataRowView rData = (DataRowView)e.Row.DataItem;
// passed test (room been cleaned) 2 = yes, 1, rejected, 0 - no status
switch ((int)rData["PassedTest"])
{
case 2:
Ibtn.ImageUrl = ResolveUrl(@"~/Content/Approved80.png");
break;
case 1:
Ibtn.ImageUrl = ResolveUrl(@"~/Content/Reject80.png");
break;
default:
Ibtn.ImageUrl = ResolveUrl(@"~/Content/Empty80.png");
break;
}
}
}
So, code to load is this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadGrid();
}
void LoadGrid()
{
string strSQL =
"SELECT * FROM tblHotelsA ORDER BY HotelName";
GridView1.DataSource = General.MyRst(strSQL);
GridView1.DataBind();
}
And the result is now this:
So, you don't have messy markup.
You don't have to intermix code and markup.
You have a clean nice row event to use
The row event has full database/data source row.
and you keep all the code in code behind for that page.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论