I need specific programming code to retain the selected index value,
and pass correct value to downstream cascaded listboxes between
autoposts (not included in the page load subroutine) which use SQL
data source in asp.net?
Question Description.
I have a page which uses ASP.net with calls to MS SQL to
populate listboxes. The listboxes are cascaded. Meaning, when you
select one , the value of it is used in a dynamic sql statement which
populates the next. All the listboxes autopostback properties are set
to true. So, each time the user clicks a box, an event fires to
retrieve an sql query to populate the next listbox based on the value
of the previous one.
The first listbox is populated within the page load subroutine, and
always retains it's selection between posts. No problems here. The
problem is when selecting the second listbox, the selectedindex always
toggles to 0 and has a selected indexvalue of 0 which goes to the
downstream listbox 3 each time a selection is made. For testing
purposes, I set up listbox 3 to get selectedindex value of listbox 2.
No matter which selection you make in listbox 2, listbox 3 recieves
selected index of 0 and listbox 2 toggles to selected index of 0.
Now I have an older version of this page, where all listbox source
data is hardcoded into the page, and it works, the selected index
always holds between posts (See Example Code 1-Included link to view
live page) of all the listboxes.
Now, in my new version of the page (See Example Code 2), which
uses calls to the SQL server, the selected index value is always lost
and toggles to 0 and passes 0 to third listbox when a selection is
made.
Working (Old hard coded page)
http://programminglibrary.com/Programming%20Library/Xceltools2/index.aspx
Listbox sources Hardcoded into page with no SQL call to populate
listboxes. (See Example Code 1)
Demo by going to site link and following the clicks
Click Controls->Listbox->Clear All Contents
In this example, asp listbox control retains selection between posts.
Not Working (SQL Driven Version)
http://programminglibrary.com/Programming%20Library/default.aspx
Example with call to SQL to populate listboxes.
Click following selections to demo.
Select Excel VBA->Functions
In this example, listbox 2 selection is lost and always passes
selectedindex of 0 to third listbox. AHHHH!
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Example Code 1 (Hard Coded no call to SQL Server)
<%@ Page Language="VB" %>
<script runat="server">
' Insert page code here
'
Sub Page_Load
lb_nav.selectedindex = 0
end sub
Sub lb_1_SelectedIndexChanged(s as Object, e as EventARgs)
'USE : PUSH VALUES TO LB_2
Dim v1 as string
v1 = lb_1.selecteditem.text
'ADD VALUE TO LB_2
'CLEAR LB
lb_2.items.clear
lb_3.items.clear
'LOGIC
sELECT CASE v1
case is = "Controls"
'_____________________________code____________________
lb_2.Items.Add("ListBox")
'lb_2.Items.Add("CheckBox")
'lb_2.Items.Add("Radio-Button")
'____________________________end code_________________
case is = "Range"
'_____________________________code____________________
'lb_2.Items.Add("Cells")
'lb_2.Items.Add("Column")
'lb_2.Items.Add("Row")
'____________________________end code_________________
end select
end sub
Sub lb_2_SelectedIndexChanged(s as Object, e as EventARgs)
'USE : PUSH VALUES TO LB_3
Dim v1 as string
v1 = lb_2.selecteditem.text
'ADD VALUE TO LB_2
'CLEAR LB
lb_3.items.clear
'LOGIC
sELECT CASE v1
case is = "ListBox"
'_____________________________code____________________
LB_3.ITEMS.ADD(NEW ListItem("ADD-VALUE TEXT STRING","1"))
LB_3.ITEMS.ADD(NEW ListItem("ADD-VALUE VARIABLE","2"))
LB_3.ITEMS.ADD(NEW ListItem("ADD-VALUE TEXT TO MULTICOLUMN LISTBOX","3"))
LB_3.ITEMS.ADD(NEW ListItem("ADD-VALUE VARIABLE TO MULTICOLUMN LISTBOX","4"))
LB_3.ITEMS.ADD(NEW ListItem("CLEAR-ALL CONTENTS","5"))
LB_3.ITEMS.ADD(NEW ListItem("CLEAR-ALL CONTENTS (DYNAMIC NAMED CONTROL)","6"))
LB_3.ITEMS.ADD(NEW ListItem("USE-DYNAMIC NAMED LISTBOX WITH VARIABLES","7"))
LB_3.ITEMS.ADD(NEW ListItem("ADD-SHEET NAMES TO LISTBOX","8"))
LB_3.ITEMS.ADD(NEW ListItem("COUNT-NUMBER OF ITEMS IN LISTBOX","9"))
LB_3.ITEMS.ADD(NEW ListItem("TEST-LISTBOX FOR SELECTION (SINGLE
SELECT MODE)","10"))
LB_3.ITEMS.ADD(NEW ListItem("STORE-SELECTED ITEM POSITION IN
LISTBOX(SINGLE SELECT MODE)","11"))
'____________________________end code_________________
end select
end sub
Sub lb_3_SelectedIndexChanged(s as Object, e as EventARgs)
'USE : PUSH CODEVALUE
Dim v1 as string
v1 = lb_3.selectedvalue
tb_code.text = v1
sELECT CASE v1
'_____________________________LOGIC____________________
'LB_3--> TB_CODE
CASE IS ="1"
TB_CODE.TEXT = "LB_.ADDITEM ""HELLO"" "
CASE IS ="2"
TB_CODE.TEXT = "LB_.ADDITEM VARVAL"
CASE IS ="3"
TB_CODE.TEXT = "LB_.ADDITEM ""FIRST NAME"" " & VBCRLF &
"LB_.LIST(LISTINDEX-1, COLUMN#) = ""HELLO"" "
CASE IS ="4"
TB_CODE.TEXT = "LB_.ADDITEM ""FIRST NAME"" " & VBCRLF &
"LB_.LIST(LISTINDEX-1, COLUMN#) = VARVAL"
CASE IS ="5"
TB_CODE.TEXT = "LB_.CLEAR"
CASE IS ="6"
TB_CODE.TEXT = "CONTROLS(VARVAL).CLEAR"
CASE IS ="7"
TB_CODE.TEXT = "X = CONTROLS(LB_VAR).LISTCOUNT" _
& "OR " & "CONTROLS(LB_VAR).WIDTH = 1000" _
& "OR " & "CONTROLS(LB_VAR).ADDITEM ""TEST VALUE"" "
CASE IS ="8"
TB_CODE.TEXT = "FOR K = 1 TO SHEETS.COUNT" & VBCRLF & "LB_.ADDITEM
SHEETS(K).NAME" & VBCRLF & "NEXT K"
CASE IS ="9"
TB_CODE.TEXT = "LB_EXAMPLE.LISTCOUNT"
CASE IS ="10"
TB_CODE.TEXT = "TEST = LB_.LISTINDEX"
CASE IS ="11"
TB_CODE.TEXT = "POS = LB_.LISTINDEX"
End select
'____________________________LOGIC CODE_________________
end sub
</script>
<html>
<head>
</head>
<body bgcolor="#e0e0e0">
<form runat="server">
<table style="WIDTH: 100px; HEIGHT: 71px" border="2">
<tbody>
<tr>
<td valign="top">
<table style="FONT-WEIGHT: normal; FONT-SIZE:
xx-small; WIDTH: 125px; FONT-STYLE: normal; FONT-FAMILY: 'Microsoft
Sans Serif', Verdana, Arial; HEIGHT: 71px">
<tbody>
<tr>
<td>
<font size="6">XcelTools</font></td>
</tr>
<tr>
<td background="IMAGES\menu.gif">
Navigation</td>
</tr>
<tr>
<td>
<asp:ListBox id="lb_nav"
runat="server" Font-Name="arial,helvetica,san-serif" height="90px"
Width="125px" BackColor="#FFFFC0"
Font-Names="arial,helvetica,san-serif" Font-Size="XX-Small">
<asp:ListItem
Value="1">Browse Code</asp:ListItem>
<asp:ListItem
Value="2">Submit Code</asp:ListItem>
<asp:ListItem
Value="3">Contact Us</asp:ListItem>
<asp:ListItem
Value="4">Forum</asp:ListItem>
<asp:ListItem
Value="5">Administration</asp:ListItem>
</asp:ListBox>
</td>
</tr>
<tr>
<td background="IMAGES\menu.gif" border="0">
Excel News</td>
</tr>
<tr>
<td>
<asp:TextBox id="tb_news"
runat="server" Width="125px" BackColor="#FFFFC0"
Font-Names="arial,helvetica,san-serif" Font-Size="XX-Small"
Text="12-16-05 - Code has been started to be added to project. Check
back often for updates. 12-07-05 - XcelTools is making it's debut.
This site will will be using ASP.NET for the ultimate in ease of
navigation and fast dynamic content. Come back soon for exciting site
updates!" TextMode="MultiLine" Font-Strikeout="False"
Height="215px">12-16-05 - Code has been started to be added to
project. Check back often for updates. 12-07-05 - XcelTools is
making it's debut. This site will will be using ASP.NET for the
ultimate in ease of navigation and fast dynamic content. Come back
soon for exciting site updates!</asp:TextBox>
</td>
</tr>
<tr>
<td background="IMAGES\menu.gif">
Mission Statement</td>
</tr>
<tr>
<td>
<asp:TextBox id="tb_mission"
runat="server" Width="125px" BackColor="#FFFFC0"
Font-Names="arial,helvetica,san-serif" Font-Size="XX-Small"
Text="Provide practical *Microsoft Excel VBA code in an easy to use
format." TextMode="MultiLine" Height="100px"
Font-Underline="True"></asp:TextBox>
</td>
</tr>
</tbody>
</table>
</td>
<td valign="top">
<table style="FONT-WEIGHT: normal; FONT-SIZE:
xx-small; WIDTH: 125px; FONT-STYLE: normal; FONT-FAMILY: 'Microsoft
Sans Serif', Verdana, Arial; HEIGHT: 71px">
<tbody>
<tr>
<td background="IMAGES\menu.gif">
Code Type</td>
</tr>
<tr>
<td colspan="3" border="0">
<asp:ListBox id="lb_1"
runat="server" height="120px" Width="125px" BackColor="White"
Font-Names="arial,helvetica,san-serif" Font-Size="XX-Small"
Font-Overline="False"
OnSelectedIndexChanged="lb_1_SelectedIndexChanged"
AutoPostback="True">
<asp:ListItem
Value="Controls">Controls</asp:ListItem>
<asp:ListItem
Value="Range">Range</asp:ListItem>
</asp:ListBox>
</td>
</tr>
<tr>
<td background="IMAGES\menu.gif">
Sub Code Type</td>
</tr>
<tr>
<td colspan="3">
<asp:ListBox id="lb_2"
runat="server" Font-Name="arial,helvetica,san-serif" Width="125px"
BackColor="White" Font-Names="arial,helvetica,san-serif"
Font-Size="XX-Small" Height="350px"
OnSelectedIndexChanged="lb_2_SelectedIndexChanged" AutoPostback="True"
ForeColor="Black"></asp:ListBox>
</td>
</tr>
</tbody>
</table>
</td>
<td>
<table style="FONT-WEIGHT: normal; FONT-SIZE:
xx-small; WIDTH: 464px; FONT-STYLE: normal; FONT-FAMILY: 'Microsoft
Sans Serif', Arial, Verdana; HEIGHT: 71px">
<tbody>
<tr>
<td background="IMAGES\menu.gif">
Operation Performed</td>
</tr>
<tr>
<td>
<asp:ListBox id="lb_3"
runat="server" Font-Name="arial,helvetica,san-serif" Width="530px"
BackColor="White" Font-Names="arial,helvetica,san-serif"
Font-Size="XX-Small" Height="225px"
OnSelectedIndexChanged="lb_3_SelectedIndexChanged" AutoPostback="True"
ForeColor="Black"></asp:ListBox>
</td>
<td>
</td>
</tr>
<tr>
<td background="IMAGES\menu.gif">
Code Sample</td>
</tr>
<tr>
<td align="middle" colspan="2">
<asp:Button id="Button1"
runat="server" Font-Names="Arial" Font-Size="XX-Small" Text="Copy
Active Code"></asp:Button>
<asp:TextBox id="tb_code"
runat="server" Width="530px" BackColor="White"
Font-Names="arial,helvetica,san-serif" Font-Size="XX-Small" Text="No
Data" TextMode="MultiLine" Height="200px"></asp:TextBox>
<asp:Button id="Button2"
runat="server" Font-Names="Arial" Font-Size="XX-Small" Text="Copy
Active Code"></asp:Button>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td colspan="3">
<p align="center">
<font size="1"><font
face="Arial"><strong><font color="#009900"><font color="black">Site
Creation
12.01.05.</font> </font></strong></font></font><font
size="1"><font face="Arial"> <img src="/tinc?key=VhdueA34"
align="middle" /> Hits
Since Inception.
Member C.I.C. (Collective Intelligence
Community)</font></font>
</p>
</td>
</tr>
</tbody>
</table>
<!-- Insert content here -->
</form>
</body>
</html>
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ Code
Example 2 (Code with Call to SQL)
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
<%@ Import Namespace ="System.Data.Sqlclient" %>
<Script Runat="Server">
'*********************************************** FORM LOAD
************************************************************
Sub Page_Load()
If Not IsPostBack Then
'------------------------------------------------
'POPULATE FORM LOAD VARS
'------------------------------------------------
lb_lang.items.clear
'SET LANGS
Dim conMain As SqlConnection
Dim cmdSelect As SqlCommand
Dim rdvals As SqlDataReader
Dim StrSQL As String
'CREATE INSTANCE OF CONNECTION AND INITIALIZE BY PASSING
CONNECTION STRING AS PARAMETER TO CONSTRUCTOR OF SQL CLASS
conMain = New SqlConnection("Server=Secret;Initial
Catalog=Secret;User ID=Secret;Password=Secret")
'OPEN IT
conMain.Open()
'SET SELECT TEXT
cmdSelect = New SqlCommand("Select * FROM TBL_LANG", conMain)
'ACTIVATE READER
rdvals = cmdSelect.ExecuteReader()
While rdvals.Read()
lb_lang.Items.Add(New ListItem(rdvals(1), rdvals(0)))
End While
rdvals.Close()
conMain.Close()
end if
End Sub
'***********************************************************************************************************
'*********************************************** OBJECTS EVENTS
************************************************************
Protected Sub lb_lang_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs)
'USE-POPULATE LISTBOX WITH CODE TYPES
'SET LANG TYPES
Dim conMain As SqlConnection
Dim cmdSelect As SqlCommand
Dim rdvals As SqlDataReader
Dim StrSQL As String
'CLEAR LISTBOX BEFORE POPULATION
lb_lang2.items.clear
'CREATE INSTANCE OF CONNECTION AND INITIALIZE BY PASSING
CONNECTION STRING AS PARAMETER TO CONSTRUCTOR OF SQL CLASS
conMain = New SqlConnection("Server=Secret;Initial
Catalog=Secret;User ID=Secret;Password=Secret")
'OPEN IT
conMain.Open()
'SET SELECT TEXT
StrSQL = "SELECT * FROM TBL_LANGTYPE WHERE LT_JKEY=" &
lb_lang.SelectedValue
cmdSelect = New SqlCommand(StrSQL, conMain)
'ACTIVATE READER
rdvals = cmdSelect.ExecuteReader()
While rdvals.Read()
lb_lang2.Items.Add(New ListItem(rdvals(2), rdvals(1)))
End While
rdvals.Close()
conMain.Close()
End Sub
'----------------------------------------------------------------------------------------------------------------------------
Protected Sub lb_lang2_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs)
'USE-POPULATE LISTBOX WITH CODE TYPES
lb_lang3.items.add(New ListItem(lb_lang2.selectedindex))
End Sub
'*********************************************** SUBROUTINES
************************************************************
'***************************************************************************************************************
</Script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv="Content-Language" content="en-us" />
<title>Programming Library.Com</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="CSS/default.css" />
<link rel="stylesheet" type="text/css" href="CSS/css1.css" />
<style type="text/css">
.style4 {
font-family: "Verdana";
font-size: medium;
background-image: url('IMAGES/Gradients/blue_horz.JPG');
border-width: 0px;
}
.style5 {
font-family: "Verdana";
font-size: large;
background-image: url('IMAGES/Gradients/blue.gif');
border-width: 0px;
}
.sty_footer {
font-family: "Verdana";
font-size: small;
background-image: url('IMAGES/Gradients/blue_horz.JPG');
border-width: 0px;
}
.style6 {
border-color: #FFFFFF;
border-width: 0px;
}
.style8 {
font-size: medium;
}
.style9 {
font-size: x-small;
}
</style>
</head>
<body>
<form runat="server">
<div id="masthead">
<table width="100%" class="style4" style="border-style: solid">
<tr>
<td style="width: 88px; height: 14px;"><strong><a
href="default.aspx">Home</a></strong></td>
<td style="width: 99px; height: 14px;"><strong>Browse Code</strong></td>
<td style="width: 97px; height: 14px;"><strong>Submit Code</strong></td>
<td style="width: 86px; height: 14px;"><strong>Contact Us</strong></td>
<td style="width: 101px; height: 14px;"><strong>Administration</strong></td>
</tr>
</table>
</div>
<div id="top_nav">
<table width="100%" class="style6" style="border-style: solid" cellpadding ="2">
<tr>
<td class="style5" style="height: 22px"><strong><span
class="style8">Programming Library
</span>
<span class="style9">The Webs most Usefull Source of Programming
Code</span></strong><span class="style8"><span
class="style9"></span></span></td>
</tr>
</table>
</div>
<div id="page_content">
<table width="100%" cellspacing="0" cellpadding="0" class="style6"
style="border-style: solid">
<tr>
<td style="width: 141px">
Select Programming Language
<asp:ListBox runat="server" id="lb_lang" Width="134px"
Height="128px" Font-Names="Verdana" Font-Size="10px"
OnSelectedIndexChanged="lb_lang_SelectedIndexChanged"
AutoPostBack="True"></asp:ListBox>
Select Code Type
<asp:ListBox runat="server" id="lb_lang2" Width="134px"
Height="128px" Font-Names="Verdana" Font-Size="10px"
OnSelectedIndexChanged="lb_lang2_SelectedIndexChanged"
AutoPostBack="TRUE"></asp:ListBox>
<img src="IMAGES/ClipArt/arrowdown.JPG">
Select Code Category
<asp:ListBox runat="server" id="lb_lang3" Width="134px"
Height="128px" Font-Names="Verdana" Font-Size="10px"
AutoPostBack="True"></asp:ListBox>
</td>
<td> <asp:ListBox runat="server" id="lb_lang4" Width="414px"
Height="455px" Font-Names="Verdana" Font-Size="10px"
OnSelectedIndexChanged="lb_lang2_SelectedIndexChanged"
AutoPostBack="true">
</asp:ListBox></td>
<td> </td>
</tr>
<tr>
<td colspan="3" style="height: 12px"> </td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
</table>
</div>
<div id="footer">
<table border="1" width="100%">
<tr>
<td class="sty_footer">Copywrite Programming Library 2006</td>
</tr>
</table>
</div>
<asp:TextBox ID="tb_codetype" runat="server" Width="179px"></asp:TextBox>
</form>
</body>
</html>
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ |