Thursday, September 11, 2008

Going codeless - Conditionally hide a control in a formview using databinding

I am a big fan of “codeless” code.  I’ve been relying on FormView controls quite a bit lately.  In one form I needed to show a textbox when the form is in Insert mode, but hide for Edit (I only use one template, EditItemTemplate, in my FormView).  Playing around with WPF had me thinking of ways to bind to properties that already existed rather than create a wrapper property, so I tried binding the TextBox.Visible property to the FormView.CurrentMode property and it WORKED!  I find this much cleaner than adding a FormView.FindControl(“inAgencyId”) in the codebehind.

<asp:TextBox
ID="inAgencyId"
runat="server"
Text="<%# Bind("AgencyCode") %>"
Visible="<%# (fvwComp1.CurrentMode = FormViewMode.Insert) %>"
/>

 

No comments: