473,395 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Dynamic Listbox control in editable datagrid

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*ETCon...c*eholder.aspx
2. http://msdn2.microsoft.com/lib*rary/...s,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*GridSortCommandEven tArgs) 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****************************************

Nov 19 '05 #1
1 2867
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

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ericm1155 | last post by:
I am trying to display some information from a database in a form that displays one record per line. When the user clicks anywhere on a line, that record is highlighted and selected, and my program...
0
by: Alex | last post by:
Interested in more .NET stuff visit www.dedicatedsolutions.co.uk The DataList is not as powerful as the DataGrid. It requires more work from you since it has no default data presentation format....
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
5
by: Lie | last post by:
Hi all, I have problem in getting selectedindex of multiple listbox selection in a datagrid. I have a listbox with multiple selection mode inside datagrid. In Edit mode, I need to get back all...
1
by: Gunjan Garg | last post by:
Hello All, I am working to create a generic datagrid which accepts a datasource(ListData - This is our own datatype) and depending on the calling program customizes itself for sorting,...
8
by: George Meng | last post by:
I got a tough question: The backgroud for this question is: I want to design an application works like a engine. After release, we can still customize a form by adding a button, and source code...
3
by: ER | last post by:
Hi, I would like to create a datagrid or free entry form with dynamic columns based on the user selection, do you have any ideas or samples? Any help would be appreciated! ER
0
by: cindy | last post by:
I have a dynamic datagrid. I have custom classes for the controls public class CreateEditItemTemplateDDL : ITemplate { DataTable dtBind; string strddlName; string strSelectedID; string...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.