Showing posts with label listbox. Show all posts
Showing posts with label listbox. Show all posts

Monday, March 26, 2012

Populate a Listbox by using JavaScript

Hi,
I have a page with a ListBox on it, and It's also have a some buttons to
populate the ListBox by using JavaScript.
I have no problem with populate the listbox using JavaScript. But when the
page is submited / postback, the listbox is still empty.
Do you have any solution for this case (if I still want to use ListBox
Server Control) ?
thanks,
Devin WoodDevin,
You need to pass the new items to the server. It can be done in a hidden
input html control. On server side you will parse the input's value and
populate the listbox.
Eliyahu
"Devin Wood" <nordlitch@.msn.com> wrote in message
news:Opwr1avLFHA.2648@.TK2MSFTNGP14.phx.gbl...
> Hi,
> I have a page with a ListBox on it, and It's also have a some buttons to
> populate the ListBox by using JavaScript.
> I have no problem with populate the listbox using JavaScript. But when the
> page is submited / postback, the listbox is still empty.
> Do you have any solution for this case (if I still want to use ListBox
> Server Control) ?
> thanks,
> Devin Wood
>

Populate a Listbox by using JavaScript

Hi,

I have a page with a ListBox on it, and It's also have a some buttons to
populate the ListBox by using JavaScript.
I have no problem with populate the listbox using JavaScript. But when the
page is submited / postback, the listbox is still empty.

Do you have any solution for this case (if I still want to use ListBox
Server Control) ?

thanks,
Devin WoodDevin,

You need to pass the new items to the server. It can be done in a hidden
input html control. On server side you will parse the input's value and
populate the listbox.

Eliyahu

"Devin Wood" <nordlitch@.msn.com> wrote in message
news:Opwr1avLFHA.2648@.TK2MSFTNGP14.phx.gbl...
> Hi,
> I have a page with a ListBox on it, and It's also have a some buttons to
> populate the ListBox by using JavaScript.
> I have no problem with populate the listbox using JavaScript. But when the
> page is submited / postback, the listbox is still empty.
> Do you have any solution for this case (if I still want to use ListBox
> Server Control) ?
> thanks,
> Devin Wood

Populate a ListBox based on database search

I am trying to populate a ListBox with values retrieved from a database based on a search. I have a TextBox in which the user will enter the text they wish to search for, a Button which will submit the text, and then a ListBox which is invisible until values are returned from the database. I have a SqlDataSource with a SelectCommand saying "SELECT * FROM [table] WHERE [name] LIKE '@dotnet.itags.org.Parameter'". I want to know how to bind the value in the TextBox to the "@dotnet.itags.org.Parameter" in the Sql statement. I am quite new at programming, and I'm sure this is quite a simple task. Any help would be appreciated.

MYSQL:

myCommand.Parameters.Clear();
myCommand.CommandText = "SELECT * FROM [table] WHERE [name] LIKE (?)";
myCommand.Parameters.Add(new OdbcParameter(null, txtSearch.Text));

MSSQL:

myCommand.Parameters.Clear();
myCommand.CommandText = "SELECT * FROM [table] WHERE [name] LIKE @.search";
myCommand.Parameters.Add(new SqlParameter("@.search", txtSearch.Text));


Thanks for the reply. Where would I put this code? I'm using MSSql, but I am not using any stored procedures. All my SQL code is on my .aspx page. Should this go somewhere on my .aspx page or in my VB codebehind?

