View Single Post
  #1 (permalink)  
Old May 25th, 2004, 01:38 PM
nyclimber nyclimber is offline
Registered User
 
Join Date: May 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Selecting an Item from a databound listbox

I have created a web page (using web matrix) that contains a listbox (of city names) that is bound to a datatable. I then want to be able to pre-select a city name as the default (see code below).

It appears that the databind() function does not fully let go of the listbox control on page_load(). Even when I place my selection code in page_prerender(), I am unable to default to the city name.

As a test, I manually loaded the listbox and the code worked fine and I was able to default to the city name.

Any ideas?

Thank you in advance.


<%@ Page Language="VB" smartnavigation="True" autoeventwireup="False" Inherits="System.Web.UI.Page" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.Web.UI" %>
<%@ import Namespace="System.Web.UI.Page" %>
<%@ import Namespace="System.Web.UI.WebControls" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

         Dim adoDtCity As New DataTable

         Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '// Load listbox with static values
                'lstCity.Items.Add(New ListItem("Amlin", 1))
                'lstCity.Items.Add(New ListItem("Bexley", 2))
                'lstCity.Items.Add(New ListItem("Blacklick", 3))
                'lstCity.Items.Add(New ListItem("Canal Winchester", 4))
                'lstCity.Items.Add(New ListItem("Columbus", 5))
                'lstCity.Items.Add(New ListItem("Dublin", 6))
                'lstCity.Items.Add(New ListItem("Gahanna", 7))
                'lstCity.Items.Add(New ListItem("Galloway", 8))

            '// Load listbox from database
                adoDtCity = GetCityTbl()
                lstCity.DataSource = adoDtCity
                lstCity.DataTextField = "name"
                lstCity.DataBind()
         End Sub

         Public Sub Page_PreRender(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
                lstCity.SelectedIndex = lstCity.Items.IndexOf(lstCity.Items.FindByText("Co lumbus"))
         End Sub

         Public Function GetCityTbl() As DataTable
             Dim adoCn As SqlConnection
             Dim adoCd As SqlCommand
             Dim adoDr As SqlDataReader
             Dim adoDt As New DataTable()
             Dim adoParm As SqlParameter
             Dim adoRow As DataRow

             Try
                 adoDt.Columns.Add("city_id", System.Type.GetType("System.String"))
                 adoDt.Columns.Add("name", System.Type.GetType("System.String"))

                 adoCn = New SqlConnection(ConfigurationSettings.AppSettings("S QL_LOGIN"))
                 adoCd = New SqlCommand(ConfigurationSettings.AppSettings("spAl lCity"), adoCn)
                 adoCd.CommandType = CommandType.StoredProcedure
                 adoParm = adoCd.Parameters.Add(New SqlParameter("@PgmID", SqlDbType.VarChar, 4))
                 adoParm.Direction = ParameterDirection.Input
                 adoParm.Value = ConfigurationSettings.AppSettings("PGM")
                 adoCn.Open()
                 adoDr = adoCd.ExecuteReader(CommandBehavior.CloseConnectio n)

                 While adoDr.Read
                     adoRow = adoDt.NewRow()
                     adoRow(0) = adoDr("city_id")
                     adoRow(1) = adoDr("name")
                     adoDt.Rows.Add(adoRow)
                 End While
             Catch
                 'Error handling goes here
             Finally
                 adoCn.Close()
             End Try

             Return adoDt
         End Function

</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR" />
    <meta content="Visual Basic 7.0" name="CODE_LANGUAGE" />
    <meta content="JavaScript" name="vs_defaultClientScript" />
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema" />
    <link href="Includes/cnas.css" rel="stylesheet" />
    <script language="javascript" src="Includes/inc_cmn_pg.js"></script>
</head>
<body>
    <form id="Intake" method="post" runat="server">
        <div id="contentstart" style="HEIGHT: 100px">
            <table cellspacing="0" cellpadding="0" width="760" border="0">
                <tbody>
                    <tr><td>&nbsp;</td></tr>
                    <tr>
                        <td>
                            <asp:ListBox id="lstCity" runat="server" rows="1" Font-Size="Smaller" Font-Names="Arial"></asp:ListBox>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <asp:Label id="lblMsg" runat="server"></asp:Label>
    </form>
</body>
</html>