如何根据按钮单击修改 ASP.NET FormView 中的自定义对象?

发布于 2024-08-29 10:22:01 字数 4412 浏览 11 评论 0原文

<代码>注意。我要离开到下周二。因此,感谢所有帮助,但在此之前我将无法发表评论/回复。

我有一个可以修改自定义类实例的 FormView。各种表单控件(TextBox、DropDownList 等)工作正常。但是,我想包含一个按钮,它将根据一些非常简单的逻辑修改 DataItem 的状态。没有任何表单控件可以以不令人困惑的方式控制此更改。

实际情况是我有一个用于输入地址的表格。该地址可能是“标准”澳大利亚地址(街道号和名称、郊区州和邮政编码),也可能是“非标准”,这意味着它在郊区之前有 3 个地址行(对于有更具体地址要求的人)。我想要一个显示“添加更多行”的按钮,单击它会将对象从 AddressLines.StandardAustralian 更改为 AddressLines.NonStandardAustralian。对于非标准地址,会有另一个按钮显示“删除多余的行”,单击该按钮将反转该过程。

所以我尝试添加一个 Button 并修改代码隐藏中 DataItem 的状态。但我遇到的问题是 FormView 的 DataItem 为空/无。通过阅读这个SO问题似乎问题在于,当触发按钮的 Click 事件时,该项目没有数据绑定。

所以,问题是; 是否可以在按钮的单击事件期间获取 FormView 的 DataItem?,如果不能:实现此操作的选项有哪些?

提前致谢。

背后代码:

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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深府石板幽径 2024-09-05 10:22:01

好的,所以我有这个工作。也许这不是最好的方法,但它确实有效。

基本上我添加了一个隐藏字段绑定到我想要更改的值。然后在按钮的单击事件方法中我修改了隐藏字段的值并让 FormView 更新自定义类。这不是像我一开始尝试的那样从 FormView 获取 DataItem 并直接修改它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文