J
JMann101
I am writing a ASP.NET(VB) application and am having some trouble. I
have a datagrid which list users and when an admin clicks "edit" a
defined column becomes visible and a dynamic listbox control is added
to that column/row. When the admin clicks "update" I am unable to
retrieve the values that were selected from that listbox.
Basically, This is a multi-select listbox, so I need to loop to pull
all selected items. This is the only listbox on the page as it is
created dynamically at runtime. However when the page posts back to do
the update the listbox is no longer accessible. Even If I recreate the
listbox by overriding the ViewState on postback the newly selected
items are not preserved.
I have already tried several approaches, listed below are the things I
have tried.
1.
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
2. http://msdn2.microsoft.com/library/ms178511(en-us,vs.80).aspx
3. http://scottonwriting.net/sowblog/posts/3962.aspx
Any help would be greatly appreciated.
Thanks,
Josh
'Code Begins***************************************
Imports ASP.BusinessLayer
Imports ASP.Common
Imports ASP.Common.Helper
Public Class UserAffiliateAccess
Inherits PageBase
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents lblError As System.Web.UI.WebControls.Label
Protected WithEvents grid As System.Web.UI.WebControls.DataGrid
Protected WithEvents cListbox As New
System.Web.UI.WebControls.ListBox
'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Public Sub New()
MyBase.Title = "Manage Users Affiliate Access"
MyBase.CSS = "../styles/main.css"
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsAdmin() Then
Response.Redirect("../default.aspx")
End If
Try
If (Not Me.Page.IsPostBack) Then
Me.lblError.Text = String.Empty
Me.BindGrid()
Me.grid.Columns(Me.grid.Columns.Count - 1).Visible =
False
End If
Catch ex As Exception
Me.lblError.Text = ex.Message
End Try
End Sub
#Region "Grid event handlers"
Protected Sub OnCancelClick(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Try
Me.grid.EditItemIndex = -1
Me.BindGrid()
Me.grid.Columns(Me.grid.Columns.Count - 1).Visible =
False
Me.grid.AllowSorting = True
Me.lblError.Text = ""
Catch ex As Exception
Me.lblError.Text = ex.Message
End Try
End Sub
Protected Sub OnUpdateClick(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Try
Dim adminMgr As IAdmin = Manager.GetUserMgr
Dim ctrlLBL As Label = e.Item.FindControl("lblUserID")
Dim userID As Integer = CInt(ctrlLBL.Text)
If (Not adminMgr.DeleteUserAffiliateAccess(userID)) Then
Throw New Exception("Could not delete existing access
for the selected User.")
Else
Dim lbItem As ListItem
For Each lbItem In cListbox.Items
If lbItem.Selected Then
If (Not
adminMgr.SaveUserAffiliateAccess(userID, CInt(lbItem.Text))) Then
Throw New Exception("Could not save access
for the selected User.")
End If
End If
Next
Me.lblError.Text = "User access has been updated"
End If
Me.grid.EditItemIndex = -1
Me.BindGrid()
Me.grid.Columns(Me.grid.Columns.Count - 1).Visible =
False
Me.grid.AllowSorting = True
Catch ex As Exception
Me.lblError.Text = ex.Message
End Try
End Sub
Protected Sub OnEditClick(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Try
Me.grid.EditItemIndex() = e.Item.ItemIndex
Me.grid.Columns(Me.grid.Columns.Count - 1).Visible = True
Me.BindGrid()
Dim clblUserID As Label = e.Item.FindControl("lblUserID")
Dim userID As Integer = CInt(clblUserID.Text)
Me.grid.Items(e.Item.ItemIndex).Cells( _
Me.grid.Columns.Count -
1).Controls.Add(Me.BindCountryList(userID))
Me.grid.AllowSorting = False
Catch ex As Exception
Me.lblError.Text = ex.Message
End Try
End Sub
Private Sub grid_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
grid.SortCommand
Try
Dim adminMgr As IAdmin = Manager.GetUserMgr()
Dim loView As DataView
loView = adminMgr.GetAllUsers().DefaultView
loView.Sort = e.SortExpression
grid.DataSource = loView
Me.grid.DataBind()
Catch ex As Exception
Me.lblError.Text = ex.Message
End Try
End Sub
#End Region
#Region "Private Methods"
Private Sub BindGrid()
Try
Dim adminMgr As IAdmin = Manager.GetUserMgr()
Dim loView As DataView
loView = adminMgr.GetAllUsers().DefaultView
loView.Sort = "LastName"
grid.DataSource = loView
Me.grid.DataBind()
Catch ex As Exception
Me.lblError.Text = ex.Message
End Try
End Sub
Private Function BindCountryList(ByVal userID As Integer) As
System.Web.UI.WebControls.ListBox
Dim uiMgr As IUI = Manager.GetUIMgr
Dim dt As DataTable = uiMgr.GetAffiliateListing()
Try
cListbox.ID = "lbCountry"
Dim row As DataRow = dt.NewRow()
row("ID") = -1
row("Country") = " --Select Country--"
dt.Rows.InsertAt(row, 0)
dt.DefaultView.Sort = "Country"
cListbox.DataSource = dt.DefaultView
cListbox.DataTextField = "Country"
cListbox.DataValueField = "ID"
cListbox.SelectionMode = ListSelectionMode.Multiple
cListbox.EnableViewState = True
cListbox.DataBind()
Dim adminMgr As IAdmin = Manager.GetUserMgr
Dim dtAccess As DataTable =
adminMgr.GetUserAffiliateAccess(userID)
Dim dr As DataRow
For Each dr In dtAccess.Rows
cListbox.Items.FindByValue(dr.Item("AffiliateID")).Selected = True
Next
Return cListbox
Catch ex As Exception
Throw ex
Finally
dt.Dispose()
End Try
End Function
#End Region
End Class
'Code Ends***************************************
have a datagrid which list users and when an admin clicks "edit" a
defined column becomes visible and a dynamic listbox control is added
to that column/row. When the admin clicks "update" I am unable to
retrieve the values that were selected from that listbox.
Basically, This is a multi-select listbox, so I need to loop to pull
all selected items. This is the only listbox on the page as it is
created dynamically at runtime. However when the page posts back to do
the update the listbox is no longer accessible. Even If I recreate the
listbox by overriding the ViewState on postback the newly selected
items are not preserved.
I have already tried several approaches, listed below are the things I
have tried.
1.
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
2. http://msdn2.microsoft.com/library/ms178511(en-us,vs.80).aspx
3. http://scottonwriting.net/sowblog/posts/3962.aspx
Any help would be greatly appreciated.
Thanks,
Josh
'Code Begins***************************************
Imports ASP.BusinessLayer
Imports ASP.Common
Imports ASP.Common.Helper
Public Class UserAffiliateAccess
Inherits PageBase
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents lblError As System.Web.UI.WebControls.Label
Protected WithEvents grid As System.Web.UI.WebControls.DataGrid
Protected WithEvents cListbox As New
System.Web.UI.WebControls.ListBox
'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Public Sub New()
MyBase.Title = "Manage Users Affiliate Access"
MyBase.CSS = "../styles/main.css"
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsAdmin() Then
Response.Redirect("../default.aspx")
End If
Try
If (Not Me.Page.IsPostBack) Then
Me.lblError.Text = String.Empty
Me.BindGrid()
Me.grid.Columns(Me.grid.Columns.Count - 1).Visible =
False
End If
Catch ex As Exception
Me.lblError.Text = ex.Message
End Try
End Sub
#Region "Grid event handlers"
Protected Sub OnCancelClick(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Try
Me.grid.EditItemIndex = -1
Me.BindGrid()
Me.grid.Columns(Me.grid.Columns.Count - 1).Visible =
False
Me.grid.AllowSorting = True
Me.lblError.Text = ""
Catch ex As Exception
Me.lblError.Text = ex.Message
End Try
End Sub
Protected Sub OnUpdateClick(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Try
Dim adminMgr As IAdmin = Manager.GetUserMgr
Dim ctrlLBL As Label = e.Item.FindControl("lblUserID")
Dim userID As Integer = CInt(ctrlLBL.Text)
If (Not adminMgr.DeleteUserAffiliateAccess(userID)) Then
Throw New Exception("Could not delete existing access
for the selected User.")
Else
Dim lbItem As ListItem
For Each lbItem In cListbox.Items
If lbItem.Selected Then
If (Not
adminMgr.SaveUserAffiliateAccess(userID, CInt(lbItem.Text))) Then
Throw New Exception("Could not save access
for the selected User.")
End If
End If
Next
Me.lblError.Text = "User access has been updated"
End If
Me.grid.EditItemIndex = -1
Me.BindGrid()
Me.grid.Columns(Me.grid.Columns.Count - 1).Visible =
False
Me.grid.AllowSorting = True
Catch ex As Exception
Me.lblError.Text = ex.Message
End Try
End Sub
Protected Sub OnEditClick(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Try
Me.grid.EditItemIndex() = e.Item.ItemIndex
Me.grid.Columns(Me.grid.Columns.Count - 1).Visible = True
Me.BindGrid()
Dim clblUserID As Label = e.Item.FindControl("lblUserID")
Dim userID As Integer = CInt(clblUserID.Text)
Me.grid.Items(e.Item.ItemIndex).Cells( _
Me.grid.Columns.Count -
1).Controls.Add(Me.BindCountryList(userID))
Me.grid.AllowSorting = False
Catch ex As Exception
Me.lblError.Text = ex.Message
End Try
End Sub
Private Sub grid_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
grid.SortCommand
Try
Dim adminMgr As IAdmin = Manager.GetUserMgr()
Dim loView As DataView
loView = adminMgr.GetAllUsers().DefaultView
loView.Sort = e.SortExpression
grid.DataSource = loView
Me.grid.DataBind()
Catch ex As Exception
Me.lblError.Text = ex.Message
End Try
End Sub
#End Region
#Region "Private Methods"
Private Sub BindGrid()
Try
Dim adminMgr As IAdmin = Manager.GetUserMgr()
Dim loView As DataView
loView = adminMgr.GetAllUsers().DefaultView
loView.Sort = "LastName"
grid.DataSource = loView
Me.grid.DataBind()
Catch ex As Exception
Me.lblError.Text = ex.Message
End Try
End Sub
Private Function BindCountryList(ByVal userID As Integer) As
System.Web.UI.WebControls.ListBox
Dim uiMgr As IUI = Manager.GetUIMgr
Dim dt As DataTable = uiMgr.GetAffiliateListing()
Try
cListbox.ID = "lbCountry"
Dim row As DataRow = dt.NewRow()
row("ID") = -1
row("Country") = " --Select Country--"
dt.Rows.InsertAt(row, 0)
dt.DefaultView.Sort = "Country"
cListbox.DataSource = dt.DefaultView
cListbox.DataTextField = "Country"
cListbox.DataValueField = "ID"
cListbox.SelectionMode = ListSelectionMode.Multiple
cListbox.EnableViewState = True
cListbox.DataBind()
Dim adminMgr As IAdmin = Manager.GetUserMgr
Dim dtAccess As DataTable =
adminMgr.GetUserAffiliateAccess(userID)
Dim dr As DataRow
For Each dr In dtAccess.Rows
cListbox.Items.FindByValue(dr.Item("AffiliateID")).Selected = True
Next
Return cListbox
Catch ex As Exception
Throw ex
Finally
dt.Dispose()
End Try
End Function
#End Region
End Class
'Code Ends***************************************