473,548 Members | 2,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Populating Mulitiselect Listbox

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)..

Before denormalizing the data in hopes of simplifying the binding, I thought
I would ask if I was missing an obviously simple approach to doing what I am
after (and leaving the dataset denormalized).

Thanks
Nov 18 '05 #1
6 2861

I think you should be able to do this with a single table; have you tried something like:

ListBox lb = new ListBox();
lb.SelectionMod e = ListSelectionMo de.Multiple;
lb.DataSource = whatever;
lb.DataBind();
// now set from the list box
foreach (ListItem item in lb.Items)
{
if (IsItemSelected (item.Value)) // IsItemSelected is your method
{
item.Selected = true;
}
}

// or do it from the data side
foreach (DataRow row in whatever)
{
// blah, whatever you have to determine what's selected.
if (row["IsSelected "] == "True")
{
lb.Items.FindBy Value(row["Value"].ToString()).Se lected = true;
}
}

Scott
"Chris Leuty" <ch*********@no spam.briniker.c om> wrote in message news:OY******** ******@tk2msftn gp13.phx.gbl...
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)..

Before denormalizing the data in hopes of simplifying the binding, I thought
I would ask if I was missing an obviously simple approach to doing what I am
after (and leaving the dataset denormalized).

Thanks
Nov 18 '05 #2
The listbox is currently in the aspx, bound like this:
<TD vAlign="top" align="right">
<asp:label id="Label13" runat="server" CssClass="form-label">Trades</asp:label>
</TD>
<TD>
<asp:listbox id="lstTrades" runat="server" CssClass="form-text" Width="154px" SelectionMode=" Multiple" DataSource='<%# ContactInfo1 %>' DataMember="Tra des" DataTextField=" TradeName" DataValueField= "TradeID">
</asp:listbox>
</TD>
I have code similar to your examples in the codebehind, but I'm having a hard time finding a place where the Listbox is available as an object (or perhaps my code is fubar). I'm trying to get the listbox reference for the asp:listbox item above by doing the following (in this case, I was doing it during the event that bound the data. e is a DataListCommand EventArgs):
lstTrades = CType(e.Item.Fi ndControl("lstT rades"), ListBox)
I think once I get to that point, the actual selection based on the data will be easy. So far, I haven't found a good place to hook this line (I keep getting back Nothing).
"Scott G." <no*****@this-is-extra-hotmail.com> wrote in message news:OE******** ******@tk2msftn gp13.phx.gbl...

I think you should be able to do this with a single table; have you tried something like:

ListBox lb = new ListBox();
lb.SelectionMod e = ListSelectionMo de.Multiple;
lb.DataSource = whatever;
lb.DataBind();
// now set from the list box
foreach (ListItem item in lb.Items)
{
if (IsItemSelected (item.Value)) // IsItemSelected is your method
{
item.Selected = true;
}
}

// or do it from the data side
foreach (DataRow row in whatever)
{
// blah, whatever you have to determine what's selected.
if (row["IsSelected "] == "True")
{
lb.Items.FindBy Value(row["Value"].ToString()).Se lected = true;
}
}

Scott
"Chris Leuty" <ch*********@no spam.briniker.c om> wrote in message news:OY******** ******@tk2msftn gp13.phx.gbl...
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)..

Before denormalizing the data in hopes of simplifying the binding, I thought
I would ask if I was missing an obviously simple approach to doing what I am
after (and leaving the dataset denormalized).

Thanks
Nov 18 '05 #3

A good spot is probably Page_Load; if you are using the default code that VS.NET generates then this method should be there.... again, if you are using VS.NET there should already be a member field call "lstTrades" as a protected member of the code behind for the ASPX page.

If you want the ListBox during a ListBox DataBinding event; then just CType the sender (the first parent) to the event handler; In C# that might look like (for the ListBox DataBindingEven t):

private void lstTrades_DataB inding(object sender, EventArgs e)
{
ListBox lb = (ListBox)sender ;
}

