Repeater Button CommandArgument is Empty String

╄→尐↘猪︶ㄣ 提交于 2019-12-10 21:59:02

问题


Losing my mind with this one. My button gets a commandargument of empty string even though the commandargument gets set. I have verified it gets set to the correct ID in debug mode, but then when I go to access this commandargument later in the repeaters ItemCommand event the commandarguments are empty string. And I have no idea why. I end up getting a sq foreign key exception because it is inserting an ID of 0 from the empty string values. There is no other code regarding the repeaters buttons that would be resetting it.

Repeater:

<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>

Code behind:

    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

回答1:


Try this:

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

Note the single quotes around the attribute value. ASP.NET will not execute the server side data binding code if they are double quotes. You'll need to remove the CommandArgument assignments from your code behind.

Address_ID will need to be a property on the object you are databinding to the repeater.



来源:https://stackoverflow.com/questions/5796564/repeater-button-commandargument-is-empty-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!