PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

Databound DropDownList and SelectedIndexChanged Event issues

 
 
=?Utf-8?B?VGFuZDM1MDA2?=
Guest
Posts: n/a
 
      21st Oct 2004
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">
<aspropDownList id="DDL_Top_Category" style="Z-INDEX: 101;
LEFT: 24px; POSITION: absolute; TOP: 8px"
runat="server" AutoPostBack="True"
Width="112px"></aspropDownList>
<aspataGrid id="DG_Selections" style="Z-INDEX: 103; LEFT:
16px; POSITION: absolute; TOP: 64px"
runat="server"></aspataGrid>
<aspropDownList id="DDL_Sub_Category" style="Z-INDEX: 102;
LEFT: 24px; POSITION: absolute; TOP: 32px"
runat="server" Width="112px"
AutoPostBack="True"></aspropDownList></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.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SelectedIndexChanged Event of Dropdownlist on a DLL.. =?Utf-8?B?ZGVuIDIwMDU=?= Microsoft ASP .NET 1 27th Apr 2006 11:01 AM
Databound Combobox doesn't call SelectedIndexChanged event Cory Burkhardt Microsoft Dot NET Framework Forms 0 1st Sep 2004 10:17 PM
DropdownList SelectedIndexChanged event Gary Microsoft ADO .NET 1 16th Aug 2003 07:23 PM
SelectedIndexChanged event in DropDownList Pepito Grillo Microsoft ASP .NET 0 28th Jul 2003 09:36 AM
Re: Dropdownlist SelectedIndexChanged event problem Robert Hanson Microsoft ASP .NET 0 10th Jul 2003 11:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:09 PM.