473,725 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.BusinessLay er
Imports ASP.Common
Imports ASP.Common.Help er

Public Class UserAffiliateAc cess
Inherits PageBase

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt *epThrough()> Private Sub
InitializeCompo nent()

End Sub

Protected WithEvents lblError As System.Web.UI.W ebControls.Labe *l
Protected WithEvents grid As System.Web.UI.W ebControls.Data *Grid
Protected WithEvents cListbox As New
System.Web.UI.W ebControls.List *Box

'NOTE: The following placeholder declaration is required by the Web

Form Designer.
'Do not delete or move it.
Private designerPlaceho lderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.
InitializeCompo nent()
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.EventArg s) Handles MyBase.Load

If Not IsAdmin() Then
Response.Redire ct("../default.*aspx")
End If

Try
If (Not Me.Page.IsPostB ack) Then

Me.lblError.Tex t = String.Empty

Me.BindGrid()

Me.grid.Columns (Me.grid.Column *s.Count - 1).Visible =
False

End If
Catch ex As Exception
Me.lblError.Tex t = ex.Message
End Try
End Sub

#Region "Grid event handlers"

Protected Sub OnCancelClick(B yVal sender As Object, ByVal e As
DataGridCommand EventArgs)

Try
Me.grid.EditIte mIndex = -1
Me.BindGrid()
Me.grid.Columns (Me.grid.Column *s.Count - 1).Visible =
False
Me.grid.AllowSo rting = True
Me.lblError.Tex t = ""

Catch ex As Exception
Me.lblError.Tex t = ex.Message
End Try
End Sub

Protected Sub OnUpdateClick(B yVal sender As Object, ByVal e As
DataGridCommand EventArgs)
Try

Dim adminMgr As IAdmin = Manager.GetUser Mgr
Dim ctrlLBL As Label = e.Item.FindCont rol("lblUserID" *)
Dim userID As Integer = CInt(ctrlLBL.Te xt)

If (Not adminMgr.Delete UserAffiliateAc *cess(userID)) Then
Throw New Exception("Coul d 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.SaveUs erAffiliateAcce *ss(userID, CInt(lbItem.Tex t))) Then
Throw New Exception("Coul d not save access
for the selected User.")
End If
End If
Next

Me.lblError.Tex t = "User access has been updated"
End If

Me.grid.EditIte mIndex = -1
Me.BindGrid()

Me.grid.Columns (Me.grid.Column *s.Count - 1).Visible =
False
Me.grid.AllowSo rting = True

Catch ex As Exception
Me.lblError.Tex t = ex.Message
End Try
End Sub

Protected Sub OnEditClick(ByV al sender As Object, ByVal e As
DataGridCommand EventArgs)
Try
Me.grid.EditIte mIndex() = e.Item.ItemInde x

Me.grid.Columns (Me.grid.Column *s.Count - 1).Visible = True
Me.BindGrid()

Dim clblUserID As Label = e.Item.FindCont rol("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.AllowSo rting = False
Catch ex As Exception
Me.lblError.Tex t = ex.Message
End Try

End Sub

Private Sub grid_SortComman d(ByVal source As Object, ByVal e As
System.Web.UI.W ebControls.Data *GridSortComman dEventArgs) Handles
grid.SortComman d
Try
Dim adminMgr As IAdmin = Manager.GetUser Mgr()
Dim loView As DataView
loView = adminMgr.GetAll Users().Default *View
loView.Sort = e.SortExpressio n

grid.DataSource = loView

Me.grid.DataBin d()

Catch ex As Exception
Me.lblError.Tex t = ex.Message
End Try
End Sub

#End Region
#Region "Private Methods"

Private Sub BindGrid()
Try
Dim adminMgr As IAdmin = Manager.GetUser Mgr()
Dim loView As DataView
loView = adminMgr.GetAll Users().Default *View
loView.Sort = "LastName"

grid.DataSource = loView

Me.grid.DataBin d()

Catch ex As Exception
Me.lblError.Tex t = ex.Message
End Try
End Sub

Private Function BindCountryList (ByVal userID As Integer) As
System.Web.UI.W ebControls.List *Box
Dim uiMgr As IUI = Manager.GetUIMg r
Dim dt As DataTable = uiMgr.GetAffili ateListing()

Try
cListbox.ID = "lbCountry"

Dim row As DataRow = dt.NewRow()
row("ID") = -1
row("Country") = " --Select Country--"
dt.Rows.InsertA t(row, 0)

dt.DefaultView. Sort = "Country"
cListbox.DataSo urce = dt.DefaultView
cListbox.DataTe xtField = "Country"
cListbox.DataVa lueField = "ID"

cListbox.Select ionMode = ListSelectionMo de.Multiple
cListbox.Enable ViewState = True

cListbox.DataBi nd()

Dim adminMgr As IAdmin = Manager.GetUser Mgr
Dim dtAccess As DataTable =
adminMgr.GetUse rAffiliateAcces *s(userID)
Dim dr As DataRow
For Each dr In dtAccess.Rows

cListbox.Items. FindByValue(dr. *Item("Affiliat eID")).Selecte d = 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 2883
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.Ge t(CType(ViewSta te("clbCountry" ), String))
'*******ITEM BOUND EVENT********** *************** *

Private Sub grid_ItemDataBo und(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Data GridItemEventAr gs) Handles
grid.ItemDataBo und
If e.Item.ItemType = ListItemType.Ed itItem Then
Dim cListbox As System.Web.UI.W ebControls.List Box = New
System.Web.UI.W ebControls.List Box
Dim clblUserID As Label = e.Item.FindCont rol("lblUserID" )
Dim userID As Integer = CInt(clblUserID .Text)

cListbox = BindCountryList (userID)

e.Item.Cells(Me .grid.Columns.C ount - 1).Controls.Add At(0,
cListbox)

ViewState("clbC ountry") =
e.Item.Cells(Me .grid.Columns.C ount - 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
2214
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 will respond to that. The problem is that with a listbox control (which is what it sounds like I need), I can't get the information to format at all. \t-Tabs skew the data unpredicatably - it leaves some words closer together on some lines than...
0
3112
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. However, the DataGrid begins to get very cumbersome as the number of columns of data you present increases. Anything more than half a dozen columns or so and you probably induce horizontal scrolling - a real no-no for me. If you put such a...
1
7583
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 dropdown in UC1 _________________________ 1) MainPage_Load 2) User Control_1 Load
6
2876
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 a denormalized mirror of the database, but I'm not having much luck getting the selection logic down (I haven't found a 'hook' where I can access the listbox object as an object to set the listitem's selected property before it gets rendered).. ...
5
3529
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 selected items of that listbox and display it. can anyone help? Thanks
1
2691
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, paginantion or accepting the add and remove item events. What i am observing is that none of the vents are happening... (Sort, page, or item). I am sure I am missing something basic here... Need help... Thanks much
8
2016
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 for the button. (This is done by the form itself, not by using VS.Net) (button and source code should be a record in database, these information should be retrieve from database when the form shows up) What I want is:
3
1384
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
2909
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 strDataValueField; string strDataTextField; public CreateEditItemTemplateDDL(string DDLName,string DataValueField,string
0
8748
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9248
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9092
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6695
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4506
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4775
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2151
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.