I'm a bit confused if you if you are trying to bind the data to the ListBox in an event for the ListBox; if that's what you are asking, then I think you'd be better off binding the data to the ListBox in the containing page.

Scott
"Chris Leuty" <chris.leuty-at-nospam.brinker. com> wrote in message news:%2******** *******@TK2MSFT NGP09.phx.gbl.. .
The listbox is currently in the aspx, bound like this:
<TD vAlign="top" align="right">
<asp:label id="Label13" runat="server" CssClass="form-label">Trades</asp:label>
</TD>
<TD>
<asp:listbox id="lstTrades" runat="server" CssClass="form-text" Width="154px" SelectionMode=" Multiple" DataSource='<%# ContactInfo1 %>' DataMember="Tra des" DataTextField=" TradeName" DataValueField= "TradeID">
</asp:listbox>
</TD>
I have code similar to your examples in the codebehind, but I'm having a hard time finding a place where the Listbox is available as an object (or perhaps my code is fubar). I'm trying to get the listbox reference for the asp:listbox item above by doing the following (in this case, I was doing it during the event that bound the data. e is a DataListCommand EventArgs):
lstTrades = CType(e.Item.Fi ndControl("lstT rades"), ListBox)
I think once I get to that point, the actual selection based on the data will be easy. So far, I haven't found a good place to hook this line (I keep getting back Nothing).
"Scott G." <no*****@this-is-extra-hotmail.com> wrote in message news:OE******** ******@tk2msftn gp13.phx.gbl...

I think you should be able to do this with a single table; have you tried something like:

ListBox lb = new ListBox();
lb.SelectionMod e = ListSelectionMo de.Multiple;
lb.DataSource = whatever;
lb.DataBind();
// now set from the list box
foreach (ListItem item in lb.Items)
{
if (IsItemSelected (item.Value)) // IsItemSelected is your method
{
item.Selected = true;
}
}

// or do it from the data side
foreach (DataRow row in whatever)
{
// blah, whatever you have to determine what's selected.
if (row["IsSelected "] == "True")
{
lb.Items.FindBy Value(row["Value"].ToString()).Se lected = true;
}
}

Scott
"Chris Leuty" <ch*********@no spam.briniker.c om> wrote in message news:OY******** ******@tk2msftn gp13.phx.gbl...
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)..

Before denormalizing the data in hopes of simplifying the binding, I thought
I would ask if I was missing an obviously simple approach to doing what I am
after (and leaving the dataset denormalized).

Thanks
Nov 18 '05 #4
I did bind the list data in the asp:listbox. What I'm trying to do now is mark certain items as selected, which is found in a different table. To be specific, the listbox contains trades (Plumbing, Electrical, Masonry, etc), and a second table contains the trades that apply to the displayed form (i.e. this vendor does this sort of work). This listbox is part of an Edit Template on a DataList (so that the selections can be changed).

