FormView Tips

FormView is  a  new  control  released with ASP.NET 2.0

1. To access controls inside template of FormView

You can use FormView.Row.FindControl("ControlID"), but make sure, we can only retrieve control instance in a certain template when the template is in active. In other word, in normal mode, we can access controls in ItemTemplate through
FormView.Row.FindControl( controlID) and only in Edit mode can we access controls in Edit template.

2. When to user FindControl()

It should be used during the FomView.DataBinding or FromView.DataBound or any later event, but not during handling the page.load, as explained, because the Page.Load event fires before the FormView is populated with the data.

3. Data Binding

Declaratively like this:

<asp:Label Id="COUNTLabel" runat="server" Text='<%# Bind ("COUNTFieldName")%>' Visible='<%# Eval("COUNTFieldName").ToString().Equals("0") %>' ></Label>

Or the way look like this:
Label lbl=(Label)FormView1.Row.FindControl("COUNTLabel");


4. DropDownList Control

You can directly bind data like this:

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2"
        DataTextField="CategoryName" DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryID") %>'>
</asp:DropDownList>

Or:

You can user FindControl() to find the DropDownList when the FormView is in Edit Mode


5. A lesson Learned

The use of the DataKeyNames property in the <asp:FormView> when executing insert/update/delete it is compulsory

posted on 2006-03-16 16:16  signaldance  阅读(921)  评论(0编辑  收藏  举报