|
|
19.1 How to make the listbox scroll down to show it's last entry when the page loads?
|
 |
<asp:ListBox id="ListBox1" runat="server"></asp:ListBox>
|
<asp:Button id="buttonAdd" runat="server" Text="Add"></asp:Button>
|
In button Click Event
VB.NET
|
ListBox1.Items.Add(DateTime.Now.ToString("MMM dd, yyyy") + " " + DateTime.Now.ToString("t"))
|
Dim scrollScript As String
|
scrollScript &= "<script language='javascript'>"
|
scrollScript &= "document.forms[0].ListBox1.selectedIndex " & _
|
" = document.forms[0].ListBox1.options.length-1;"
|
scrollScript &= "<" & "/" & "script>"
|
Page.RegisterStartupScript("", scrollScript)
|
ListBox1.Items.Add(DateTime.Now.ToString("MMM dd, yyyy") + " " + DateTime.Now.ToString("t"));
|
scrollScript += "<script language='javascript'>";
|
scrollScript += "document.forms[0].ListBox1.selectedIndex = document.forms[0].ListBox1.options.length-1;";
|
scrollScript += "<" + "/" + "script>";
|
Page.RegisterStartupScript("", scrollScript);
|
19.2 How to check if an item already exists in a listbox?
|
 |
Dim lstitem As ListItem = ListBox1.Items.FindByValue("<valuecheckedfor>")
|
If Not lstitem Is Nothing Then
|
Response.Write("Item Exists")
|
Response.Write("Item Does not exist")
|
ListItem lstitem = ListBox1.Items.FindByValue("<valuecheckedfor>");
|
Response.Write ("Does exist");
|
Response.Write ("Does not exist");
|
You can also use FindByText instead of FindByValue.
|
19.3 How to populate a listbox with the Column Names in a Table?
|
 |
For Each dc In ds.Tables(0).Columns
|
ListBox1.Items.Add(dc.ColumnName)
|
foreach (DataColumn dc in ds.Tables[0].Columns)
|
ListBox1.Items.Add(dc.ColumnName);
|
19.4 How to add items dynamically to a ListBox using an ArrayList?
|
 |
<asp:ListBox id="ListBox1" runat="server" AutoPostBack="True"></asp:ListBox>
|
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
|
'Put user code to initialize the page here
|
Dim arrList As New ArrayList
|
ListBox1.DataSource = arrList
|
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
|
Response.Write(ListBox1.SelectedItem.Text)
|
private void Page_Load(object sender, System.EventArgs e)
|
// Put user code to initialize the page here
|
ArrayList arrList = new ArrayList();
|
ListBox1.DataSource = arrList;
|
private void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e)
|
Response.Write (ListBox1.SelectedItem.Text );
|
19.5 How to move items between ListBoxes?
|
 |
<asp:ListBox id="ListBox1" runat="server">
|
<asp:ListItem Value="Faqs">Faqs</asp:ListItem>
|
<asp:ListItem Value="Tips">Tips</asp:ListItem>
|
<asp:ListItem Value="Tricks">Tricks</asp:ListItem>
|
<asp:ListItem Value="Advanced">Advanced</asp:ListItem>
|
<asp:Button id="btnToRight" style="Z-INDEX: 101; LEFT: 112px; POSITION: absolute; TOP: 24px"
|
runat="server" Text=">"></asp:Button>
|
<asp:ListBox id="ListBox2" style="Z-INDEX: 102; LEFT: 152px; POSITION: absolute; TOP: 16px" runat="server"></asp:ListBox>
|
<asp:RequiredFieldValidator id="RequiredFieldValidator1" style="Z-INDEX: 103; LEFT: 24px; POSITION: absolute; TOP: 120px"
|
runat="server" ErrorMessage="Please Select Item" ControlToValidate="ListBox1"></asp:RequiredFieldValidator>
|
Private Sub btnToRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToRight.Click
|
If ListBox1.Items.Count = 0 Then
|
Response.Write("No item to move")
|
Dim itemremoved As String = ListBox1.SelectedItem.Text
|
ListBox1.Items.Remove(itemremoved)
|
ListBox2.Items.Add(itemremoved)
|
private void btnToRight_Click(object sender, System.EventArgs e)
|
if (ListBox1.Items.Count == 0 )
|
Response.Write("No item to move");
|
string itemremoved = ListBox1.SelectedItem.Text;
|
ListBox1.Items.Remove(itemremoved);
|
ListBox2.Items.Add(itemremoved);
|
19.6 How to select a specific Item in a ListBox in code?
|
 |
