468,532 Members | 1,637 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,532 developers. It's quick & easy.

Databound DropDownList and SelectedIndexChanged Event issues

Hi, I hope some one can help with this. I have a basic webform with 2
DropDownLists and a single DataGrid. What I am trying to do is populate the
first DDList from a dataset on Form_Load. I then want to use this 1st DDList
to populate the 2nd DDList via the SelectedIndexChange Event.

So far so good. all works up to this point.

The next thing I'm trying to do is to use the 2nd DDList value in a queery
to populate the Datagrid also via the SelectedIndexChange Event. This is
where I run into the problem: When the 2nd DDList does it's postback it
seems to loose it's Values and the SelectedIndexChanged Event won't fire.

If I pre-populate the 2nd DDList manually from IDE before runtime, then the
problem disappears.

any Ideas anyone?

Thanks

WebForm1.aspx :
***********

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"
Inherits="shop.WebForm1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DropDownList id="DDL_Top_Category" style="Z-INDEX: 101;
LEFT: 24px; POSITION: absolute; TOP: 8px"
runat="server" AutoPostBack="True"
Width="112px"></asp:DropDownList>
<asp:DataGrid id="DG_Selections" style="Z-INDEX: 103; LEFT:
16px; POSITION: absolute; TOP: 64px"
runat="server"></asp:DataGrid>
<asp:DropDownList id="DDL_Sub_Category" style="Z-INDEX: 102;
LEFT: 24px; POSITION: absolute; TOP: 32px"
runat="server" Width="112px"
AutoPostBack="True"></asp:DropDownList></form>
</body>
</HTML>
WebForm1.aspx.vb :
**************

Imports System.Data.SqlClient

Public Class WebForm1
Inherits System.Web.UI.Page
Public DS_Selections As New DataSet

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents DDL_Top_Category As
System.Web.UI.WebControls.DropDownList
Protected WithEvents DDL_Sub_Category As
System.Web.UI.WebControls.DropDownList
Protected WithEvents DG_Selections As System.Web.UI.WebControls.DataGrid

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack = True Then

'Populate DDL_Top_Category

Dim ds1 As New DataSet

Dim sConn As SqlConnection = New SqlConnection("Data
Source=*****;Initial Catalog=*****;User Id=*****;Password=*****")
Dim sq1 As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM
Product_Top_Category", sConn)

sq1.Fill(ds1)

DDL_Top_Category.DataSource = ds1.Tables(0)
DDL_Top_Category.DataTextField =
ds1.Tables(0).Columns(1).ColumnName.ToString
DDL_Top_Category.DataValueField =
ds1.Tables(0).Columns(2).ColumnName.ToString
DDL_Top_Category.DataBind()

End If
End Sub

Private Sub DDL_Top_Category_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
DDL_Top_Category.SelectedIndexChanged

'Fill DDL_Sub_Category depending on DDL_Top_Category Selection
DDL_Sub_Category.Items.Clear()

Dim ds1 As New DataSet

Dim sConn As SqlConnection = New SqlConnection("Data
Source=*****;Initial Catalog=*****;User Id=*****;Password=*****")
Dim sq1 As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM
Product_Sub_Category WHERE Category_Level='" & DDL_Top_Category.SelectedValue
& "'", sConn)

sq1.Fill(ds1)

DDL_Sub_Category.DataSource = ds1.Tables(0)
DDL_Sub_Category.DataTextField =
ds1.Tables(0).Columns(1).ColumnName.ToString
DDL_Sub_Category.DataValueField =
ds1.Tables(0).Columns(2).ColumnName.ToString
DDL_Sub_Category.DataBind()

End Sub

Private Sub DDL_Sub_Category_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
DDL_Sub_Category.SelectedIndexChanged

'Fill DG_Selections depending on DDL_Sub_Category Selection
Dim sConn As SqlConnection = New SqlConnection("Data
Source=*****;Initial Catalog=*****;User Id=*****;Password=*****")
Dim sq1 As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM
Products WHERE Product_ID='" & DDL_Sub_Category.SelectedValue & "' AND
Product_In_Use=1", sConn)

sq1.Fill(DS_Selections)

DG_Selections.DataSource = DS_Selections
DG_Selections.DataBind()

End Sub
End Class

--
Before you judge a man, walk a mile in his shoes. Then who cares! You are a
mile away and you have his shoes.
Nov 21 '05 #1
0 2041

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Mike D | last post: by
1 post views Thread by sergeyr3 | last post: by
4 posts views Thread by David | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.