HardCode Posted August 27, 2004 Posted August 27, 2004 I have a simple test ASP.NET page, using a DropDownList to select the WHERE clause to retrieve data into a datagrid: <%@ 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 Quote
eramgarden Posted August 27, 2004 Posted August 27, 2004 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. Quote
HardCode Posted August 27, 2004 Author Posted August 27, 2004 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). Quote
Arch4ngel Posted August 27, 2004 Posted August 27, 2004 (edited) Try this... 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] Edited August 27, 2004 by Arch4ngel Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
Arch4ngel Posted August 27, 2004 Posted August 27, 2004 You must add it after DataBinding. (at least it's what I do) Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.