Dynamic Listbox control in editable datagrid

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/ASPN­ETControls/DynamicControlsPlac­eholder.aspx


2. http://msdn2.microsoft.com/lib­rary/ms178511(en-us,vs.80).asp­x

3. http://scottonwriting.net/sowb­log/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.DebuggerSt­epThrough()> Private Sub
InitializeComponent()

End Sub

Protected WithEvents lblError As System.Web.UI.WebControls.Labe­l
Protected WithEvents grid As System.Web.UI.WebControls.Data­Grid
Protected WithEvents cListbox As New
System.Web.UI.WebControls.List­Box

'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.Column­s.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.Column­s.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.DeleteUserAffiliateAc­cess(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.SaveUserAffiliateAcce­ss(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.Column­s.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.Column­s.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.BindCountry­List(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.Data­GridSortCommandEventArgs) Handles
grid.SortCommand
Try
Dim adminMgr As IAdmin = Manager.GetUserMgr()
Dim loView As DataView
loView = adminMgr.GetAllUsers().Default­View
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().Default­View
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.List­Box
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.GetUserAffiliateAcces­s(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**************************­*************
 
J

JMann101

I figured it out....woohooo!
By inserting the dynamic control at the ItemDataBound event and storing
the unique id in the viewstate I can retrieve the selected values when
the update event fires by using :

Dim sReturnValues As String =
Request.Form.Get(CType(ViewState("clbCountry"), String))


'*******ITEM BOUND EVENT**************************

Private Sub grid_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
grid.ItemDataBound
If e.Item.ItemType = ListItemType.EditItem Then
Dim cListbox As System.Web.UI.WebControls.ListBox = New
System.Web.UI.WebControls.ListBox
Dim clblUserID As Label = e.Item.FindControl("lblUserID")
Dim userID As Integer = CInt(clblUserID.Text)

cListbox = BindCountryList(userID)

e.Item.Cells(Me.grid.Columns.Count - 1).Controls.AddAt(0,
cListbox)

ViewState("clbCountry") =
e.Item.Cells(Me.grid.Columns.Count - 1).Controls(0).UniqueID
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top