Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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.

Posted

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).

Posted (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 by Arch4ngel

"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

Posted
You must add it after DataBinding. (at least it's what I do)

"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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...