This would go in your code behind, in the onclick method from your button (double click your submit button and you'll go automatically to your onclick method).

Remember the code I gave in the example is C# code, I don't know the correct syntax in VB.net but it should get you started.


This should help:

http://www.asp.net/learn/dataaccess/default.aspx?tabid=63

Cheers

Populate data in ListBox for Multiple Selections (C#)

Hi there,
I am new in ASP.NET and wonder if somebody can help me with populating data in a ListBox from a database. Users should be able to choose multiple selections and pass the selected values for a search query. Please let me know if I miss anything.
Thanks a lot in advance!
// search.aspx
<asp:ListBox id="Community2" runat="server"></asp:ListBox>
//search.aspx.cs

if (!Community2.Items.FindByValue("-1").Selected)

{

for (int i=0 ; i<Community2.Items.Count ; i++)

{

if (Community2.ItemsIdea [I].Selected)

{

if (Community2 != "") Community2 += ",";

Community2 += Community2.ItemsIdea [I].Value;

}

}

}


//db.cs

publicvoid PopulateCommunities2(ListControl i_list,bool i_showalloption)

{

OleDbDataAdapter adapter =new OleDbDataAdapter("SELECT distinct community FROM properties", m_conn);

DataTable dt =new DataTable();

adapter.Fill(dt);

i_list.Items.Clear();

if (i_showalloption) i_list.Items.Add(new ListItem("All Communities", "-1"));

for (int i=0 ; i<dt.Rows.Count ; i++)

{

i_list.Items.Add(new ListItem(dt.RowsIdea [I]["community"])); //I am using pre-build code and have no idea what else I could do here.

}

}

For databind of a ListBox, you don't need to use loop to put data into the list. Just use ListBox1.DataSource field. For example:
ListBox1.DataSourse = GetCommunities();
ListBox1.DataValueField = "community";
ListBox1.DataTextField = "community";
ListBox1.DataBind;
GetCommunites method could be a DataSet, DataTable or DataView like:
Private DataTable GetCommunities()
{

OleDbDataAdapter adapter =new OleDbDataAdapter("SELECT distinct community FROM properties", m_conn);

DataTable dt =new DataTable();

adapter.Fill(dt);


return dt;
}

For your process of the user selected items from the ListBox, you can put a "," at the first or last. But, after that, you need to remove the first "," (if you put at first), or remove the last "," (if you put at the last).


Also, you're treating Community2 like a listbox, as in
Community2.Items.FindByValue("-1")
...but you're also treating it as a string, like this:
Community2 += ",";
...sometimes even in the same statement!

if (Community2 != "") Community2 += ",";

Community2 += Community2.Items[i ].Value;

}
You need to decide which one it is (I'm assuming the former).

populate datagrid from listbox value

I have to populate a list box from a db table and when the user clicks on
the value in the listbox I need to then populate a datagrid with detailed
information for the highlighted row.
How can i do that?Try this?
ASP.NET: Master/Detail View using a DropDownList and a DataGrid
[url]http://authors.aspalliance.com/aldotnet/examples/masterdetail_ddl_datagrid.aspx[/u
rl]
"Mike" <me@.me.com> wrote in message
news:ORHOgJWXEHA.2364@.TK2MSFTNGP12.phx.gbl...
>I have to populate a list box from a db table and when the user clicks on
> the value in the listbox I need to then populate a datagrid with detailed
> information for the highlighted row.
> How can i do that?
>

populate datagrid from listbox value

I have to populate a list box from a db table and when the user clicks on
the value in the listbox I need to then populate a datagrid with detailed
information for the highlighted row.

How can i do that?Try this?

ASP.NET: Master/Detail View using a DropDownList and a DataGrid

http://authors.aspalliance.com/aldo...l_datagrid.aspx

"Mike" <me@.me.com> wrote in message
news:ORHOgJWXEHA.2364@.TK2MSFTNGP12.phx.gbl...
>I have to populate a list box from a db table and when the user clicks on
> the value in the listbox I need to then populate a datagrid with detailed
> information for the highlighted row.
> How can i do that?

Saturday, March 24, 2012

populate listbox using Javascript

I tried following :
document.getElementById("lsTest").Items.Add(new ListItem("Test"))
This gives following error message:
'document.getElementById(...).Items' is null or not an object
Also tried:
document.getElementById("lsTest").Add(new ListItem("Test"))
Result - Error Message:
'listitem' is undefined
Any idea?var o = document.createElement("option");
o.text = "test"
o.value = "test";
document.getElementById("lsTest").add(o);
-- bruce (sqlwork.com)
"RA" <rchaudhary-nospam@.storis.com> wrote in message
news:%23IOtEyPyEHA.2016@.TK2MSFTNGP15.phx.gbl...
| I tried following :
| document.getElementById("lsTest").Items.Add(new ListItem("Test"))
|
| This gives following error message:
| 'document.getElementById(...).Items' is null or not an object
|
| Also tried:
| document.getElementById("lsTest").Add(new ListItem("Test"))
| Result - Error Message:
| 'listitem' is undefined
|
| Any idea?
|
|
Hi RA,
you can not add a listitem from javascript. A ListBox is rendered as
html SELECT element. So, you should add the items in javascript as if you
are adding to a SELECT element, which would be like follows
document.getElementById("lsTest").add(new Option("Text","Value"));
HTH
Kumar
"RA" <rchaudhary-nospam@.storis.com> wrote in message
news:%23IOtEyPyEHA.2016@.TK2MSFTNGP15.phx.gbl...
> I tried following :
> document.getElementById("lsTest").Items.Add(new ListItem("Test"))
> This gives following error message:
> 'document.getElementById(...).Items' is null or not an object
> Also tried:
> document.getElementById("lsTest").Add(new ListItem("Test"))
> Result - Error Message:
> 'listitem' is undefined
> Any idea?
>
To remove an item from a textbox I tried;
document.getElementById("lsTest").remove(new Option("Testing","Testing"));
and
document.getElementById("lsTest").remove("Testing");
neither of above work. Both deletes very first Item in the listbox.
What I am doing wrong?
"Kumar Reddi" <KumarReddi@.REMOVETHIS.gmail.com> wrote in message
news:Odz5c8PyEHA.3096@.tk2msftngp13.phx.gbl...
> Hi RA,
> you can not add a listitem from javascript. A ListBox is rendered as
> html SELECT element. So, you should add the items in javascript as if you
> are adding to a SELECT element, which would be like follows
> document.getElementById("lsTest").add(new Option("Text","Value"));
> HTH
> Kumar
> "RA" <rchaudhary-nospam@.storis.com> wrote in message
> news:%23IOtEyPyEHA.2016@.TK2MSFTNGP15.phx.gbl...
>

populate listbox using Javascript

I tried following :
document.getElementById("lsTest").Items.Add(new ListItem("Test"))

This gives following error message:
'document.getElementById(...).Items' is null or not an object

Also tried:
document.getElementById("lsTest").Add(new ListItem("Test"))
Result - Error Message:
'listitem' is undefined

Any idea?var o = document.createElement("option");
o.text = "test"
o.value = "test";
document.getElementById("lsTest").add(o);

-- bruce (sqlwork.com)

"RA" <rchaudhary-nospam@.storis.com> wrote in message
news:%23IOtEyPyEHA.2016@.TK2MSFTNGP15.phx.gbl...
| I tried following :
| document.getElementById("lsTest").Items.Add(new ListItem("Test"))
|
| This gives following error message:
| 'document.getElementById(...).Items' is null or not an object
|
| Also tried:
| document.getElementById("lsTest").Add(new ListItem("Test"))
| Result - Error Message:
| 'listitem' is undefined
|
| Any idea?
|
|
Hi RA,
you can not add a listitem from javascript. A ListBox is rendered as
html SELECT element. So, you should add the items in javascript as if you
are adding to a SELECT element, which would be like follows

document.getElementById("lsTest").add(new Option("Text","Value"));

HTH
Kumar

"RA" <rchaudhary-nospam@.storis.com> wrote in message
news:%23IOtEyPyEHA.2016@.TK2MSFTNGP15.phx.gbl...
> I tried following :
> document.getElementById("lsTest").Items.Add(new ListItem("Test"))
> This gives following error message:
> 'document.getElementById(...).Items' is null or not an object
> Also tried:
> document.getElementById("lsTest").Add(new ListItem("Test"))
> Result - Error Message:
> 'listitem' is undefined
> Any idea?
To remove an item from a textbox I tried;

document.getElementById("lsTest").remove(new Option("Testing","Testing"));
and
document.getElementById("lsTest").remove("Testing");

neither of above work. Both deletes very first Item in the listbox.

What I am doing wrong?

"Kumar Reddi" <KumarReddi@.REMOVETHIS.gmail.com> wrote in message
news:Odz5c8PyEHA.3096@.tk2msftngp13.phx.gbl...
> Hi RA,
> you can not add a listitem from javascript. A ListBox is rendered as
> html SELECT element. So, you should add the items in javascript as if you
> are adding to a SELECT element, which would be like follows
> document.getElementById("lsTest").add(new Option("Text","Value"));
> HTH
> Kumar
> "RA" <rchaudhary-nospam@.storis.com> wrote in message
> news:%23IOtEyPyEHA.2016@.TK2MSFTNGP15.phx.gbl...
>> I tried following :
>> document.getElementById("lsTest").Items.Add(new ListItem("Test"))
>>
>> This gives following error message:
>> 'document.getElementById(...).Items' is null or not an object
>>
>> Also tried:
>> document.getElementById("lsTest").Add(new ListItem("Test"))
>> Result - Error Message:
>> 'listitem' is undefined
>>
>> Any idea?
>>
>>

Populate ListBox With Directories & Files

I have a ListBox which should list all the files & directories that
exist in a particular directory. The problem is I can get the ListBox
to list either all the files or all the directories but not the 2 of
them together. This is what I tried:
Sub Page_Load(....)
Dim dInfo As DirectoryInfo
dInfo = New DirectoryInfo(Server.MapPath(MyDir))
'get all the directories existing in MyDir
lstFilesDirs.DataSource = dInfo.GetDirectories
'get all the files existing in MyDir
lstFilesDirs.DataSource = dInfo.GetFiles
lstFilesDirs.DataBind()
End Sub
<form runat="server">
<asp:ListBox ID="lstFilesDirs" runat="server"/>
</form>
In the above code, since the GetFiles method has been issued after the
GetDirectories method, the ListBox lists only the files existing in the
directory named MyDir. On the other hand, had the GetFiles method been
issued after the GetDirectories method, then the ListBox would have
listed only the directories existing in the directory MyDir. So the
ListBox lists either all the files or all the directories existing in
the directory named MyDir.
How do I list all the directories as well as all the files existing in
the directory MyDir in the ListBox?Well, this can be done using the GetFileSystemInfos method of the
DirectoryInfo object. Here's how you do it (just in case, if someone
encounters a similar problem):
Sub Page_Load(....)
Dim dInfo As DirectoryInfo
dInfo = New DirectoryInfo(Server.MapPath(MyDir))
'get all directories as well as files existing in MyDir
lstFilesDirs.DataSource = dInfo.GetFileSystemInfos
lstFilesDirs.DataBind()
End Sub
Even the GetFileSystemEntries method of the Directory object can be
used to get all the directories & files existing in a directory. The
difference between GetFileSystemInfos & GetFileSystemEntries is that
the latter, being a method of the Directory class, will get the
physical path of the files & directories as well along with the file
names & directory names respectively.
lstFilesDirs.DataSource =
Directory.GetFileSystemEntries(Server.MapPath(My Dir))
rn5a@.rediffmail.com wrote:
> I have a ListBox which should list all the files & directories that
> exist in a particular directory. The problem is I can get the ListBox
> to list either all the files or all the directories but not the 2 of
> them together. This is what I tried:
> Sub Page_Load(....)
> Dim dInfo As DirectoryInfo
> dInfo = New DirectoryInfo(Server.MapPath(MyDir))
> 'get all the directories existing in MyDir
> lstFilesDirs.DataSource = dInfo.GetDirectories
> 'get all the files existing in MyDir
> lstFilesDirs.DataSource = dInfo.GetFiles
> lstFilesDirs.DataBind()
> End Sub
> <form runat="server">
> <asp:ListBox ID="lstFilesDirs" runat="server"/>
> </form>
> In the above code, since the GetFiles method has been issued after the
> GetDirectories method, the ListBox lists only the files existing in the
> directory named MyDir. On the other hand, had the GetFiles method been
> issued after the GetDirectories method, then the ListBox would have
> listed only the directories existing in the directory MyDir. So the
> ListBox lists either all the files or all the directories existing in
> the directory named MyDir.
> How do I list all the directories as well as all the files existing in
> the directory MyDir in the ListBox?
Hi,
rn5a@.rediffmail.com wrote:
> I have a ListBox which should list all the files & directories that
> exist in a particular directory. The problem is I can get the ListBox
> to list either all the files or all the directories but not the 2 of
> them together. This is what I tried:
> Sub Page_Load(....)
> Dim dInfo As DirectoryInfo
> dInfo = New DirectoryInfo(Server.MapPath(MyDir))
> 'get all the directories existing in MyDir
> lstFilesDirs.DataSource = dInfo.GetDirectories
> 'get all the files existing in MyDir
> lstFilesDirs.DataSource = dInfo.GetFiles
I saw you got the answer according to your later post, but just to
clarify, you overwrite the DataSource property with the list of files
after you gave it the list of directories. This is not cumulative.
Since both FileInfo and DirectoryInfo derive from FileSystemInfo, you
can merge both arrays returned by GetDirectories and GetFiles (but in
that case, it doesn't make sense, since the method GetFileSystemInfo
gives you everything you need as you found out already).
HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Thanks, Laurent. It was indeed very kind of you to pinpoint those
points.

