在CodeBehind类中,我有这个:
Public Class MyControl
Inherits UserControl
' Is used in other functions so cannot be hardcoded:
Public Enum TabID
Accepted = 138
Declined = 139
Unknown = 140
InProgress = 141
End Enum
Public Sub Tab_OnClick(sender As Object, e As EventArgs)
Dim selectedTabID As Integer
Dim linkBtn As LinkButton
If Page.IsPostBack Then
linkBtn = CType(sender, LinkButton)
selectedTabID = CType(linkBtn.CommandArgument, Integer)
' Do stuff with selectedTabID...
End If
End Sub
' More irrelevant code...
End Class
在ascx文件中(前面的代码?)我有这个:
<asp:LinkButton id="AcceptedTab" Runat="server" Text="Accepted" CssClass="selected"
CommandArgument="<%# TabID.Accepted %>" OnClick="Tab_OnClick"/>
问题是,行selectedTabID = CType(linkBtn.CommandArgument, Integer)
显示调试期间linkBtn.CommandArgument
是一个空字符串(应该是&#34; 138&#34;)并且它无法将空字符串转换为整数在运行期间。
我还试过将enum转换为ascx文件中的整数:
<asp:LinkButton id="AcceptedTab" Runat="server" Text="Accepted" CssClass="selected"
CommandArgument="<%# CInt(TabID.Accepted) %>" OnClick="Tab_OnClick"/>
如何使用此方法在Tab_OnClick函数中获取此枚举值?
答案 0 :(得分:2)
您需要在Page Load上调用DataBind()
,因为您要将数据绑定到<%#
的LinkButton。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
DataBind
End Sub
其次,切换到Command
,而不是Click
。然后您可以直接访问CommandArgument
<asp:LinkButton id="AcceptedTab" Runat="server" Text="Accepted" CssClass="selected"
CommandArgument="<%# CInt(TabID.Accepted) %>" OnCommand="AcceptedTab_Command"/>
在代码背后。
Protected Sub AcceptedTab_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
Dim arg As String = e.CommandArgument.ToString
End Sub