听到我发布了我的网格视图。
<Columns>
<asp:TemplateField HeaderText="Item">
<ItemTemplate>
<p> <asp:Label ID="lblproductname" display="Dynamic" runat="server" Text='<%# Bind("productname") %>'></asp:Label></p>
<p><asp:Label ID="lblProductWeight" display="Dynamic" runat="server" Text='<%# Bind("groupvalue") %>'></asp:Label></p>
<p> <asp:Label ID="lblProductType" display="Dynamic" runat="server" Text='<%# Bind("groupname") %>'></asp:Label></p>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:BoundField DataField="Quantity" HeaderText="Qty">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Price" HeaderText="Price">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="SubTotal" HeaderText="Sub Total">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
</Columns>
<FooterStyle CssClass="datatable" />
</asp:GridView>
那么如何在xyz方法中以编程方式制作标签的错误ItemTemplate
答案 0 :(得分:3)
在网格的Rowcommand中,您可以隐藏或显示项目模板,例如:
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//Your Condition
Label lblproductname = (Label)e.Row.FindControl("lblproductname")
If(a > B)
lblproductname.Visible = true;
//Others Lables
....
}
}
我希望有所帮助。
答案 1 :(得分:0)
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[columnIndex].Visible = false;
}
我建议的唯一解决方法是在ASPX页面上提供一个HeaderText,然后找到它。
protected void xyz()
{
((DataControlField)youGridView.Columns
.Cast<DataControlField>()
.Where(fld => fld.HeaderText == "your header text")
.SingleOrDefault()).Visible = false;
}
答案 2 :(得分:0)
我相信你的问题在这里得到解答:
How to find control in TemplateField of GridView?
从答案中复制:
foreach(GridViewRow row in GridView1.Rows) {
if(row.RowType == DataControlRowType.DataRow) {
Label myLabel = row.FindControl("myLabelID") as Label;
myLabel.Visible = false;
}
}
来自RowDataBoundEvent:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
Label myLabel = e.Row.FindControl("myLabelID") as Label;
myLabel.Visible = false;
}
}