> Since both FileInfo and DirectoryInfo derive from FileSystemInfo, you
> can merge both arrays returned by GetDirectories and GetFiles
Laurent, could you please tell me how to merge the arrays returned by
GetDirectories & GetFiles methods? A small code snippet would be highly
appreciated.

> that case, it doesn't make sense, since the method GetFileSystemInfo
> gives you everything you need as you found out already).
I guess it was a typo; the method is GetFileSystemInfos & not
GetFileSystemInfo.
Thanks once again.
Laurent Bugnion [MVP] wrote:
> Hi,
> rn5a@.rediffmail.com wrote:
> I saw you got the answer according to your later post, but just to
> clarify, you overwrite the DataSource property with the list of files
> after you gave it the list of directories. This is not cumulative.
> Since both FileInfo and DirectoryInfo derive from FileSystemInfo, you
> can merge both arrays returned by GetDirectories and GetFiles (but in
> that case, it doesn't make sense, since the method GetFileSystemInfo
> gives you everything you need as you found out already).
> HTH,
> Laurent
> --
> Laurent Bugnion [MVP ASP.NET]
> Software engineering: http://www.galasoft-LB.ch
> PhotoAlbum: http://www.galasoft-LB.ch/pictures
> Support children in Calcutta: http://www.calcutta-espoir.ch
Hi,
rn5a@.rediffmail.com wrote:
> Thanks, Laurent. It was indeed very kind of you to pinpoint those
> points.
>
> Laurent, could you please tell me how to merge the arrays returned by
> GetDirectories & GetFiles methods? A small code snippet would be highly
> appreciated.
Off the top of my head:
DirectoryInfo dir = new DirectoryInfo( "d:\\temp" );
FileInfo[] files = dir.GetFiles();
DirectoryInfo[] dirs = dir.GetDirectories();
List<FileSystemInfo> all = new List<FileSystemInfo>();
all.AddRange( files );
all.AddRange( dirs );
FileSystemInfo[] allFileSystemInfos = all.ToArray();
foreach ( FileSystemInfo fileSystemInfo in allFileSystemInfos )
{
Console.WriteLine( fileSystemInfo.Name );
}
I am sure there are more elegant ways to do that though.

