从Code Behind隐藏DetailsView TemplateField

时间:2013-12-31 16:11:07

标签: asp.net webforms code-behind detailsview

我试图从后面的代码中有条件地隐藏DetailsView行。我希望能够根据某些条件调用SomeMethod并让它隐藏第12行。有没有人知道如何实现这一目标?

我使用调试器确认Row [12]是正确的行,并且该方法正在执行。

protected void SomeMethod(object sender, EventArgs e)
{
    DetailsView1.Rows[12].visible= false;

   //Alternative methods I attempted that also did not work.
     //DetailsViewRow row = DetailsView1.DataItem as DetailsViewRow;
     //row[12].Visible = false;
    //DetailsViewRow row = DetailsView1.Rows[12];
    //row.visible = false;
}

这是我的aspx。

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="ObjectDataSource2">
...
<asp:TemplateField HeaderText="Item" 
            SortExpression="Item">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Item") %>'></asp:TextBox>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Item") %>'></asp:TextBox>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Item") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
...

1 个答案:

答案 0 :(得分:5)

您必须使用Fields DetailsView的集合。您的方法应如下所示:

protected void SomeMethod(object sender, EventArgs e)
{
    DetailsView1.Fields[12].Visible= false;
}

要测试创建添加DetailsView到页面,请添加一个按钮:

<asp:DetailsView ID="DetailsView1" AutoGenerateRows="false"   runat="server" Height="50px" Width="125px">
    <Fields>

        <asp:TemplateField HeaderText="Id" SortExpression="Id">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Id") %>'></asp:TextBox>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Id") %>'></asp:TextBox>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name" SortExpression="Name">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Category" SortExpression="Category">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Category") %>'></asp:TextBox>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Category") %>'></asp:TextBox>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Category") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Fields>
</asp:DetailsView>
<br />
<asp:Button ID="btnHide" runat="server" Text="Hide" OnClick="SomeMethod" />

在代码中添加以下代码:

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DetailsView1.DataSource = GetProduct();
            DetailsView1.DataBind();
        }
    }

    private List<Product> GetProduct()
    {

        return new List<Product>()
        {
            new Product{Id=1, Name="Paper", Category="Stationery"},
            new Product{Id=2, Name="Pen", Category="Stationery"},
            new Product{Id=3, Name="Pencil", Category="Stationery"}
        };
    }

    protected void SomeMethod(object sender, EventArgs e)
    {
        DetailsView1.Fields[0].Visible = false;
    }
}


public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }
}

这是结果:

enter image description here