我在usercontrol中有两个ASP.NET ListBoxex。 数据绑定事件触发一次。我确定,我确实跟踪。 但是listbox总是返回第一个值! 这对我来说很奇怪。 我不知道如何解决它。 感谢帮助。 这是用户控制源:
<asp:UpdatePanel ID="updPanel" runat="server" ChildrenAsTriggers="true">
<ContentTemplate>
<table class="style1">
<tr>
<td align="left" dir="rtl" valign="top" width="250">
<asp:ListBox ID="lst2" runat="server" DataTextField="Title"
DataValueField="Value" Font-Names="Tahoma" Font-Size="Small" Height="306px"
Width="230px"></asp:ListBox>
</td>
<td align="right" dir="rtl" width="30">
<table>
<tr>
<td width="25">
<asp:Button ID="btnAdd" runat="server" meta:resourcekey="btnAddResource1"
Text=">" Width="28px" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnAddAll" runat="server" meta:resourcekey="btnAddAllResource1"
Text=">>" Width="28px" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnRemove" runat="server" meta:resourcekey="btnRemoveResource1"
Text="<" Width="28px" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnRemoveAll" runat="server"
meta:resourcekey="btnRemoveAllResource1" Text="<<" Width="28px" />
</td>
</tr>
</table>
</td>
<td align="left" valign="top" width="250">
<asp:ListBox ID="lst1" runat="server" DataTextField="Title"
DataValueField="value" Font-Names="Tahoma" Font-Size="Small" Height="306px"
Width="230px"></asp:ListBox>
</td>
</tr>
</table>
</ContentTemplate>
这是我背后的代码
Public Property UserName As String
Get
Return ViewState("UserName")
End Get
Set(ByVal value As String)
ViewState("UserName") = value
End Set
End Property
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
MsgBox(lst2.SelectedItem.Text)
End Sub
Public Sub BindUserNotInGroups()
Dim objSecurity As New Negso.Security.SecurityHelper()
Dim dt As New DataTable
dt = objSecurity.GetNonGroupsForUser(UserName)
lst2.DataValueField = dt.Columns("SID").ColumnName
lst2.DataTextField = dt.Columns("Name").ColumnName
lst2.DataSource = dt
lst2.DataBind()
End Sub
''' <summary>
''' Binds the user not in groups.
''' </summary>
''' <remarks></remarks>
Public Sub BindUserInGroups()
Dim objSecurity As New Negso.Security.SecurityHelper()
'Dim k = objSecurity.GetGroupsForUser(UserName).Rows.Count
Dim dt As New DataTable
dt = objSecurity.GetGroupsForUser(UserName)
lst1.DataValueField = dt.Columns("SID").ColumnName
lst1.DataTextField = dt.Columns("Name").ColumnName
lst1.DataSource = dt
lst1.DataBind()
End Sub
我将在设置Username属性时调用绑定函数:
Public Property UserName() As String
Get
Return ViewState("UserName")
End Get
Set(ByVal value As String)
ViewState("UserName") = value
userGroups1.UserName = ViewState("UserName")
userGroups1.BindUserInGroups()
userGroups1.BindUserNotInGroups()
'ListBox3.UserName = ViewState("UserName")
End Set
答案 0 :(得分:1)
基本上这不是最好的方法。你应该像这样工作
public overrides sub onload()
if (not String.isNullOrEmpty(UserName) and not Page.IsPostBack) OR Me.UserNameChanged then
userGroups1.UserName = Me.UserName
userGroups1.BindUserInGroups()
userGroups1.BindUserNotInGroups()
end if
end sub
好的,像这样
Public Class testpage
Inherits System.Web.UI.Page
Private lst2 As New ListBox
Private WithEvents btnadd As New Button
Private _UserNameChanged As Boolean
Public Property UserNameChanged() As Boolean
Get
Return _UserNameChanged
End Get
Set(ByVal value As Boolean)
_UserNameChanged = value
End Set
End Property
Public Property UserName As String
Get
Return ViewState("UserName")
End Get
Set(ByVal value As String)
If ViewState("UserName") <> value Then
UserNameChanged = True
End If
ViewState("UserName") = value
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.Page.IsPostBack And Not String.IsNullOrEmpty(UserName) Then
Me.DataBind(True)
End If
End Sub
Private Sub onbtnClick() Handles btnadd.Click
If Me.UserNameChanged And Not String.IsNullOrEmpty(UserName) Then
Me.DataBind(True)
End If
End Sub
Public Overrides Sub DataBind()
userGroups1.UserName = Me.UserName
userGroups1.BindUserInGroups()
userGroups1.BindUserNotInGroups()
MyBase.DataBind()
End Sub
End Class