> I guess it was a typo; the method is GetFileSystemInfos & not
> GetFileSystemInfo.
Yes, sorry!

> Thanks once again.
Greetings,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch

Populate ListBox With Directories & Files

I have a ListBox which should list all the files & directories that
exist in a particular directory. The problem is I can get the ListBox
to list either all the files or all the directories but not the 2 of
them together. This is what I tried:

Sub Page_Load(....)
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath(MyDir))

'get all the directories existing in MyDir
lstFilesDirs.DataSource = dInfo.GetDirectories

'get all the files existing in MyDir
lstFilesDirs.DataSource = dInfo.GetFiles

lstFilesDirs.DataBind()
End Sub

<form runat="server">
<asp:ListBox ID="lstFilesDirs" runat="server"/>
</form>

In the above code, since the GetFiles method has been issued after the
GetDirectories method, the ListBox lists only the files existing in the
directory named MyDir. On the other hand, had the GetFiles method been
issued after the GetDirectories method, then the ListBox would have
listed only the directories existing in the directory MyDir. So the
ListBox lists either all the files or all the directories existing in
the directory named MyDir.

How do I list all the directories as well as all the files existing in
the directory MyDir in the ListBox?Well, this can be done using the GetFileSystemInfos method of the
DirectoryInfo object. Here's how you do it (just in case, if someone
encounters a similar problem):

