•      Powered by
 

Automatically Submit an Page
using ASP.NET DropDownList Box

Click here to view demo

This example shows how to populate a dropdownlist box from a database and have it automatically submit the page when selected.  This specific example does a select against a Pubs database and write out a message it was selected with a timestamp.   This example requires MSDE or SQL Server with the pub's database to loaded.

Here is the code

<%@ Page EnableSessionState="False" EnableViewState="True" Debug="False" Trace="False" strict="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<script language=VB runat=server>
Sub Page_Load(Sender As Object, E As EventArgs)
    If Not IsPostBack Then
        Dim myConnection As SqlConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
        Dim mySQL as string = "Select au_id, au_lname, au_fname from Authors"
        Dim myCmd as New SQLCommand(mySQL,myConnection)
        myConnection.Open()
        MyDropDownList.DataSource = myCmd.ExecuteReader()
        myDropDownList.DataBind()
        myConnection.Close()
    End If
End Sub

Sub Index_Changed(sender As Object, e As EventArgs)
    lblDisplayMessage.Text = "Form Was Submitted Automatically on Selection of DropDown: " & DateTime.Now.ToString()
End Sub
</script>
<html>
    <head><title>View Specific Profession Info</title></head>
        <body>
        <asp:label id="lblDisplayMessage" runat="server" />
        <form name="frmValues" runat="server">
            <asp:dropdownlist
                DataValueField = "au_lname"
                DataTextField = "au_id"
                id="MyDropDownList"
                OnSelectedIndexChanged="Index_Changed"
                autopostback="true"
            runat="server" />           
        </form>
        </body>
</html>
 

tio

Terms of Use | Privacy Statement ©2005-2006 IISLogs.com. All rights reserved - Powered by IIS7 - info @ www.IIS.net