DetailsView: Change TemplateField properties dynamically (vb)

S

sck10

Hello,

I am trying to programically make the following TemplateField ("MyTemplate")
visible when the user clicks on the "Edit" Button using the PreRender handle
event. My question is, how do you reference the properties of a particular
TemplateField? For example:

Protected Sub dvDetail_PreRender(ByVal sender As Object, ByVal e As
EventArgs) Handles dvDetail.PreRender
If Me.dvDetail.CurrentMode = DetailsViewMode.Edit Then
MyTemplate.Visible = True
End If
End Sub


<asp:TemplateField
Visible="false"
HeaderText="Activity Date">
<ItemTemplate>
<asp:Label ID="lblActivityDate" Text='<%# Eval("ActivityDate",
"{0:d}") %>' Runat="Server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtActivityDate" Text='<%# Bind("ActivityDate",
"{0:d}") %>' Runat="Server" />
</EditItemTemplate>
</asp:TemplateField>
 
G

Guest

Ctype(sender,DataGrid).Columns(i).Visible= False
'i is the index of your templatefield
 
S

Steven Cheng[MSFT]

Hi Sck10,

Regarding on the problem you mentioned, we can access all the Fields we
defined in DetailsView control through its DetailsView.Fields Property
(through index..)

And of course we can change a certain one's Visble propety. However, as you
mentioned that what you need is hidden the TemplateField in normal status ,
but make it visible when DetailsView turn into Edit mode, yes? If so, we
can just put UI controls in that templateField's "EditTemplate" and put
nothing in "ItemTemplate" , thus there will only displaying UI for that
TemplateField in edit mode. Does this also meet your requirement?

If there're any other questions on this, please feel fee to post here.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "sck10" <[email protected]>
| Subject: DetailsView: Change TemplateField properties dynamically (vb)
| Date: Fri, 4 Nov 2005 14:00:08 -0600
| Lines: 29
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 189.202.185.135.in-addr.arpa 135.185.202.189
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:136212
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hello,
|
| I am trying to programically make the following TemplateField
("MyTemplate")
| visible when the user clicks on the "Edit" Button using the PreRender
handle
| event. My question is, how do you reference the properties of a
particular
| TemplateField? For example:
|
| Protected Sub dvDetail_PreRender(ByVal sender As Object, ByVal e As
| EventArgs) Handles dvDetail.PreRender
| If Me.dvDetail.CurrentMode = DetailsViewMode.Edit Then
| MyTemplate.Visible = True
| End If
| End Sub
|
|
| <asp:TemplateField
| Visible="false"
| HeaderText="Activity Date">
| <ItemTemplate>
| <asp:Label ID="lblActivityDate" Text='<%# Eval("ActivityDate",
| "{0:d}") %>' Runat="Server"/>
| </ItemTemplate>
| <EditItemTemplate>
| <asp:TextBox id="txtActivityDate" Text='<%# Bind("ActivityDate",
| "{0:d}") %>' Runat="Server" />
| </EditItemTemplate>
| </asp:TemplateField>
|
|
|
 
Joined
Aug 19, 2009
Messages
1
Reaction score
0
How to control visibility of a TemplateField

I did a google search on how to programmatically control a TemplateField, specifically the Visibility attribute.

TemplateField's do not DataBind, so they can't be programmatically controlled that easily, other than

Code:
MyGridView.Columns[columnIndex].Visibile = TrueFalseValue;

Even this doesn't guarantee it would work.

Here's my simple solution:
- Use 2 Labels in the HeaderTemplate, and set the Visible attribute of both to show/hide via a call to a function;
- Use 2 Panels in the ItemTemplate area around your controls, and and set the Visible attribute to show/hide via a call to a function;

Code:
      <asp:TemplateField HeaderStyle-HorizontalAlign="Left">
       <HeaderTemplate>
          <asp:Label ID="lblOpenInWindowT" Text="Open in Window ..." Visible-='<%# !ShowAdminTools() %>' runat="server" />
          <asp:Label ID="lblAdminToolsT" Text="Admin Tools" Visible-='<%# ShowAdminTools() %>' runat="server" />
       </HeaderTemplate>
        <ItemTemplate>
          <asp:Panel ID="pnlOpenInWindow" Visible-='<%# !ShowAdminTools() %>' runat="server">
            <asp:HyperLink ID="hlink" Text="New" NavigateUrl='<%# Eval("virtualPathFull") %>' Target="_blank" runat="server" />
            &nbsp;&nbsp;
            <asp:LinkButton ID="lnkCurrentWindow" Text="Current" CommandName="Current" CommandArgument='<%# Eval("virtualPathFull").ToString() %>' runat="server" />
            &nbsp;&nbsp;
            <asp:LinkButton ID="lnkOpenBelowList" Text="Below" CommandName="Below" CommandArgument='<%# Eval("virtualPathFull").ToString() %>' runat="server" /> 
            &nbsp;&nbsp;
          </asp:Panel>\
          
          <asp:Panel ID="pnlAdminFileTools" Visible='<%# ShowAdminTools() %>' runat="server">
            <asp:HyperLink ID="hlinkAdmin" Text="Link" NavigateUrl='<%# Eval("virtualPathFull") %>' Target="_blank" runat="server" />
            <asp:ImageButton ID="btnFileEdit" CausesValidation="false" ImageUrl="~/images/fileeditdisabled.jpg" Enabled="false" AlternateText="Edit File"  OnClientClick="return false;" CommandName="fileedit" CommandArgument="" runat="server" />
            <asp:ImageButton ID="btnFileDelete" CausesValidation="false" ImageUrl="~/images/filedelete.jpg" AlternateText="Delete File" OnClientClick="return confirm('Delete this file?');" CommandName="filedelete" runat="server" />
            <asp:ImageButton ID="btnFileRename" CausesValidation="false" ImageUrl="~/images/rename.jpg" AlternateText="Rename File" OnClientClick="return confirm('Rename this file?');" CommandName="filerename" CommandArgument="" runat="server" />
          </asp:Panel>
        </ItemTemplate>
      </asp:TemplateField>


Now, the code below, you'll want to store the bShowHide value in a session variable or other page variable. I use a session variable, as I show/hide the column controls on an AJAX postback depending upon the user's control preferences.

Code:
public bool ShowHide()
{
  return bShowHide_SessionVariable;
}


GaryN
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top