views:

190

answers:

1

NB. I am going to be away until Tuesday next week. So all help is appreciated but I will be unable to comment/respond until then.

I have a FormView that modifies an instance of a custom class. The various form controls (TextBox, DropDownList etc.) are working fine. However, I want to include a Button that will modify the state of the DataItem based on some very simple logic. There is no form control which could control this change in a non-confusing way.

The actual situation is I have a form for entering an address. The address might be a "standard" Australian address (street # and name, suburb state and postcode) or it might be "non-standard" which means it has 3 address lines before suburb (for people with more specific address requirements). I want a button that says "add more lines" and clicking it will change the object from being AddressLines.StandardAustralian to AddressLines.NonStandardAustralian. For non-standard addresses there will be another button that says "Remove extra lines" and clicking that will reverse the process.

So I tried adding a Button and modifying the state of the DataItem in the code-behind. But the problem I encounter is that the FormView's DataItem is null/nothing. From reading this SO question it seems the problem is that the item is not databound when the Button's Click event is fired.

So, the question; Is it possible to get the DataItem for the FormView during a Button's Click event? and if not: what are my options for implementing this?

Thanks in advance.

Code Behind:

Private ReadOnly Property addressView() As AddressView
    Get
        Return CType(FormView1.DataItem, AddressView) ' <-- But DataItem is Nothing when called from lbMakeNonStd_Click
    End Get
End Property

Protected Sub lbMakeNonStd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbMakeNonStd.Click
    If addressView IsNot Nothing Then

        Select Case addressView.NonStd
            Case AddressLines.StandardAustralian
                addressView.NonStd = AddressLines.NonStandardAustralian

            Case AddressLines.NonStandardAustralian
                addressView.NonStd = AddressLines.StandardAustralian

            Case Else
                ' Other cases ignored, shouldn't change address lines
        End Select
    End If
End Sub

Aspx:

<asp:FormView ID="FormView1" runat="server" DataKeyNames="IDNO, AddressType" DataSourceID="ObjectDataSource1" EnableViewState="true" >
<ItemTemplate>
    ...
</ItemTemplate>
<EditItemTemplate>
    <fieldset>
    <legend>Address</legend>
    <asp:UpdatePanel ID="upAddressFields" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger controlid="txtPostcode" eventname="TextChanged" />
    </Triggers>
    <ContentTemplate>
    <asp:Table ID="tblForm" runat="server">
        <asp:TableRow ID="trName" runat="server">
            <asp:TableHeaderCell ID="TableCell1" runat="server">
                Name
            </asp:TableHeaderCell>
            <asp:TableCell ID="TableCell2" runat="server">
                <asp:TextBox ID="tbName" runat="server" Text='<%# Bind("AlternateName") %>' MaxLength="30"></asp:TextBox>
            </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow ID="TableRow2" runat="server" Cs>
            <asp:TableHeaderCell ID="TableCell3" runat="server">
                Number and Street
            </asp:TableHeaderCell>
            <asp:TableCell ID="TableCell4" runat="server">
                <asp:TextBox ID="tbLine1" runat="server" Text='<%# Bind("Line1") %>' MaxLength="30"></asp:TextBox>
                <asp:PlaceHolder ID="phMakeNonStdButton" runat="server">(<asp:LinkButton ID="lbMakeNonStd" runat="server" Text="Add more lines..." />)</asp:PlaceHolder>
            </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow ID="trLine2" runat="server" CssClass="tablerowbg_light">
            <asp:TableHeaderCell ID="TableCell5" runat="server">
                Line 2
            </asp:TableHeaderCell>
            <asp:TableCell ID="TableCell6" runat="server">
                <asp:TextBox ID="tbLine2" runat="server" Text='<%# Bind("Line2") %>' MaxLength="30"></asp:TextBox>
                <br /><asp:LinkButton ID="lbMakeStd" runat="server" Text="Use fewer lines..." />
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>
    </ContentTemplate>
    </asp:UpdatePanel>
    <asp:LinkButton ID="lbUpdate" runat="server" CommandName="Update" ValidationGroup="ResidentialAddress" Font-Bold="true">Save Changes</asp:LinkButton> |
    <asp:LinkButton ID="lbCancel" runat="server" CommandName="Cancel" CausesValidation="false">Cancel</asp:LinkButton>
</EditItemTemplate>

A: 

Ok, so I have this working. Perhaps not the best way it could be done it works.

Basically I added a hidden field bound to the value I wanted to change. Then in the button's click event method I modified the value of the hidden field and let the FormView update the custom class. This is instead of getting the DataItem from the FormView and modifying it directly as I was trying at the start.

David