I chose DataBinding() only as an example of where I have tried to get a reference to the listbox (it didn't work). I had to define the listbox in the code (VS.NET did not create a reference to it; I figured it was because it was part of the Edit Template, which is late bound), so I felt that I needed to find a place in the process where the listbox was created, but not yet sent back to the browser. The point where the data is bound seemed to make the most sense to me, but I can't grab a reference to it.

Thanks for your help so far.
"Scott G." <no*****@this-is-extra-hotmail.com> wrote in message news:eO******** ******@TK2MSFTN GP10.phx.gbl...

A good spot is probably Page_Load; if you are using the default code that VS.NET generates then this method should be there.... again, if you are using VS.NET there should already be a member field call "lstTrades" as a protected member of the code behind for the ASPX page.

If you want the ListBox during a ListBox DataBinding event; then just CType the sender (the first parent) to the event handler; In C# that might look like (for the ListBox DataBindingEven t):

private void lstTrades_DataB inding(object sender, EventArgs e)
{
ListBox lb = (ListBox)sender ;
}

I'm a bit confused if you if you are trying to bind the data to the ListBox in an event for the ListBox; if that's what you are asking, then I think you'd be better off binding the data to the ListBox in the containing page.

Scott
"Chris Leuty" <chris.leuty-at-nospam.brinker. com> wrote in message news:%2******** *******@TK2MSFT NGP09.phx.gbl.. .
The listbox is currently in the aspx, bound like this:
<TD vAlign="top" align="right">
<asp:label id="Label13" runat="server" CssClass="form-label">Trades</asp:label>
</TD>
<TD>
<asp:listbox id="lstTrades" runat="server" CssClass="form-text" Width="154px" SelectionMode=" Multiple" DataSource='<%# ContactInfo1 %>' DataMember="Tra des" DataTextField=" TradeName" DataValueField= "TradeID">
</asp:listbox>
</TD>
I have code similar to your examples in the codebehind, but I'm having a hard time finding a place where the Listbox is available as an object (or perhaps my code is fubar). I'm trying to get the listbox reference for the asp:listbox item above by doing the following (in this case, I was doing it during the event that bound the data. e is a DataListCommand EventArgs):
lstTrades = CType(e.Item.Fi ndControl("lstT rades"), ListBox)
I think once I get to that point, the actual selection based on the data will be easy. So far, I haven't found a good place to hook this line (I keep getting back Nothing).
"Scott G." <no*****@this-is-extra-hotmail.com> wrote in message news:OE******** ******@tk2msftn gp13.phx.gbl...

I think you should be able to do this with a single table; have you tried something like:

ListBox lb = new ListBox();
lb.SelectionMod e = ListSelectionMo de.Multiple;
lb.DataSource = whatever;
lb.DataBind();
// now set from the list box
foreach (ListItem item in lb.Items)
{
if (IsItemSelected (item.Value)) // IsItemSelected is your method
{
item.Selected = true;
}
}

// or do it from the data side
foreach (DataRow row in whatever)
{
// blah, whatever you have to determine what's selected.
if (row["IsSelected "] == "True")
{
lb.Items.FindBy Value(row["Value"].ToString()).Se lected = true;
}
}

Scott
"Chris Leuty" <ch*********@no spam.briniker.c om> wrote in message news:OY******** ******@tk2msftn gp13.phx.gbl...
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)..

Before denormalizing the data in hopes of simplifying the binding, I thought
I would ask if I was missing an obviously simple approach to doing what I am
after (and leaving the dataset denormalized).

Thanks
Nov 18 '05 #5

Got it; here's an example in C# that gets you to the control; in the DG_EditCommand handler, you'll see that I grab the index of the item and then grab the item to get the ListBox1; now this seems to be the same as the code you posted in a message or two ago. If you run this page as a test you should see the "found control" message.

Scott
<%@ Page language="c#" AutoEventWireup ="false" Trace="true" %>
<%@ Import Namespace="Syst em.Data" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>DGBind </title>
<script language="C#" runat="server">
protected override void OnLoad(EventArg s e)
{
this.DG.DataSou rce = MyFakeDataSet;
this.DG.DataMem ber = "Table1";
this.DG.EditCom mand += new DataGridCommand EventHandler(DG _EditCommand);
this.DG.CancelC ommand += new DataGridCommand EventHandler(DG _CancelCommand) ;
if (!this.IsPostBa ck)
{
this.DG.DataBin d();
}
}

private void DG_EditCommand( object source, DataGridCommand EventArgs e)
{
this.DG.EditIte mIndex = e.Item.ItemInde x;
this.DG.DataBin d();
ListBox lb = (ListBox)this.D G.Items[e.Item.ItemInde x].FindControl("L istBox1");
if (lb != null)
{
this.Trace.Warn ("found control");
}
}

private void DG_CancelComman d(object source, DataGridCommand EventArgs e)
{
this.DG.EditIte mIndex = -1;
this.DG.DataBin d();
}

