3

これで私の心を失います。commandargument が設定されていても、私のボタンは空の文字列の commandargument を取得します。デバッグ モードで正しい ID に設定されることを確認しましたが、後でリピーターの ItemCommand イベントでこのコマンド引数にアクセスすると、コマンド引数は空の文字列になります。そして、私には理由がわかりません。空の文字列値から 0 の ID を挿入しているため、sq 外部キー例外が発生します。それをリセットするリピーターボタンに関する他のコードはありません。

リピータ:

<asp:Repeater ID="repeaterAddresses" ViewStateMode="Enabled" DataSourceID="sqlFacilityAddresses" runat="server">
    <ItemTemplate>
        <Select:Address ID="AddressControl" runat="server"  />
        <asp:Button ID="btnMarkAsBilling" runat="server" EnableViewState="true" CommandName="Billing" Text="Mark as billing address" />
        <asp:button ID="btnMarkAsPhysical" runat="server" EnableViewState="true" CommandName="Physical" Text="Mark as physical address" />
    </ItemTemplate>
</asp:Repeater>

コードビハインド:

    Protected Sub repeaterAddresses_ItemCreated(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles repeaterAddresses.ItemCreated
        If Not IsNothing(DirectCast(e.Item.DataItem, DataRowView)) Then
            If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
                Dim addressControl As Address = DirectCast(e.Item.FindControl("AddressControl"), Address)
                addressControl.AddressID = CInt(DirectCast(e.Item.DataItem, DataRowView)("Address_ID").ToString())
                Dim btnBilling As Button = DirectCast(e.Item.FindControl("btnMarkAsBilling"), Button)
                btnBilling.CommandArgument = CInt(DirectCast(e.Item.DataItem, DataRowView)("Address_ID").ToString())
                Dim btnPhysical As Button = DirectCast(e.Item.FindControl("btnMarkAsPhysical"), Button)
                btnPhysical.CommandArgument = CInt(DirectCast(e.Item.DataItem, DataRowView)("Address_ID").ToString())
            End If
        End If
    End Sub
   Protected Sub repeaterAddress_ItemCommand(ByVal sender As Object, ByVal e As RepeaterCommandEventArgs) Handles repeaterAddresses.ItemCommand
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("Trustaff_ESig2").ConnectionString)
        Dim button As Button = CType(e.CommandSource, Button)

        Select Case e.CommandName
            Case "Billing"
                Dim cmdBillingAddress As New SqlCommand("[SP_Facility_UpdateBillingAddress]", conn)
                With cmdBillingAddress
                    .CommandType = CommandType.StoredProcedure
                    .Parameters.Add(New SqlParameter("@Facility_ID", DbType.Int32)).Value = sqlFacilityAddresses.SelectParameters("Facility_ID").DefaultValue
                    .Parameters.Add(New SqlParameter("@BillingAddress_ID", DbType.Int32)).Value = e.CommandArgument
                End With
                conn.Open()
                cmdBillingAddress.ExecuteNonQuery()
                conn.Close()
            Case "Physical"
                Dim cmdPhysicalAddress As New SqlCommand("[SP_Facility_UpdatePhysicalAddress]", conn)
                With cmdPhysicalAddress
                    .CommandType = CommandType.StoredProcedure
                    .Parameters.Add(New SqlParameter("@Facility_ID", DbType.Int32)).Value = sqlFacilityAddresses.SelectParameters("Facility_ID").DefaultValue
                    .Parameters.Add(New SqlParameter("@PhysicalAddress_ID", DbType.Int32)).Value = e.CommandArgument
                End With
                conn.Open()
                cmdPhysicalAddress.ExecuteNonQuery()
                conn.Close()
        End Select
        PopulateBillingPhysicalAddresses(sqlFacilityAddresses.SelectParameters("Facility_ID").DefaultValue)
    End Sub
4

1 に答える 1

11

これを試して:

<asp:Button ID="btnMarkAsBilling" runat="server" EnableViewState="true" 
    CommandName="Billing" Text="Mark as billing address" 
    CommandArgument='<%# Eval("Address_ID") %>'/>

属性値が一重引用符で囲まれていることに注意してください。二重引用符の場合、ASP.NET はサーバー側のデータ バインディング コードを実行しません。CommandArgumentコード ビハインドから割り当てを削除する必要があります。

Address_IDリピーターにデータバインディングしているオブジェクトのプロパティである必要があります。

于 2011-04-26T21:21:47.623 に答える