Link to home
Start Free TrialLog in
Avatar of MrDavidThorn
MrDavidThorn

asked on

.NET Combo box - post back on change

Hi I think its pretty straight forward what im trying to do - for a  aspx web page to post back the results of a selected combo box , I can do this with a button on the form but I would like to post back when the user has selected an item in the combo box, Iv seen other solutions that use a javascript on chnange event within the combo box but cant quite see how it works. Any help would be great!  
.net code

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim t As Integer
        For t = 1 To 12
            DropDownList1.Items.Add("test " & t)
        Next t
    End Sub
HTML code
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="KormaCom.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="DropDownList1" style="Z-INDEX: 101; LEFT: 152px; POSITION: absolute; TOP: 72px"
				runat="server" Height="32px" Width="224px"></asp:dropdownlist></form>
	</body>
</HTML>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jkofte
jkofte
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Set AutoPostback property of Combobox to true.
Avatar of Pratima
You want to post the seleted drop down value to next page or on same page button click ?

first of all you need to add postback condition in page_load
    Dim t As Integer
        If Page.IsPostBack = False Then
            For t = 1 To 12
                DropDownList1.Items.Add("test " & t)
            Next t
        End If

On same page on button click
  Response.Write(DropDownList1.SelectedItem.Value)
Do add If(!Ispostback) condition to your page load, so that combobox is loaded each time page postbacks.
Do add If(!Ispostback) condition to your page load, so that combobox is not loaded each time page postbacks.
to ensure that a dropdown selection Change triggers a postBack you should add the AutoPostback to true.

This ensures that every change will do a PostBack.

But knowing the standard use of page_Load you're filling there the dropdown.
So you should indeed use the ( Page.IsPostBack) property to check.
if this is false this is the initial call for the page and the controls must be initialised.

I hope this was clear and understandable ;-)

regards
poor beggar