I have a simple test ASP.NET page, using a DropDownList to select the WHERE clause to retrieve data into a datagrid:
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
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