DetailsView - Hide row/field conditionally?

K

K B

Hi,

Is there ANY WAY to hide a row/field in a DetailsView based on meeting a
condition at the ItemCreated or ModeChanging event -- or any other way?
There appears not to be, but I was hoping for confirmation before
totally giving up.

Thanks,
Kit
 
P

PeterKellner

Hi,

Is there ANY WAY to hide a row/field in a DetailsView based on meeting a
condition at the ItemCreated or ModeChanging event -- or any other way?
There appears not to be, but I was hoping for confirmation before
totally giving up.

Thanks,
Kit


You can access the "fields" collection and set them invisible. Ignore
the fact that I converted the email column to a template. I was just
experimenting.

Here is an example that turns off the email column in the page_load
event.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
DetailsView1.Fields[2].Visible = false;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="DetailsView1" runat="server"
AutoGenerateRows="False" DataKeyNames="UserName"
DataSourceID="ObjectDataSource1" Height="50px"
Width="125px">
<Fields>

<asp:BoundField DataField="UserName"
HeaderText="UserName" ReadOnly="True" SortExpression="UserName" />
<asp:CheckBoxField DataField="IsApproved"
HeaderText="IsApproved" SortExpression="IsApproved" />
<asp:TemplateField HeaderText="Email"
SortExpression="Email">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("Email") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("Email") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("Email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>

</div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DeleteMethod="Delete"
InsertMethod="Insert"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetMembers"
TypeName="MembershipUtilities.MembershipUserODS"
UpdateMethod="Update">
<DeleteParameters>
<asp:parameter Name="UserName" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="UserName" Type="String" />
<asp:parameter Name="email" Type="String" />
<asp:parameter Name="isApproved" Type="Boolean" />
<asp:parameter Name="comment" Type="String" />
<asp:parameter Name="lastActivityDate" Type="DateTime"
/>
<asp:parameter Name="lastLoginDate" Type="DateTime" />
<asp:parameter Name="password" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:parameter Name="sortData" Type="String" />
</SelectParameters>
<InsertParameters>
<asp:parameter Name="userName" Type="String" />
<asp:parameter Name="isApproved" Type="Boolean" />
<asp:parameter Name="comment" Type="String" />
<asp:parameter Name="lastLockoutDate" Type="DateTime"
/>
<asp:parameter Name="creationDate" Type="DateTime" />
<asp:parameter Name="email" Type="String" />
<asp:parameter Name="lastActivityDate" Type="DateTime"
/>
<asp:parameter Name="providerName" Type="String" />
<asp:parameter Name="isLockedOut" Type="Boolean" />
<asp:parameter Name="lastLoginDate" Type="DateTime" />
<asp:parameter Name="isOnline" Type="Boolean" />
<asp:parameter Name="passwordQuestion" Type="String"
/>
<asp:parameter Name="lastPasswordChangedDate"
Type="DateTime" />
<asp:parameter Name="password" Type="String" />
<asp:parameter Name="passwordAnswer" Type="String" />
</InsertParameters>
</asp:ObjectDataSource>
</form>
</body>
</html>
Peter Kellner
http://peterkellner.net
 
P

PeterKellner

Hi,

Is there ANY WAY to hide a row/field in a DetailsView based on meeting a
condition at the ItemCreated or ModeChanging event -- or any other way?
There appears not to be, but I was hoping for confirmation before
totally giving up.

Thanks,
Kit

actually, after re-reading your post, you want to see how to not show
fields programatically. I did that first and then confused myself to
think you wanted just the other. So, a few control-z's and this has
an example of both getting rid of a field and of getting rid of a
complete attribute. To hide the field, you just set the visible
property of the column in the template.

Good luck and here is the code again with some extra stuff.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected bool GetShowEmail()
{
// put your logic here to return true or false for visible or
not visible
return false;
}


protected void Page_Load(object sender, EventArgs e)
{
DetailsView1.Fields[2].Visible = false;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="DetailsView1" runat="server"
AutoGenerateRows="False" DataKeyNames="UserName"
DataSourceID="ObjectDataSource1" Height="50px"
Width="125px">
<Fields>

<asp:BoundField DataField="UserName"
HeaderText="UserName" ReadOnly="True" SortExpression="UserName" />
<asp:CheckBoxField DataField="IsApproved"
HeaderText="IsApproved" SortExpression="IsApproved" />
<asp:TemplateField HeaderText="Email"
SortExpression="Email">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("Email") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("Email") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
visible='<%# (bool) GetShowEmail() %>' Text='<%# Bind("Email")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>

</div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DeleteMethod="Delete"
InsertMethod="Insert"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetMembers"
TypeName="MembershipUtilities.MembershipUserODS"
UpdateMethod="Update">
<DeleteParameters>
<asp:parameter Name="UserName" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="UserName" Type="String" />
<asp:parameter Name="email" Type="String" />
<asp:parameter Name="isApproved" Type="Boolean" />
<asp:parameter Name="comment" Type="String" />
<asp:parameter Name="lastActivityDate" Type="DateTime"
/>
<asp:parameter Name="lastLoginDate" Type="DateTime" />
<asp:parameter Name="password" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:parameter Name="sortData" Type="String" />
</SelectParameters>
<InsertParameters>
<asp:parameter Name="userName" Type="String" />
<asp:parameter Name="isApproved" Type="Boolean" />
<asp:parameter Name="comment" Type="String" />
<asp:parameter Name="lastLockoutDate" Type="DateTime"
/>
<asp:parameter Name="creationDate" Type="DateTime" />
<asp:parameter Name="email" Type="String" />
<asp:parameter Name="lastActivityDate" Type="DateTime"
/>
<asp:parameter Name="providerName" Type="String" />
<asp:parameter Name="isLockedOut" Type="Boolean" />
<asp:parameter Name="lastLoginDate" Type="DateTime" />
<asp:parameter Name="isOnline" Type="Boolean" />
<asp:parameter Name="passwordQuestion" Type="String"
/>
<asp:parameter Name="lastPasswordChangedDate"
Type="DateTime" />
<asp:parameter Name="password" Type="String" />
<asp:parameter Name="passwordAnswer" Type="String" />
</InsertParameters>
</asp:ObjectDataSource>
</form>
</body>
</html>
Peter Kellner
http://peterkellner.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top