FormView DataBinding: Safely Detecting FormView Status Based on a TextBox or DropDownList

FormView controls are very useful in rapidly putting together a multi-view Web form. As part of this, dynamically changing the status of the Form between Read, Edit, Insert is an important task.

The catch can be that this can get quirky to do right. and the simplest method is to programmatically trigger an event when a Control in your Form gets rendered or bound to data. The ways of doing so vary depending on the type of control that you are checking. In this example I will take a look at adding an event to the DropDownList and to the TextBox controls.

With the DropDownList control you can call the OnDataBinding=”Do_Something” method which is a sure-fire way to find out the current state of your FormView Control.

For example, if you change the state of your FormView Control somewhere along the line while processing your page, then detecting the databinding state of your dropdownlist control is a straightforward and simple way to tell what state your FormView is currently in. This is more accurate and simple than detecting the FormView _ModeChanged event, which doesn’t fire when you change the mode from one state to another in your code.

So, if you change your FormView state from Edit to Readonly in the Page_Init Event, the FormView _ModeChanged event does not get called. If you try to work around this by calling a handler function after you set .ChangeMode(FormViewMode.ReadOnly), you’ll quickly find out that you cannot access any of the controls within your FormView.

The simplest and most straightforward solution is to trigger a function when a unique control is instantiated within the correct template of your FormView control.
Interestingly enough, you cannot call the OnDataBinding=”Do_Something” method with a TextBox control. Instead, you need to use the OnPreRender method as: OnPreRender=”DoSomething”

So, while your dropdown control may appear something like this:

<asp:DropDownList ID="ddlTypeEdt" runat="server" DataSourceID="dsPopulateDropDown" DataValueField="SomeId" DataTextField="SomeDescription" OnDataBound="Do_Something"> </asp:DropDownList>

Your TextBox would appear something like this:

<asp:TextBox ID="txtTestId" runat="server" Text='<%# Bind("TestId") %>' OnPreRender="Do_Something"></asp:TextBox>

In the meantime, your Do_Something method for either case would look something like this in Visual Basic:

Protected Sub Do_Something(sender As Object, e As System.EventArgs)
 ‘ Handle your code here
End Sub
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s