private DataSet m_ds;
private DataSet MyFakeDataSet {
get {
if (m_ds != null) {
return m_ds;
}
m_ds = new DataSet();
DataTable t = new DataTable("Tabl e1");
t.Columns.Add(" Column1");
m_ds.Tables.Add (t);

for (int i = 0; i < 10; i++) {
DataRow r = m_ds.Tables[0].NewRow();
r["Column1"] = i.ToString();
m_ds.Tables[0].Rows.Add(r);
}
return m_ds;
}
}
</script>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DG" runat="server" autogeneratecol umns="False">
<columns>
<asp:editcomman dcolumn buttontype="Lin kButton" updatetext="Upd ate" headertext="Col umn1" canceltext="Can cel"
edittext="Edit" ></asp:editcommand column>
<asp:templateco lumn headertext="Col umn1">
<itemtemplate >
<asp:Label id="Label1" runat="server" Text='<%# DataBinder.Eval (Container, "DataItem.Colum n1") %>'>
</asp:label>
</itemtemplate>
<edititemtempla te>
<asp:listbox id="ListBox1" runat="server"> </asp:listbox>
</edititemtemplat e>
</asp:templatecol umn>
</columns>
</asp:datagrid>
</form>
</body>
</html>

"Chris Leuty" <chris.leuty-at-nospam.brinker. com> wrote in message news:eu******** *****@TK2MSFTNG P11.phx.gbl...
I did bind the list data in the asp:listbox. What I'm trying to do now is mark certain items as selected, which is found in a different table. To be specific, the listbox contains trades (Plumbing, Electrical, Masonry, etc), and a second table contains the trades that apply to the displayed form (i.e. this vendor does this sort of work). This listbox is part of an Edit Template on a DataList (so that the selections can be changed).

I chose DataBinding() only as an example of where I have tried to get a reference to the listbox (it didn't work). I had to define the listbox in the code (VS.NET did not create a reference to it; I figured it was because it was part of the Edit Template, which is late bound), so I felt that I needed to find a place in the process where the listbox was created, but not yet sent back to the browser. The point where the data is bound seemed to make the most sense to me, but I can't grab a reference to it.

Thanks for your help so far.
"Scott G." <no*****@this-is-extra-hotmail.com> wrote in message news:eO******** ******@TK2MSFTN GP10.phx.gbl...

A good spot is probably Page_Load; if you are using the default code that VS.NET generates then this method should be there.... again, if you are using VS.NET there should already be a member field call "lstTrades" as a protected member of the code behind for the ASPX page.

If you want the ListBox during a ListBox DataBinding event; then just CType the sender (the first parent) to the event handler; In C# that might look like (for the ListBox DataBindingEven t):

private void lstTrades_DataB inding(object sender, EventArgs e)
{
ListBox lb = (ListBox)sender ;
}

I'm a bit confused if you if you are trying to bind the data to the ListBox in an event for the ListBox; if that's what you are asking, then I think you'd be better off binding the data to the ListBox in the containing page.

Scott
"Chris Leuty" <chris.leuty-at-nospam.brinker. com> wrote in message news:%2******** *******@TK2MSFT NGP09.phx.gbl.. .
The listbox is currently in the aspx, bound like this:
<TD vAlign="top" align="right">
<asp:label id="Label13" runat="server" CssClass="form-label">Trades</asp:label>
</TD>
<TD>
<asp:listbox id="lstTrades" runat="server" CssClass="form-text" Width="154px" SelectionMode=" Multiple" DataSource='<%# ContactInfo1 %>' DataMember="Tra des" DataTextField=" TradeName" DataValueField= "TradeID">
</asp:listbox>
</TD>
I have code similar to your examples in the codebehind, but I'm having a hard time finding a place where the Listbox is available as an object (or perhaps my code is fubar). I'm trying to get the listbox reference for the asp:listbox item above by doing the following (in this case, I was doing it during the event that bound the data. e is a DataListCommand EventArgs):
lstTrades = CType(e.Item.Fi ndControl("lstT rades"), ListBox)
I think once I get to that point, the actual selection based on the data will be easy. So far, I haven't found a good place to hook this line (I keep getting back Nothing).
"Scott G." <no*****@this-is-extra-hotmail.com> wrote in message news:OE******** ******@tk2msftn gp13.phx.gbl...

I think you should be able to do this with a single table; have you tried something like:

ListBox lb = new ListBox();
lb.SelectionMod e = ListSelectionMo de.Multiple;
lb.DataSource = whatever;
lb.DataBind();
// now set from the list box
foreach (ListItem item in lb.Items)
{
if (IsItemSelected (item.Value)) // IsItemSelected is your method
{
item.Selected = true;
}
}

// or do it from the data side
foreach (DataRow row in whatever)
{
// blah, whatever you have to determine what's selected.
if (row["IsSelected "] == "True")
{
lb.Items.FindBy Value(row["Value"].ToString()).Se lected = true;
}
}

Scott
"Chris Leuty" <ch*********@no spam.briniker.c om> wrote in message news:OY******** ******@tk2msftn gp13.phx.gbl...
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)..

