【问题标题】:Repeater Button CommandArgument is Empty String中继器按钮 CommandArgument 为空字符串
【发布时间】:2011-08-13 09:14:44
【问题描述】:

因为这个而失去理智。即使设置了命令参数,我的按钮也会获得空字符串的命令参数。我已经验证它在调试模式下设置为正确的 ID,但是当我稍后在中继器 ItemCommand 事件中访问此命令参数时,命令参数是空字符串。我不知道为什么。我最终得到了一个 sq 外键异常,因为它从空字符串值中插入了 0 的 ID。没有其他关于将重置它的中继器按钮的代码。

中继器:

<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

【问题讨论】:

    标签: asp.net vb.net repeater commandargument


    【解决方案1】:

    试试这个:

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

    注意属性值周围的单引号。如果它们是双引号,ASP.NET 将不会执行服务器端数据绑定代码。您需要从后面的代码中删除 CommandArgument 分配。

    Address_ID 需要是您要绑定到中继器的对象的属性。

    【讨论】:

    • 这行得通,但我不明白为什么命令参数设置背后的代码不起作用。我已经以这种方式多次使用它与 gridviews 并且效果很好。
    • 您的数据绑定代码是否包含在 Page.IsPostBack 的检查中?
    • 搞砸了一段时间,不知道需要使用单引号而不是普通引号。非常感谢!
    猜你喜欢
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 2017-12-08
    • 2010-11-25
    相关资源
    最近更新 更多