Sub Page_Load(....)
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath(MyDir))

'get all directories as well as files existing in MyDir
lstFilesDirs.DataSource = dInfo.GetFileSystemInfos
lstFilesDirs.DataBind()
End Sub

Even the GetFileSystemEntries method of the Directory object can be
used to get all the directories & files existing in a directory. The
difference between GetFileSystemInfos & GetFileSystemEntries is that
the latter, being a method of the Directory class, will get the
physical path of the files & directories as well along with the file
names & directory names respectively.

lstFilesDirs.DataSource =
Directory.GetFileSystemEntries(Server.MapPath(My Dir))

rn5a@.rediffmail.com wrote:

Quote:

Originally Posted by

I have a ListBox which should list all the files & directories that
exist in a particular directory. The problem is I can get the ListBox
to list either all the files or all the directories but not the 2 of
them together. This is what I tried:
>
Sub Page_Load(....)
Dim dInfo As DirectoryInfo
>
dInfo = New DirectoryInfo(Server.MapPath(MyDir))
>
'get all the directories existing in MyDir
lstFilesDirs.DataSource = dInfo.GetDirectories
>
'get all the files existing in MyDir
lstFilesDirs.DataSource = dInfo.GetFiles
>
lstFilesDirs.DataBind()
End Sub
>
<form runat="server">
<asp:ListBox ID="lstFilesDirs" runat="server"/>
</form>
>
In the above code, since the GetFiles method has been issued after the
GetDirectories method, the ListBox lists only the files existing in the
directory named MyDir. On the other hand, had the GetFiles method been
issued after the GetDirectories method, then the ListBox would have
listed only the directories existing in the directory MyDir. So the
ListBox lists either all the files or all the directories existing in
the directory named MyDir.
>
How do I list all the directories as well as all the files existing in
the directory MyDir in the ListBox?


