Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataTable / ListBox problem

Status
Not open for further replies.

oddball

Technical User
Mar 17, 2000
64
GB
Right this is really begining to do my head in now. Help would be really appreciated :)

There is a screenshot at this URL of the control i'm trying to get working:


What I'm trying to achieve is a page that retrieves all the values from a ProductDefinition table into a datatable where its filtered for only those selected in the ProductCategory DropDownList and bound to the ProductDefinition ListBox (right).

When a user changes the selection in the category DropDownList the values in the ProductDefinition ListBox update.

When a user selects a product from this ListBox and clicks the Add button the selection is added to the CompanyProducts ListBox (left). Conversely if they click the Remove button the Selected Value in the ComapnyProducts ListBox is removed.

The way I was trying to achieve this was by Creating a new DataTable on PageLoad and Bind it to the CompanyProducts ListBox. When Add is clicked a new row is created in this DataTable and populated with the values selected in the ProductDefinition ListBox. Values would be removed from the CompanyProducts ListBox by removing the row from the DataTable.

I've tried so many methods and just can't get it working!

I think the biggest problems i'm getting are:

- Referencing the new datatable (created by the NewDataTable Function) from the AddBtn_Click Subroutine

- Getting the right hand ListBox to update properly

- Getting the AddBtn to work at all


Perhaps my whole approach is wrong - any advice is gladly accepted cos i've got a deadline on Tuesday.

Anyway heres my (bug ridden) code:

==========================================================

<%@ Page language=&quot;VB&quot; Debug=&quot;true&quot;%>
<%@ Import Namespace=&quot;System.Web.Mail&quot; %>
<%@ Import Namespace=&quot;System.Data&quot; %>


<%@ Register TagPrefix=&quot;portal&quot; TagName=&quot;Banner&quot; Src=&quot;~/DesktopPortalBanner.ascx&quot; %>
<script runat=&quot;server&quot;>

Function ProdDefList() As ICollection

Dim products As New ASPNetPortal.ProductsDB()
Dim myDataSet = products.GetAllProductDefinitions()
Dim myDataView As New DataView(myDataSet.Tables(&quot;tblProductDefinitions&quot;))
Dim strCategoryID = CategoryList.SelectedItem.Value

' Filter the DataView for only rows of the selected Category
myDataView.RowFilter = &quot;CategoryID = &quot; & strCategoryID

Return myDataView
End Function


Function NewDataTable() As ICollection

' Create a new DataTable in the existing DataSet for the Companies
' Selected Products

Dim ds As New DataSet
Dim dt As New DataTable

dt = ds.Tables(&quot;CompanyProducts&quot;)
dt.Columns.Add(New DataColumn(&quot;ProductDefinitionID&quot;, GetType(Int32)))
dt.Columns.Add(New DataColumn(&quot;Description&quot;, GetType(String)))
Dim myDataView = dt.DefaultView

'Return myDataView
End Function


Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)

If Not IsPostBack Then

Email.Text = Request.QueryString(&quot;email&quot;)

' Specify the DataView as the Data Source of the ProductList ListBox
' and Bind it
ProductDefinitionList.DataSource = ProdDefList()
ProductDefinitionList.DataValueField = &quot;ProductDefinitionID&quot;
ProductDefinitionList.DataTextField = &quot;Description&quot;
ProductDefinitionList.DataBind()

'Bind the CompanyProducts ListBox to the new DataTable
CompanyProducts.DataSource = NewDataTable()
CompanyProducts.DataValueField = &quot;ProductDefinitionID&quot;
CompanyProducts.DataTextField = &quot;Description&quot;
CompanyProducts.DataBind()

End If

End Sub

Sub AddBtn_Click(ByVal sender As Object, ByVal E As EventArgs)

Dim dt As New DataTable


' ??
' ??
' ??


Dim dr As DataRow

dr = dt.NewRow()
dr(&quot;ProductDefinitionID&quot;) = ProductDefinitionList.SelectedItem.Value
dr(&quot;Description&quot;) = ProductDefinitionList.SelectedItem.Text
dt.Rows.Add(dr)

End Sub

Sub RemoveBtn_Click(ByVal sender As Object, ByVal E As EventArgs)

' ??
' ??
' ??

End Sub

Sub RegisterBtn_Click(ByVal sender As Object, ByVal E As EventArgs)

' Set the user's authentication name to the userId
FormsAuthentication.SetAuthCookie(Email.Text, False)

' Redirect browser back to home page'
Response.Redirect(&quot;~/DesktopDefault.aspx&quot;)

End Sub

</script>


<script language=&quot;JavaScript&quot;>
<!--

function openWindow(filename) {

window.open (filename, &quot;newwindow&quot;, &quot;height=495, width=625, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no&quot;)

}
//-->
</script>

