DropDownList Not Changing Properly

HardCode

Freshman
Joined
Apr 2, 2004
Messages
49
I have a simple test ASP.NET page, using a DropDownList to select the WHERE clause to retrieve data into a datagrid:
Code:
<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Data" %>
<script runat="server">

    Private Const BASE_CONN As String = "<my valid connection string>"

    Public Sub GetStats()

        Dim oConn           As SqlConnection
        Dim oCommand        As SqlCommand
        Dim dtrReader       As SqlDataReader

        oConn = New SqlConnection(BASE_CONN)
        oConn.Open
        oCommand = New SqlCommand("GetStats",oConn)
        oCommand.CommandType = CommandType.StoredProcedure
        oCommand.Parameters.Add("@Study", cboStudy.SelectedItem.Text)
        dtrReader = oCommand.ExecuteReader()
        dgStats.DataSource = dtrReader
        dgStats.DataBind
        oConn.Close

    End Sub

    Public Sub GetStudies()

        Dim oConn           As SqlConnection
        Dim oCommand        As SqlCommand
        Dim dtrReader       As SqlDataReader

        oConn = New SqlConnection(BASE_CONN)
        oConn.Open
        oCommand = New SqlCommand("GetAvailableStudies",oConn)
        oCommand.CommandType = CommandType.StoredProcedure
        dtrReader = oCommand.ExecuteReader()
        cboStudy.DataSource = dtrReader
        cboStudy.DataTextField = "Study"
        cboStudy.DataBind()
        cboStudy.DataSource.Close
        oConn.Close

    End Sub

    Private Sub Page_Load()
        lblTitle.Text = "Please select a study:"
        Call GetStudies()
    End Sub

    Private Sub cboStudy_Change(sender As Object, e As System.EventArgs)
        lblTitle.Text = "Change Event Raised"
        Call GetStats()
    End Sub

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            <asp:Label id="lblTitle" runat="server" text="Label"></asp:Label>
        </p>
        <p>
            <asp:DropDownList id="cboStudy" runat="server" Width="371px" OnSelectedIndexChanged="cboStudy_Change" AutoPostBack="True"></asp:DropDownList>
        </p>
        <p>
        </p>
        <p>
            <asp:DataGrid id="dgStats" runat="server" CellSpacing="1" CellPadding="2" BackColor="PaleTurquoise"></asp:DataGrid>
        </p>
    </form>
</body>
</html>

The problem is, I had to set AutoPostBack to TRUE in order for the DropDown's Change event to raise, correct? The event raises, but each time I select a value in the DropDown, the DropDown re-sets itself to the first item in the list, right after the post back takes place. I have no idea why!

What I need is this:
* Select item in DropDown
* Raise the Change event to get the data for the grid automatically
 
I had a smiliar issue the other day and i set debug..i found out i needed to put "if not page.ispostback" in the page_load event:

Private Sub Page_Load()
lblTitle.Text = "Please select a study:"
if not page.ispostback() then
Call GetStudies()
end if

End Sub


try that.
 
Nice! That worked perfectly. Thanks.

While on the subject of DropDowns...how would I make it so the first value in the list is NOT selected when the page loads (i.e. it is blank).
 
Try this...
Visual Basic:
ddlTest.Items.Insert( 0 , new ListItem( "Please select a value...", "-1") )
After that... you could verify that the SelectedValue is not equal to -1 (or change it to any value you want...) if you're field is not optional.

There you go

[edit]This will require the following assembly : System.Web.UI.WebControls[/edit]
 
Last edited:
Back
Top