Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: DropDownList OnSelectedItemChanged with SqlConnection


Message #1 by saturnius@g... on Wed, 26 Sep 2001 14:20:10 +0200 (MEST)
Hi, 

I would like to use the DropDownList OnSelectedItemChanged event but with a

SqlConnection it is not working only the first item-text is shown ("Select an

Item")- no further event is triggert and the DDL is always jumping back to

"Select an Item".

If I bind the data to a datagrid both colunms are appearing (sqlconn is OK)?

It also works if I add some items to the HTML-Code.



Any ideas? Cheers



NOT WORKING ASPX-Code:



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

Inherits="WebApplication20.WebForm1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

  <HEAD>

    <title></title>

<meta content="Microsoft Visual Studio.NET 7.0" name=GENERATOR>

<meta content="Visual Basic 7.0" name=CODE_LANGUAGE>

<meta content=JavaScript name=vs_defaultClientScript>

<meta content=http://schemas.microsoft.com/intellisense/ie5

name=vs_targetSchema>

  </HEAD>

<body>

<form id=Form1 method=post runat="server">

<asp:DropDownList id="DropDownList1" OnSelectedIndexChanged ="writelabel"

runat="server" AutoPostBack="True" DataValueField="ID" DataTextField="catx">

asp:DropDownList>

<asp:label id=Label1 runat="server"></asp:label></form>

  </body>

</HTML>



ASPX.VB FILE:



imports System.Data.SqlClient

Public Class WebForm1

    Inherits System.Web.UI.Page

    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

    Protected WithEvents DropDownList1 As

System.Web.UI.WebControls.DropDownList

            Protected WithEvents Label1 As System.Web.UI.WebControls.Label



#Region " Web Form Designer Generated Code "



    'This call is required by the Web Form Designer.

    <System.Diagnostics.DebuggerStepThrough()> Private Sub

InitializeComponent()



    End Sub



    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

       

        Dim sqlConn As New

SqlConnection(ConfigurationSettings.AppSettings("webapp"))

        Dim strArticleLevel As String = "SELECT ID,catx From catx"

        Dim cmdArticleLevel As New SqlCommand(strArticleLevel, sqlConn)

        sqlConn.Open()

        DropDownList1.DataSource 

cmdArticleLevel.ExecuteReader(CommandBehavior.CloseConnection)

        DropDownList1.DataTextField = "catx"

        DropDownList1.DataValueField = "ID"

        DropDownList1.DataBind()

        DropDownList1.Items.Insert(0, "Select an Item")

    End Sub



    Sub writelabel(ByVal sender As System.Object, ByVal e As

System.EventArgs)

        Label1.Text = DropDownList1.SelectedItem.Text

    End Sub



End Class



-- 

GMX - Die Kommunikationsplattform im Internet.

http://www.gmx.net



Message #2 by "Chris Scott" <chris@e...> on Fri, 28 Sep 2001 09:49:46 -0700
Hi,



I think your code would work if you only loaded the DDL when the page is not

the result of a postback (check IsPostBack before loading).



Chris



----- Original Message -----

From: <saturnius@g...>

To: "ASP+" <aspx@p...>

Sent: Wednesday, September 26, 2001 5:20 AM

Subject: [aspx] DropDownList OnSelectedItemChanged with SqlConnection





> Hi,

> I would like to use the DropDownList OnSelectedItemChanged event but with

a

> SqlConnection it is not working only the first item-text is shown ("Select

an

> Item")- no further event is triggert and the DDL is always jumping back to

> "Select an Item".

> If I bind the data to a datagrid both colunms are appearing (sqlconn is

OK)?

> It also works if I add some items to the HTML-Code.

>

> Any ideas? Cheers

>

> NOT WORKING ASPX-Code:

>

> <%@ Page Language="vb" AutoEventWireup="false"

Codebehind="WebForm1.aspx.vb"

> Inherits="WebApplication20.WebForm1"%>

> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

> <HTML>

>   <HEAD>

>     <title></title>

> <meta content="Microsoft Visual Studio.NET 7.0" name=GENERATOR>

> <meta content="Visual Basic 7.0" name=CODE_LANGUAGE>

> <meta content=JavaScript name=vs_defaultClientScript>

> <meta content=http://schemas.microsoft.com/intellisense/ie5

> name=vs_targetSchema>

>   </HEAD>

> <body>

> <form id=Form1 method=post runat="server">

> <asp:DropDownList id="DropDownList1" OnSelectedIndexChanged ="writelabel"

> runat="server" AutoPostBack="True" DataValueField="ID"

DataTextField="catx">

> asp:DropDownList>

> <asp:label id=Label1 runat="server"></asp:label></form>

>   </body>

> </HTML>

>

> ASPX.VB FILE:

>

> imports System.Data.SqlClient

> Public Class WebForm1

>     Inherits System.Web.UI.Page

>     Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

>     Protected WithEvents DropDownList1 As

> System.Web.UI.WebControls.DropDownList

>             Protected WithEvents Label1 As System.Web.UI.WebControls.Label

>

> #Region " Web Form Designer Generated Code "

>

>     'This call is required by the Web Form Designer.

>     <System.Diagnostics.DebuggerStepThrough()> Private Sub

> InitializeComponent()

>

>     End Sub

>

>     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

>

>         Dim sqlConn As New

> SqlConnection(ConfigurationSettings.AppSettings("webapp"))

>         Dim strArticleLevel As String = "SELECT ID,catx From catx"

>         Dim cmdArticleLevel As New SqlCommand(strArticleLevel, sqlConn)

>         sqlConn.Open()

>         DropDownList1.DataSource 

> cmdArticleLevel.ExecuteReader(CommandBehavior.CloseConnection)

>         DropDownList1.DataTextField = "catx"

>         DropDownList1.DataValueField = "ID"

>         DropDownList1.DataBind()

>         DropDownList1.Items.Insert(0, "Select an Item")

>     End Sub

>

>     Sub writelabel(ByVal sender As System.Object, ByVal e As

> System.EventArgs)

>         Label1.Text = DropDownList1.SelectedItem.Text

>     End Sub

>

> End Class

>

> --

> GMX - Die Kommunikationsplattform im Internet.

> http://www.gmx.net

>

>

>




>

>




  Return to Index