Hi,

rn5a@.rediffmail.com wrote:

Quote:

Originally Posted by

I have a ListBox which should list all the files & directories that
exist in a particular directory. The problem is I can get the ListBox
to list either all the files or all the directories but not the 2 of
them together. This is what I tried:
>
Sub Page_Load(....)
Dim dInfo As DirectoryInfo
>
dInfo = New DirectoryInfo(Server.MapPath(MyDir))
>
'get all the directories existing in MyDir
lstFilesDirs.DataSource = dInfo.GetDirectories
>
'get all the files existing in MyDir
lstFilesDirs.DataSource = dInfo.GetFiles


I saw you got the answer according to your later post, but just to
clarify, you overwrite the DataSource property with the list of files
after you gave it the list of directories. This is not cumulative.

Since both FileInfo and DirectoryInfo derive from FileSystemInfo, you
can merge both arrays returned by GetDirectories and GetFiles (but in
that case, it doesn't make sense, since the method GetFileSystemInfo
gives you everything you need as you found out already).

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Thanks, Laurent. It was indeed very kind of you to pinpoint those
points.

Quote:

Originally Posted by

Since both FileInfo and DirectoryInfo derive from FileSystemInfo, you
can merge both arrays returned by GetDirectories and GetFiles


Laurent, could you please tell me how to merge the arrays returned by
GetDirectories & GetFiles methods? A small code snippet would be highly
appreciated.

Quote:

Originally Posted by

that case, it doesn't make sense, since the method GetFileSystemInfo
gives you everything you need as you found out already).


I guess it was a typo; the method is GetFileSystemInfos & not
GetFileSystemInfo.

Thanks once again.

