中继器按钮命令参数为空字符串

发布于 2024-11-03 05:39:24 字数 3609 浏览 0 评论 0原文

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

中继器:

<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

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

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

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

发布评论

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

评论(1

苹果你个爱泡泡 2024-11-10 05:39:24

试试这个:

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

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

Address_ID 需要是您将数据绑定到转发器的对象的属性。

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.

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