中继器按钮CommandArgument为空字符串 [英] Repeater Button CommandArgument is Empty String
问题描述
失去我的心与这一个。我的按钮获取空字符串的commandargument即使commandargument获取设置。我已经验证它被设置为在调试模式下正确的ID,但后来当我去以后访问此commandargument在ItemCommand事件的commandarguments是空字符串的中继器。我不知道为什么。我最终得到一个平方外键异常,因为它是从空字符串值插入0的ID。有这方面将其复位中继器按钮没有其他code。
中继器:
< ASP:直放站ID =repeaterAddressesViewStateMode =已启用的DataSourceID =sqlFacilityAddresses=服务器>
<&ItemTemplate中GT;
<选择:地址ID =AddressControl=服务器/>
< ASP:按钮的ID =btnMarkAsBilling=服务器的EnableViewState =真的CommandName =结算文本=标记为账单地址/>
< ASP:按钮的ID =btnMarkAsPhysical=服务器的EnableViewState =真的CommandName =物理文本=标记为物理地址/>
< / ItemTemplate中>
< / ASP:直放站>
背后code:
保护小组repeaterAddresses_ItemCreated(BYVAL发件人为对象,BYVAL E上RepeaterItemEventArgs)处理repeaterAddresses.ItemCreated
如果不IsNothing(DirectCast(e.Item.DataItem,DataRowView的))然后
如果(e.Item.ItemType = ListItemType.Item)或(e.Item.ItemType = ListItemType.AlternatingItem)然后
昏暗addressControl由于地址= DirectCast(e.Item.FindControl(AddressControl),地址)
addressControl.AddressID = CINT(DirectCast(e.Item.DataItem,DataRowView的)(ADDRESS_ID)。toString()方法)
昏暗btnBilling As按钮= DirectCast(e.Item.FindControl(btnMarkAsBilling),按钮)
btnBilling.CommandArgument = CINT(DirectCast(e.Item.DataItem,DataRowView的)(ADDRESS_ID)。toString()方法)
昏暗btnPhysical As按钮= DirectCast(e.Item.FindControl(btnMarkAsPhysical),按钮)
btnPhysical.CommandArgument = CINT(DirectCast(e.Item.DataItem,DataRowView的)(ADDRESS_ID)。toString()方法)
万一
万一
结束小组
保护小组repeaterAddress_ItemCommand(BYVAL发件人为对象,BYVAL E上RepeaterCommandEventArgs)处理repeaterAddresses.ItemCommand
昏暗康恩作为新的SqlConnection(ConfigurationManager.ConnectionStrings(Trustaff_ESig2)。ConnectionString中)
昏暗的按钮如按钮= CTYPE(e.CommandSource,巴顿) 选择案例e.CommandName
案结算
昏暗cmdBillingAddress作为新的SqlCommand([SP_Facility_UpdateBillingAddress],康恩)
随着cmdBillingAddress
.CommandType = CommandType.StoredProcedure
.Parameters.Add(新的SqlParameter(@ Facility_ID,DbType.Int32))。值= sqlFacilityAddresses.SelectParameters(Facility_ID)。默认值
.Parameters.Add(新的SqlParameter(@ BillingAddress_ID,DbType.Int32))。值= e.CommandArgument
结束与
conn.Open()
cmdBillingAddress.ExecuteNonQuery()
conn.Close()
案物理
昏暗cmdPhysicalAddress作为新的SqlCommand([SP_Facility_UpdatePhysicalAddress],康恩)
随着cmdPhysicalAddress
.CommandType = CommandType.StoredProcedure
.Parameters.Add(新的SqlParameter(@ Facility_ID,DbType.Int32))。值= sqlFacilityAddresses.SelectParameters(Facility_ID)。默认值
.Parameters.Add(新的SqlParameter(@ PhysicalAddress_ID,DbType.Int32))。值= e.CommandArgument
结束与
conn.Open()
cmdPhysicalAddress.ExecuteNonQuery()
conn.Close()
结束选择
PopulateBillingPhysicalAddresses(sqlFacilityAddresses.SelectParameters(Facility_ID)。默认值)
结束小组
试试这个:
< ASP:按钮的ID =btnMarkAsBilling=服务器的EnableViewState =真
的CommandName =结算文本=标记为账单地址
CommandArgument ='<%#的eval(ADDRESS_ID)%>/>
请注意属性值周围的单引号。如果是双引号ASP.NET将不会执行服务器端数据绑定code。你需要从你的code后面删除 CommandArgument
任务。
ADDRESS_ID
将需要你在数据绑定到中继器的对象上的属性。
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
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.
这篇关于中继器按钮CommandArgument为空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!