Laurent Bugnion [MVP] wrote:

Quote:

Originally Posted by

Hi,
>
rn5a@.rediffmail.com wrote:

Quote:

Originally Posted by

I have a ListBox which should list all the files & directories that
exist in a particular directory. The problem is I can get the ListBox
to list either all the files or all the directories but not the 2 of
them together. This is what I tried:

Sub Page_Load(....)
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath(MyDir))

'get all the directories existing in MyDir
lstFilesDirs.DataSource = dInfo.GetDirectories

'get all the files existing in MyDir
lstFilesDirs.DataSource = dInfo.GetFiles


>
I saw you got the answer according to your later post, but just to
clarify, you overwrite the DataSource property with the list of files
after you gave it the list of directories. This is not cumulative.
>
Since both FileInfo and DirectoryInfo derive from FileSystemInfo, you
can merge both arrays returned by GetDirectories and GetFiles (but in
that case, it doesn't make sense, since the method GetFileSystemInfo
gives you everything you need as you found out already).
>
HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch


Hi,

rn5a@.rediffmail.com wrote:

Quote:

Originally Posted by

Thanks, Laurent. It was indeed very kind of you to pinpoint those
points.
>

Quote:

Originally Posted by

>Since both FileInfo and DirectoryInfo derive from FileSystemInfo, you
>can merge both arrays returned by GetDirectories and GetFiles


>
Laurent, could you please tell me how to merge the arrays returned by
GetDirectories & GetFiles methods? A small code snippet would be highly
appreciated.


Off the top of my head:

DirectoryInfo dir = new DirectoryInfo( "d:\\temp" );
FileInfo[] files = dir.GetFiles();
DirectoryInfo[] dirs = dir.GetDirectories();

List<FileSystemInfoall = new List<FileSystemInfo>();
all.AddRange( files );
all.AddRange( dirs );

FileSystemInfo[] allFileSystemInfos = all.ToArray();

foreach ( FileSystemInfo fileSystemInfo in allFileSystemInfos )
{
Console.WriteLine( fileSystemInfo.Name );
}

I am sure there are more elegant ways to do that though.

Quote:

Originally Posted by

Quote:

Originally Posted by

>that case, it doesn't make sense, since the method GetFileSystemInfo
>gives you everything you need as you found out already).


>
I guess it was a typo; the method is GetFileSystemInfos & not
GetFileSystemInfo.


Yes, sorry!

Quote:

Originally Posted by

Thanks once again.


Greetings,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch

Friday, March 16, 2012

Populating a list box from a loop

I was wondering how you would populate a listbox in asp.net with values from an array created by a loop.
here is the loop structure I am using:
Public Sub Page_Load()
Dim MyString, MyArray, Msg
Dim i as integer
Dim AddThis as string

MyString=BillDS.FieldValue("Week Reason 1", nothing)
MyArray=Split(MyString, "F", -1, vbTextCompare)

Msg=MyArray(0)

For i = 1 To UBound(MyArray)
Msg=Msg & "," & MyArray(i)
Next
lblReasonforError.Text=Msg

End Sub

I can get it to populate the first value in the array but I need it to populate all the values in the array which will vary according to the record it is looking at. Then with those value(s) I want to populate a listbox with all the error codes that are stored in a table on sql server according to the results of the split.
For example:
tblErrorCodes
Code ErrorMessage
1 Wrong
2 Bad

Split gets the value of 1 and 2
Populate listbox like so:
Wrong
Bad
Hope this is clear
Thank you

You can simply bind datalist to an array by setting the array as the datasource of the datalist
http://aspnet.4guysfromrolla.com/articles/082504-1.aspx
Hope that helps


uncleb wrote:

You can simply bind datalist to an array by setting the array as the datasource of the datalist


It's the simplest and the only way since ListBox doesn't have Add method.
Madmaxrtw,I'm not sure what your code is about.
But in case you need to use just some items you'd have to transform array into another one.
Or might be use DataTextField and DataValueField properties.

Hey guys I got it here is the loop script

