Solved

Adding Item to html listbox using DataTable

Posted on 2007-11-20
13
407 Views
Last Modified: 2012-06-21
Hello Experts,

currently I am using a asp listbox which get populated based on the user selection from the combo box.. instead of using asp listbox , I wanted to use a html listbox and populate .

I have the code below that I am currently using..

lstFS is asp listbox .. can someone help me how to use a html listbox instead.

Thanks
For Each dt_row As DataRow In dt.Rows

            varNotAvailable = 1

            varX = varX + 1

            If varPlcSrvc = "Site of Service" Then

                If varX Mod 2 <> 0 Then

                    varstrcmbfin = varstrcmbfin & dt_row("MKT_ID") & "/"

                Else

                    varstrcmbfin = varstrcmbfin & dt_row("MKT_ID")

                    lstFS.Items.Add(varstrcmbfin)

                    varstrcmbfin = ""

                End If

            Else

                lstFS.Items.Add(dt_row("MKT_ID"))

            End If
 

        Next

Open in new window

0
Comment
Question by:sanjaypandey
  • 4
  • 3
  • 3
  • +1
13 Comments
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20320901
Are you coding in ASP.net page and not in code behind. As this can be done in Design view
0
 
LVL 15

Expert Comment

by:spprivate
ID: 20320903
Response.write("<SELECT>")
For Each dt_row As DataRow In dt.Rows
            varNotAvailable = 1
            varX = varX + 1
            If varPlcSrvc = "Site of Service" Then
                If varX Mod 2 <> 0 Then
                    varstrcmbfin = varstrcmbfin & dt_row("MKT_ID") & "/"
                Else
                    varstrcmbfin = varstrcmbfin & dt_row("MKT_ID")
Response.write("<OPTION>"+varstrcmbfin +"</OPTION>")

                    lstFS.Items.Add(varstrcmbfin)
                    varstrcmbfin = ""
                End If
            Else
                lstFS.Items.Add(dt_row("MKT_ID"))
            End If
 
        Next
Response.write("</SELECT>")

HTH
0
 
LVL 37

Expert Comment

by:samtran0331
ID: 20320993
sanjaypandey,

Why? the ASP.Net listbox will render the same html as a html listbox

spprivate,
Using response.write to build the html gives you no control over placement whatsoever, if you call that code from codebehind, it will actually render before the opening <html> tag...
0
 

Author Comment

by:sanjaypandey
ID: 20321114
asvforce : asnwer to your question, I am doing it in the design view  ...

spprivate: how do I pre-place the html control and then refer it directly in the code?
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20321170
sanjay,
just replace spprivate's block with your code block and everything should be good.
0
 
LVL 15

Expert Comment

by:spprivate
ID: 20321232
You cannot refer a client side control (html control) from a server script unless it is made as run as server control option.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:sanjaypandey
ID: 20321274
the reason I was thinking to switching to html control rather asp:listbox is that I have two listbox and I wanted user to select item from one listbox at a time, when he select items from one , all the items from the 2 listbox should be delselected.

and I was not able to do this using asp:listbox without causing an autopostback... which I dont want.......

can you suggest any way by means of which i can achive my objective without causing postback.

thanks again
0
 
LVL 15

Expert Comment

by:spprivate
ID: 20321360
You can use the javascript validation to do this.
I have done similar way back in 03.Let me see.
Javascript
function fundisable(val)
      {
       {
      document.getElementById("ddlOS").enabled =false;
      }
                 }

In codebehind Attributes.Add("onclick", " fundisable('" + strArg + "')");
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20321373
Then its very simple, write an onChange event to the <asp:listbox with return=false; Also you can set autopostback to false in the properties

onChange="return false;"
0
 
LVL 37

Expert Comment

by:samtran0331
ID: 20321378
any javascript you can wire up to an html listbox can be wired up to the same asp.net listbox...
if you had 2 html listboxes, how were you going to manage the de-selection of listbox2?
0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 500 total points
ID: 20321498
In the example below, select some items from the first listbox, then select something from the second listbox and the items in the first listbox will be deselected...
<%@ Page Language="VB" %>
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 

<script runat="server">

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

        If Not Me.IsPostBack Then

            ListBox2.Attributes.Add("onclick", "javascript:document.forms[0]." & ListBox1.ClientID & ".selectedIndex='-1';")

        End If

    End Sub

</script>
 

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <asp:ListBox ID="ListBox1" runat="server" SelectionMode="multiple">

                <asp:ListItem>Apples</asp:ListItem>

                <asp:ListItem>Oranges</asp:ListItem>

                <asp:ListItem>Bananas</asp:ListItem>

            </asp:ListBox>

            <asp:ListBox ID="ListBox2" runat="server" SelectionMode="multiple">

                <asp:ListItem>Mangos</asp:ListItem>

                <asp:ListItem>Papayas</asp:ListItem>

                <asp:ListItem>Plantains</asp:ListItem>

            </asp:ListBox>

        </div>

    </form>

</body>

</html>

Open in new window

0
 

Author Closing Comment

by:sanjaypandey
ID: 31410146
Thanks Samtran,

this is what I exactly needed, I was under the impression that I will not be able to use javascript with asp control, I tried using javascript and was getting error... thanks so much,
0
 

Author Comment

by:sanjaypandey
ID: 20321618
Thanks, this is what I was looking for, I appreciate your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now