为GridView的ItemTemplate中条件逻辑(使用仅标记) [英] Conditional Logic for Gridview ItemTemplate (using mark up only)
问题描述
我有一个GridView,如下图所示。当的empType是收缩的EmpID必须伪装成XXX;正式员工,实际的EmpID应该显示。此外,当它被屏蔽,我需要在列的EmpID添加按钮控制
我需要使用的加价做;不使用落后code。我们怎么能写的GridView的ItemTemplate有条件的逻辑,这个逻辑?
请注意:.NET 4.0
< ASP:GridView控件ID =Gridview1=服务器的AutoGenerateColumns =FALSE>
<柱体和GT;
< ASP:BoundField的的HeaderText =AssociateID数据字段=AssociateID/>
< ASP:的TemplateField的HeaderText =的EmpID>
<&ItemTemplate中GT;
<%#的eval(的EmpID)%>
< / ItemTemplate中>
< / ASP:的TemplateField>
< ASP:BoundField的的HeaderText =的empType数据字段=的empType/>
< /专栏>
< / ASP:GridView的>
code背后
列表<&副GT; associatesList =新的List<&副GT;();
associatesList.Add(新副教授{AssociateID = 1,的EmpID = 101的empType =合同});
associatesList.Add(新副教授{AssociateID = 2,的EmpID = 102的empType =常规});
Gridview1.DataSource = associatesList;
Gridview1.DataBind();
由于Vassy酒店的答案。完成code是
< ASP:的TemplateField的HeaderText =的EmpID>
<&ItemTemplate中GT;
<%#Convert.ToString(EVAL(的empType))==合同? XXX:Convert.ToString(EVAL(的EmpID))%GT; < ASP:按钮的ID =Button1的=服务器文本=按钮
可见='<%#的eval(的empType)==合同?真:假%GT;' /> < / ItemTemplate中>
< / ASP:的TemplateField>
相关:
行号可以通过以下<一个得到href=\"http://stackoverflow.com/questions/10831647/get-gridview-selected-row-values-using-page-$p$pvious-page\">Get使用页面previous页GridView的选择行值
CommandArgument =&LT;%#((GridViewRow)集装箱).RowIndex%GT;
I have a gridview as shown below. When the EmpType is contract the EmpID must be masked as "XXX"; for regular employees, actual EmpID should be shown. Also, when it is masked, I need to add a button control in the EmpID column.
I need to do it using mark-up; not using code behind. How can we write the conditional logic for Gridview's ItemTemplate for this logic?
Note: .Net 4.0
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="AssociateID" DataField="AssociateID" />
<asp:TemplateField HeaderText="EmpID">
<ItemTemplate>
<%# Eval("EmpID")%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="EmpType" DataField="EmpType" />
</Columns>
</asp:GridView>
CODE BEHIND
List<Associate> associatesList = new List<Associate>();
associatesList.Add(new Associate { AssociateID = 1, EmpID = 101, EmpType = "Contract" });
associatesList.Add(new Associate { AssociateID = 2, EmpID = 102, EmpType = "Regular" });
Gridview1.DataSource = associatesList;
Gridview1.DataBind();
Thanks to vassy's answer. Complete code is
<asp:TemplateField HeaderText="EmpID">
<ItemTemplate>
<%# Convert.ToString(Eval("EmpType")) == "Contract" ? "XXX" : Convert.ToString(Eval("EmpID"))%>
<asp:Button ID="Button1" runat="server" Text="Button"
Visible='<%# Eval("EmpType") == "Contract" ? true : false %>' />
</ItemTemplate>
</asp:TemplateField>
Related:
Row number can be obtained by following Get GridView Selected Row Values using Page Previous Page
CommandArgument="<%# ((GridViewRow)Container).RowIndex %>"
这篇关于为GridView的ItemTemplate中条件逻辑(使用仅标记)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!