<script language="VB" runat="server">
Public Sub Page_Load()
Dim MyString, MyArray, Msg
Dim i as integer
Dim AddThis as string


MyString=BillDS.FieldValue("Week Reason 1", nothing)
MyArray=Split(MyString, "F", -1, vbTextCompare)

Msg=MyArray(0)


For i = 1 To UBound(MyArray)
Msg=Msg & "," & MyArray(i)
Next

dlErrorList.DataSource=MyArray
dlErrorList.DataBind()

End Sub
</script>

And the DataList:
<asp:datalist CellPadding="10" ID="dlErrorList" RepeatColumns="0"
RepeatDirection="Vertical" runat="server" gridline="Both">
<ItemTemplate>
<b><%# Container.DataItem %></b>
</ItemTemplate>
</asp:datalist>

Thank you so MUCH!!!


Yakov72 wrote:

It's the simplest and the only way since ListBox doesn't have Add method.



Yes it does.
You add to the ListBox Items array. Not directly to the ListBox.

BobT wrote:


Yes it does.
You add to the ListBox Items array. Not directly to the ListBox.


You're right.
Thank you for corrections.

populating a list box by alphabetical surname

Hey guys

I have a listbox contianing strings which ahs been populated from a databsae via "SELECT * FROM researchers"

i.e

John Doe www.johndoe.com
David Allen www.daveallen.com
Andrew Rodgers www.andrewdavidson.com
..

I need this to be displayed as

David Allen www.daveallen.com
John Doe www.johndoe.com
Andrew Rodgers www.andrewdavidson.com

i.e alphabetically by surname?
How do I do this?SELECT * FROM researchers ORDER BY LastName ASC

Populating a listbox from DB w/o postback

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 and no datagrids or datasets (please don't tell me that i should as my boss clearly doesn't want to get into it at this stage).

How can i do it?

Thanks

Currently the code with the postback is as follows:
page_load

if not page.ispostback then
Do While Not rs.EOF
li = New ListItem(rs("cat_name").Value, rs("cat_id").Value)
List1.Items.Add(li)
rs.MoveNext()
Loop

End If

'List1.SelectedIndex = 0
rs.Close()
rs = Nothing

List1.SelectedValue = cat_id

'here there is some other code not relevant to the listboxes

'select items for second listbox
Dim rsSub As New ADODB.Recordset
rsSub.Open("SELECT sub_name, sub_id FROM subs WHERE cat_id=" & cat_id.ToString & " ORDER BY sub_name ASC", cn)
Dim l_item As ListItem
Do While Not rsSub.EOF
l_item = New ListItem(rsSub("sub_name").Value, rsSub("sub_id").Value)
List2.Items.Add(l_item)
rsSub.MoveNext()
Loop

List2.SelectedValue = sub_id

rsSub.Close()
rsSub = Nothing

cn.Close()
End If
' End If

End If

End Sub

Protected Sub Select1Change(ByVal sender As System.Object, ByVal e As System.EventArgs)

Dim cn As New ADODB.Connection
cn.Open(YBayTools.Constants.ConnectionString)

Dim rs As New ADODB.Recordset
Dim li As ListItem

rs.Open("Select * from subs where cat_ID =" & List1.SelectedItem.Value.ToString, cn)
'rs.Open("Select * from subs where cat_ID ='" & List1.SelectedItem.Text.ToString & "' & List1.SelectedItem.value.ToString", cn)

Dim strCat As String

If List2.Items.Count > 0 Then
List2.Items.Clear()
End If

If rs.BOF And rs.EOF Then
Response.Write("no records found")
Else

Do While Not rs.EOF
li = New ListItem(rs("sub_name").Value, rs("sub_id").Value)
List2.Items.Add(li)
rs.MoveNext()

Loop

End If

rs.Close()
rs = Nothing
cn.Close()You code is done to work with postback. So write client-site code with javascript on the first listBox and in this code, read the DB and populate you other listBox.
That's the thing I am not sure how to write it in jscript.
I need to get the data for both listboxes from a db.
You can use an XmlHttp object from client-side script, but this will probably only work on IE and Mozilla. Other solutions could use a lot of script and hidden frames (beuark!)