Before denormalizing the data in hopes of simplifying the binding, I thought
I would ask if I was missing an obviously simple approach to doing what I am
after (and leaving the dataset denormalized).

Thanks
Nov 18 '05 #6
Thanks, that worked. I converted the C# into VB and it looked like this:
lstTrades = CType(dlContact List.Items(e.It em.ItemIndex).F indControl("lst Trades"), ListBox)

VB casting is nasty looking to me (I much prefer C#, but I inherited this project....you know how that goes...). Anyway, you can see that the old line (lstTrades = CType(e.Item.Fi ndControl("lstT rades"), ListBox)) is simply looking in the wrong Item collection. Would help to look in the DataList hierarchy. Duh.

Thanks for the help

Chris

"Scott G." <no*****@this-is-extra-hotmail.com> wrote in message news:uB******** ******@TK2MSFTN GP10.phx.gbl...

Got it; here's an example in C# that gets you to the control; in the DG_EditCommand handler, you'll see that I grab the index of the item and then grab the item to get the ListBox1; now this seems to be the same as the code you posted in a message or two ago. If you run this page as a test you should see the "found control" message.

Scott
<%@ Page language="c#" AutoEventWireup ="false" Trace="true" %>
<%@ Import Namespace="Syst em.Data" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>DGBind </title>
<script language="C#" runat="server">
protected override void OnLoad(EventArg s e)
{
this.DG.DataSou rce = MyFakeDataSet;
this.DG.DataMem ber = "Table1";
this.DG.EditCom mand += new DataGridCommand EventHandler(DG _EditCommand);
this.DG.CancelC ommand += new DataGridCommand EventHandler(DG _CancelCommand) ;
if (!this.IsPostBa ck)
{
this.DG.DataBin d();
}
}

private void DG_EditCommand( object source, DataGridCommand EventArgs e)
{
this.DG.EditIte mIndex = e.Item.ItemInde x;
this.DG.DataBin d();
ListBox lb = (ListBox)this.D G.Items[e.Item.ItemInde x].FindControl("L istBox1");
if (lb != null)
{
this.Trace.Warn ("found control");
}
}

private void DG_CancelComman d(object source, DataGridCommand EventArgs e)
{
this.DG.EditIte mIndex = -1;
this.DG.DataBin d();
}

private DataSet m_ds;
private DataSet MyFakeDataSet {
get {
if (m_ds != null) {
return m_ds;
}
m_ds = new DataSet();
DataTable t = new DataTable("Tabl e1");
t.Columns.Add(" Column1");
m_ds.Tables.Add (t);

for (int i = 0; i < 10; i++) {
DataRow r = m_ds.Tables[0].NewRow();
r["Column1"] = i.ToString();
m_ds.Tables[0].Rows.Add(r);
}
return m_ds;
}
}
</script>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DG" runat="server" autogeneratecol umns="False">
<columns>
<asp:editcomman dcolumn buttontype="Lin kButton" updatetext="Upd ate" headertext="Col umn1" canceltext="Can cel"
edittext="Edit" ></asp:editcommand column>
<asp:templateco lumn headertext="Col umn1">
<itemtemplate >
<asp:Label id="Label1" runat="server" Text='<%# DataBinder.Eval (Container, "DataItem.Colum n1") %>'>
</asp:label>
</itemtemplate>
<edititemtempla te>
<asp:listbox id="ListBox1" runat="server"> </asp:listbox>
</edititemtemplat e>
</asp:templatecol umn>
</columns>
</asp:datagrid>
</form>
</body>
</html>

"Chris Leuty" <chris.leuty-at-nospam.brinker. com> wrote in message news:eu******** *****@TK2MSFTNG P11.phx.gbl...
I did bind the list data in the asp:listbox. What I'm trying to do now is mark certain items as selected, which is found in a different table. To be specific, the listbox contains trades (Plumbing, Electrical, Masonry, etc), and a second table contains the trades that apply to the displayed form (i.e. this vendor does this sort of work). This listbox is part of an Edit Template on a DataList (so that the selections can be changed).

I chose DataBinding() only as an example of where I have tried to get a reference to the listbox (it didn't work). I had to define the listbox in the code (VS.NET did not create a reference to it; I figured it was because it was part of the Edit Template, which is late bound), so I felt that I needed to find a place in the process where the listbox was created, but not yet sent back to the browser. The point where the data is bound seemed to make the most sense to me, but I can't grab a reference to it.

Thanks for your help so far.
"Scott G." <no*****@this-is-extra-hotmail.com> wrote in message news:eO******** ******@TK2MSFTN GP10.phx.gbl...

A good spot is probably Page_Load; if you are using the default code that VS.NET generates then this method should be there.... again, if you are using VS.NET there should already be a member field call "lstTrades" as a protected member of the code behind for the ASPX page.

If you want the ListBox during a ListBox DataBinding event; then just CType the sender (the first parent) to the event handler; In C# that might look like (for the ListBox DataBindingEven t):

private void lstTrades_DataB inding(object sender, EventArgs e)
{
ListBox lb = (ListBox)sender ;
}

I'm a bit confused if you if you are trying to bind the data to the ListBox in an event for the ListBox; if that's what you are asking, then I think you'd be better off binding the data to the ListBox in the containing page.

Scott
"Chris Leuty" <chris.leuty-at-nospam.brinker. com> wrote in message news:%2******** *******@TK2MSFT NGP09.phx.gbl.. .
The listbox is currently in the aspx, bound like this:
<TD vAlign="top" align="right">
<asp:label id="Label13" runat="server" CssClass="form-label">Trades</asp:label>
</TD>
<TD>
<asp:listbox id="lstTrades" runat="server" CssClass="form-text" Width="154px" SelectionMode=" Multiple" DataSource='<%# ContactInfo1 %>' DataMember="Tra des" DataTextField=" TradeName" DataValueField= "TradeID">
</asp:listbox>
</TD>
I have code similar to your examples in the codebehind, but I'm having a hard time finding a place where the Listbox is available as an object (or perhaps my code is fubar). I'm trying to get the listbox reference for the asp:listbox item above by doing the following (in this case, I was doing it during the event that bound the data. e is a DataListCommand EventArgs):
lstTrades = CType(e.Item.Fi ndControl("lstT rades"), ListBox)
I think once I get to that point, the actual selection based on the data will be easy. So far, I haven't found a good place to hook this line (I keep getting back Nothing).
"Scott G." <no*****@this-is-extra-hotmail.com> wrote in message news:OE******** ******@tk2msftn gp13.phx.gbl...

I think you should be able to do this with a single table; have you tried something like:

ListBox lb = new ListBox();
lb.SelectionMod e = ListSelectionMo de.Multiple;
lb.DataSource = whatever;
lb.DataBind();
// now set from the list box
foreach (ListItem item in lb.Items)
{
if (IsItemSelected (item.Value)) // IsItemSelected is your method
{
item.Selected = true;
}
}

// or do it from the data side
foreach (DataRow row in whatever)
{
// blah, whatever you have to determine what's selected.
if (row["IsSelected "] == "True")
{
lb.Items.FindBy Value(row["Value"].ToString()).Se lected = true;
}
}

Scott
"Chris Leuty" <ch*********@no spam.briniker.c om> wrote in message news:OY******** ******@tk2msftn gp13.phx.gbl...
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)..

Before denormalizing the data in hopes of simplifying the binding, I thought
I would ask if I was missing an obviously simple approach to doing what I am
after (and leaving the dataset denormalized).

Thanks
Nov 18 '05 #7

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

Similar topics

3
1951
by: Broder | last post by:
Hi there, I am currently running into a somewhat weired problem and hope that this NG is able to help ;-) I have a table in a MSSQL Server in which there is one Column that stores a date. The Format is 04.03.2004 Now I want to populate a Listbox but only with the year from that Column. So if I have lets say 10 entries with *.*.2004 and...
1
1956
by: cloverme | last post by:
Hi, I need help populating a listbox from a database on a webform. I created a an access database with a table, fields and data. Then I created a WebForm in vb.net and added a DropDownList control to the page. Then I created a data connection to my table... I right clicked and told it to generate a dataset (which it created dataset1).
0
1336
by: Bill Brinkworth | last post by:
I want the user to type in part of a word, and I want to return all words from an Access word table that contains specified letters. If they were to type "fe??", it should return all words in the dictionary that are 4 letters and begin with "fe" into a listbox. I am having difficulting searching for all words and taking all those words and...
6
7545
by: Deano | last post by:
I needed to have a listbox populated by locations which are stored in tblLocations. However I wanted an "All locations" entry to be at the top of the listbox. This is not in the tblLocations. The resulting list will be used to provide a query parameter and the user can choose all locations (the default) or a single, specific location. In...
2
2907
by: collie | last post by:
Hi, I have 2 listboxes. The first gets populated from the db as soon as the page loads. The second listbox get populated based on the user's selection from the first listbox. However, currently the code is such that with each selection there is a postback. We want to avoid it using filter and javascript. I am not using ADO.NET but adodbc...
6
1555
by: P K | last post by:
I have a listbox which I am populating on the client (it contains a list of dates selected from calender). The listbox is a server control. When I get to the server after postback by selecting an item in the list, the list box cannot be accessed. How do I repopulate the listbox on the server after postback and be able to access the elements?
15
26660
by: NasirMunir | last post by:
I am trying to populate a listbox from another listbox on a access form. My first listbox has names of tables on a linked odbc databse. I am trying to display the tuples of the table in the second listbox based on the selection of table name in the first listbox I am new to VBA, and am trying to do that in the following manner: Private Sub...
2
3819
by: NvrBst | last post by:
I populate a ListBox with a LogFile that has about (~1000 lines). The ListBox's datasource is a BindingList<string>. Whenever I add the elements, with the datasource set, it takes about 2 mins. I've tried wrapping "Listbox.SuspendLayout()" and "Listbox.ResumeLayout()" around the for loop (that does the adding) but it still takes about 2...
1
12841
by: dkohel | last post by:
I have 2 list boxes on my form. I am trying to populate listbox B with the selection from listbox A. I have set multi-select in both boxes to Extended... The user will select the items from listbox A and click an Add button that will then copy the items to listbox B. Then for each item in B, I need to add them to a database. If there...
0
7512
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7438
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...
0
7951
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...
1
7466
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...
0
7803
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...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3495
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...
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
751
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...

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.