I am attempting to hide a DetailsView Row conditionally from code behind. I'd like to be able to call SomeMethod based on some condition and have it to hide Row 12. Does anyone know how I can go about accomplishing this?
I have used the debugger to confirm that Row[12] is the correct row and that the method is being executed.
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;
}
This is my 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>
...
You have to use Fields
collection of DetailsView
. Your method should look like:
protected void SomeMethod(object sender, EventArgs e)
{
DetailsView1.Fields[12].Visible= false;
}
To test create add a DetailsView to page, add a button:
<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" />
And add the following code in code behind:
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; }
}
And here's the result: