SelectedOdexChanged事件不会被触发,ASP.NET DropDownList

时间:2013-07-01 18:14:20

标签: asp.net vb.net gridview datagrid

我有一个带有1个DataGrid的Windows窗体,其中一个列中有一个Dropdownlist。

我还在此DataGrid之外有另一个dropDownlist。

这两个下拉列表都绑定到同一个数据集,并且都填充了相同的项目。

两个DropDowns都将其Autopostbacks设置为true。

问题仅适用于Datagrid之外的下拉列表SelectedIndexChange事件被触发:

(我在SO上看过多个类似的问题,但没有一个建议对我有用。所以我真的很感激你能不能在这里帮助我。

Protected Sub ABCD(ByVal sender As System.Object, ByVal e As System.EventArgs)    


End Sub

对于两个下拉列表:AutoPostBack =“True”

这是Vb代码:

Imports System.Data.OleDb
Public Class WebForm1
    Inherits System.Web.UI.Page


    Protected WithEvents dg As New System.Web.UI.WebControls.DataGrid
    Private cnDB As New OleDbConnection
    Private ds As New DataSet
    Private daDB As New OleDbDataAdapter
    Protected allNames As New DataSet
    Protected MyDataSet As DataSet
    Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList
    Protected WithEvents DropDownList2 As System.Web.UI.WebControls.DropDownList
    Protected WithEvents ddlName As New System.Web.UI.WebControls.DropDownList

#Region " Web Form Designer Generated Code "

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

    End Sub

    '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



        MyDataSet = NameBudget()

        GridDataLoad()



    End Sub

    Protected Sub Grid_EditCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs)

        dg.EditItemIndex = e.Item.ItemIndex

        dg.DataSource = ds
        dg.DataBind()

    End Sub
    Protected Sub Grid_CancelCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs)

        dg.DataSource = ds.Tables(0).DefaultView
        dg.EditItemIndex = -1
        dg.DataSource = ds
        dg.DataBind()

    End Sub

    Protected Sub Grid_UpdateCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs)


    End Sub


    Private Function GridDataLoad()


        ddlName.DataSource = MyDataSet


        Dim i As Object = MyDataSet.Tables(0)
        ddlName.DataBind()


        DropDownList1.DataSource = MyDataSet
        DropDownList1.DataBind()

        Dim strCon As String = _
        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\AccessTestDataBases\TestDB.mdb; "
        Dim cnDB As New OleDbConnection(strCon)
        cnDB.Open()
        daDB = New OleDbDataAdapter("Select * from [Persons]", cnDB)

        daDB.Fill(ds, "tbl1")




        Dim j As Object = ds.Tables(0)

        dg.DataSource = ds
        dg.DataBind()


        cnDB.Close()

        Dim ii As Object = ds.Tables(0)





    End Function


    Protected Function NameEditable(ByVal n As String) As Boolean
        Return True
    End Function

    Protected Function NameBudget() As DataSet

        Dim strCon As String = _
               "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\AccessTestDataBases\TestDB.mdb; "
        Dim cnDB As New OleDbConnection(strCon)
        cnDB.Open()
        daDB = New OleDbDataAdapter("Select ID,Name from [Persons]", cnDB)

        Dim ds As New DataSet

        daDB.Fill(ds, "tbl1")
        cnDB.Close()


        Return ds


    End Function


    Sub SetDefaultListItem(ByVal sender As Object, ByVal e As System.EventArgs)
        '*************************************************************************
        '* Use this sub to set the Default List for DropDown Listboxes           *
        '*************************************************************************
        Try
            If Len(sender.DefaultValue) > 0 Then
                If sender.Items.FindByValue(sender.DefaultValue).ToString.Length > 0 Then
                    sender.Items.FindByValue(sender.DefaultValue).Selected = True
                End If
            End If
        Catch ex As System.Exception
            'Throw New System.Exception(ex.ToString())
        End Try

    End Sub



    Protected Sub ABCD(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

End Class

以下是datagrid和Dropdownlists的HTML:

<Columns>

   <ASP:ButtonColumn Text="Delete" CommandName="Delete"></ASP:ButtonColumn>

   <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel"
  EditText="Edit"></asp:EditCommandColumn>

  <ASP:TemplateColumn HeaderText="Name" SortExpression="FY" HeaderStyle-HorizontalAlign="center" HeaderStyle-Wrap="True">

  <ItemStyle Wrap="false" HorizontalAlign="left" />
  <ItemTemplate>
    <ASP:Label ID="Name" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' runat="server"/>
</ItemTemplate>
<EditItemTemplate>
  <ASP:DropDownList id="DropDownlist2" datasource="<%# MyDataSet %>" DataTextField= "Name" DataValueField="ID" runat="server" AutoPostBack="True"  OnSelectedIndexChanged="ABCD">
  </ASP:DropDownList>
</EditItemTemplate>

</ASP:TemplateColumn>

</Columns>


<asp:DropDownList id="DropDownList1" 
    datasource="<%# MyDataSet %>" DataTextField= "Name" 
         DataValueField="ID" runat="server" AutoPostBack="True"
    OnSelectedIndexChanged="ABCD">
</asp:DropDownList>

0 个答案:

没有答案
相关问题