ListBox1.Items.FindByValue(<Value>).Selected = true
|
'ListBox1.Items.FindByText(<Text>).Selected = true
|
ListBox1.Items.FindByValue(<Value>).Selected = true;
|
//ListBox1.Items.FindByText(<Text>).Selected = true;
|
19.7 How can you use a ListBox control to display items with Price above a specific value in one color and the ones below that value in a different color?
|
 |
The ListBox Web server control prevents us from assigning the style property to each item in the ListBox. This bug is confirmed by Microsoft Knowledge Base Article - 309338
So, instead use a HTML ListBox with runat=server
|
<SELECT id="listbox1" size="14" runat="server" >
|
Dim myconnection As SqlConnection
|
Dim myda As SqlDataAdapter
|
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
|
myconnection = New SqlConnection("Server=localhost;uid=sa;password=;database=northwind;")
|
myda = New SqlDataAdapter("Select * from Products ", myconnection)
|
myda.Fill(ds, "AllTables")
|
For i = 0 To ds.Tables(0).Rows.Count - 1
|
listBox1.Items.Add(New ListItem(ds.Tables(0).Rows(i)("UnitPrice"), ds.Tables(0).Rows(i)("ProductID")))
|
If ds.Tables(0).Rows(i)("UnitPrice") <= 25 Then
|
listBox1.Items(i).Attributes.Add("style", "color:red")
|
listBox1.Items(i).Attributes.Add("style", "color:green")
|
private void Page_Load(object sender, System.EventArgs e)
|
strConn="Data Source=localhost;uid=sa;pwd=;Initial Catalog=northwind";
|
mycn = new SqlConnection(strConn);
|
myda = new SqlDataAdapter ("Select * FROM Products ", mycn);
|
for(int i = 0 ;i < ds.Tables[0].Rows.Count - 1;i++)
|
listBox1.Items.Add (new ListItem(ds.Tables[0].Rows[i]["UnitPrice"].ToString(),
|
ds.Tables[0].Rows[i]["ProductID"].ToString()));
|
if(Convert.ToDouble(ds.Tables[0].Rows[i]["UnitPrice"].ToString()) <= 25 )
|
listBox1.Items[i].Attributes.Add("style", "color:red");
|
listBox1.Items[i].Attributes.Add("style", "color:green");
|
19.8 How to select all the Items from a listbox when the user selects a radio button?
|
 |
<asp:listbox id="ListBox1" runat="server" selectionmode="Multiple">
|
<asp:listitem value="Red">Red</asp:listitem>
|
<asp:listitem value="Blue">Blue</asp:listitem>
|
<asp:listitem value="Green">Green</asp:listitem>
|
<asp:listitem value="White">White</asp:listitem>
|
<asp:radiobutton id="RadioButton1" runat="server" groupname="selrb" text="Select All" autopostback="True"></asp:radiobutton>
|
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
|
For Each lstItem In ListBox1.Items
|
foreach (ListItem lstItem in ListBox1.Items)
|
19.9 How to use a single DataReader associated with two different tables to databind two different listbox controls?
|
 |
cn = New SqlConnection("Server=localhost;uid=sa;database=northwind;pwd=")
|
cmd = New SqlCommand("select * from products;select * from categories", cn)
|
ListBox1.DataTextField = "productname"
|
ListBox2.DataTextField = "categoryname"
|
cn = new SqlConnection("Server=localhost;uid=sa;database=northwind;pwd=");
|
cmd= new SqlCommand ("select * from products;select * from categories", cn);
|
dr = cmd.ExecuteReader();
|
ListBox1.DataSource = dr;
|
ListBox1.DataTextField = "productname";
|
ListBox2.DataSource = dr;
|
ListBox2.DataTextField = "categoryname";
|
19.10 How to clear all the items in a listbox?
|
 |
|
|
|
|