<html>
<head>
<link rel=&quot;stylesheet&quot; href='<%= Request.ApplicationPath & &quot;/Portal.css&quot; %>' type=&quot;text/css&quot; />
</head>
<body leftmargin=&quot;0&quot; bottommargin=&quot;0&quot; rightmargin=&quot;0&quot; topmargin=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot;>
<form method=&quot;post&quot; id=&quot;frmSubscribe&quot; runat=&quot;server&quot;>
<table width=&quot;100%&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;>
<tr valign=&quot;top&quot;>
<td colspan=&quot;2&quot;>
<portal:Banner ShowTabs=&quot;false&quot; runat=&quot;server&quot; id=&quot;Banner1&quot; />
</td>
</tr>
<tr>
<td>
<br>
<table width=&quot;98%&quot; cellspacing=&quot;0&quot; cellpadding=&quot;4&quot; border=&quot;0&quot;>
<tr>
<td width=&quot;90&quot;>&nbsp;

</td>
<td width=&quot;*&quot;>
<table cellpadding=&quot;2&quot; cellspacing=&quot;1&quot; border=&quot;0&quot; width=&quot;600&quot;>
<tr>
<td>
<span class=&quot;Head&quot;>Company/Organistion Information </span>
</td>
</tr>
<tr>
<td>
<hr noshade size=&quot;1&quot;>
</td>
</tr>
<tr>
<td>
<asp:TextBox Visible=&quot;False&quot; id=&quot;Email&quot; runat=&quot;server&quot; />
</td>
</tr>
<tr>
<td>
<table width=&quot;100%&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot;>
<tr>
<td width=&quot;50%&quot;>
<span class=&quot;ItemTitle&quot;>Product Details</span>
<br>
<br>
</td>
<td align=&quot;right&quot; valign=&quot;top&quot; ><a href=&quot;javascript:eek:penWindow('InfoProductDetails.aspx')&quot;><img src=&quot;../images/info.gif&quot; width=&quot;35&quot; height=&quot;16&quot; border=&quot;0&quot;></a></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td class=&quot;normal&quot;>Category</td>
<td>&nbsp;</td>
<td class=&quot;normal&quot;>Product Categories</td>
</tr>
<tr>
<td>
<asp:TextBox id=&quot;ProductCategory&quot; runat=&quot;server&quot; /><br>
</td>
<td>&nbsp;</td>
<td>
<asp:DropDownList id=&quot;CategoryList&quot; AutoPostBack=&quot;True&quot; runat=&quot;server&quot; CssClass=&quot;Normal&quot;>
<asp:ListItem Value=&quot;1&quot;>Exterior</asp:ListItem>
<asp:ListItem Value=&quot;2&quot;>Chassis/Underbody</asp:ListItem>
<asp:ListItem Value=&quot;3&quot;>Powertrain</asp:ListItem>
<asp:ListItem Value=&quot;4&quot;>Interior</asp:ListItem>
<asp:ListItem Value=&quot;5&quot;>Electrical/Electronic</asp:ListItem>
<asp:ListItem Value=&quot;6&quot;>Other</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class=&quot;normal&quot;>Company Products</td>
<td>&nbsp;</td>
<td class=&quot;normal&quot;>Products</td>
</tr>
<tr>
<td>
<asp:ListBox id=&quot;CompanyProducts&quot; width=&quot;250&quot; Rows=&quot;10&quot; runat=&quot;server&quot; CssClass=&quot;Normal&quot; />
</td>
<td width=&quot;45&quot; align=&quot;center&quot;>
<asp:Button id=&quot;btnAdd&quot; runat=&quot;server&quot; Text=&quot; << &quot; Onclick=&quot;AddBtn_Click&quot; CssClass=&quot;Normal&quot; />
<br><br>
<asp:Button id=&quot;btnRemove&quot; runat=&quot;server&quot; Text=&quot; >> &quot; Onclick=&quot;RemoveBtn_Click&quot; CssClass=&quot;Normal&quot; />
</td>
<td>
<asp:ListBox id=&quot;ProductDefinitionList&quot; width=&quot;250&quot; Rows=&quot;10&quot; runat=&quot;server&quot; Cssclass=&quot;Normal&quot; />
</td>
</tr>
</table>
<br>
<hr noshade size=&quot;1&quot;>
</td>
</tr>
<tr valign=&quot;top&quot;>
<td>
<p>
<asp:LinkButton CssClass=&quot;CommandButton&quot; OnClick=&quot;RegisterBtn_Click&quot; Text=&quot;Finish&quot; runat=&quot;server&quot; ID=&quot;Linkbutton1&quot; NAME=&quot;Linkbutton1&quot; />
<br>
<br>
<p>
<asp:Label id=&quot;Message&quot; CssClass=&quot;NormalRed&quot; runat=&quot;server&quot; /></p>
</td>
</tr>
</table>
<P>&nbsp;</P>
<P>&nbsp;</P>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</HTML>

===========================================================

cheers,

si
 
Hey oddball,

yes, this is doable. we actually just implemented a screen very much like yours.

The first thing I'd suggest: Ditch the datatable on page load. there's no real reason for it. You're better off just taking the selected items from the products side and just doing a lstCompanyProducts.Items.Add(). that way, you never have to play with another object (the datatable), and you'll have less problems with the whole binding thing.

Can you post what problems you've been having with it specifically?

i.e.
- You mention having problems updating hte right hand listbox. Is that problem from updating it when the drop down list box's index is changed, or when the user clicks remove and you want to add an item back in?

- You mentioned the Add button wasn't working. Whats it doing?

Jack
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top