当前位置:  开发笔记 > 后端 > 正文

GridView中的DropDownList SelectedIndexChanged没有触发!

如何解决《GridView中的DropDownListSelectedIndexChanged没有触发!》经验,为你挑选了1个好方法。

我一直在寻找一个解决方案,但看到许多帖子告诉我如何做但但是当DropDownList被更改时我无法触发我的SelectedIndexChanged事件.

DropDownList AutoPostBack设置为True,我也跟着下面的帖子中的代码: 链接到post

这是我的代码:

.ASPX

    
    
    

        

        
            
                
            
        

        

        
            
                
            
        

        
            
                
                
            
        

        
            
                
            
        

        
            
                
            
        



    

.VB

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    If (Request.IsAuthenticated = False) Then
        Response.Redirect("~/admin/default.aspx")
    End If


    Dim keypadSQL As SqlConnection = New SqlConnection()
    keypadSQL.ConnectionString = ConfigurationManager.ConnectionStrings("connKeypad").ConnectionString()


    Dim cmdActive As SqlCommand = New SqlCommand()
    cmdActive.Connection = keypadSQL
    cmdActive.CommandText = "spCasesActive"
    cmdActive.CommandType = CommandType.StoredProcedure


    Dim daCases As SqlDataAdapter = New SqlDataAdapter
    daCases.SelectCommand = cmdActive

    Dim dsCases As DataSet = New DataSet()
    daCases.Fill(dsCases, "CaseList")

    Dim CaseTotal As Integer
    CaseTotal = dsCases.Tables(0).Rows.Count

    If CaseTotal = 1 Then
        iCaseTotal.InnerHtml = CaseTotal & " Case"
    Else
        iCaseTotal.InnerHtml = CaseTotal & " Cases"
    End If

    gvCases.DataSource = dsCases
    gvCases.DataBind()
    cmdActive.Dispose()


    If Page.IsPostBack Then

    End If

End Sub

Protected Sub gvCases_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvCases.RowDataBound

    If e.Row.RowType = DataControlRowType.Header Then

        gvCases.Columns(5).ItemStyle.Width() = "60"
        gvCases.Columns(6).ItemStyle.Width() = "70"

    End If

    If e.Row.RowType = DataControlRowType.DataRow Then

        Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView)

        Dim strClientName As String
        Dim clientName As Label
        strClientName = rowView("firstname") & " " & rowView("lastname")
        clientName = CType(e.Row.FindControl("clientName"), Label)
        clientName.Text = strClientName

        Dim strDateTime As String
        Dim dateTime As Label
        strDateTime = rowView("CaseSent")
        dateTime = CType(e.Row.FindControl("dateTime"), Label)
        dateTime.Text = FormatDateTime(strDateTime, DateFormat.ShortDate) & "
" & FormatDateTime(strDateTime, DateFormat.ShortTime) gvCases.Columns(3).ItemStyle.Font.Size = 8 gvCases.Columns(5).ControlStyle.CssClass = "btnEdit" gvCases.Columns(6).ControlStyle.CssClass = "btnSchedule" Dim intUserId As String intUserId = Convert.ToString(gvCases.DataKeys(e.Row.RowIndex).Value) Dim cmd As New SqlCommand("SELECT id, Firstname, Lastname, Firstname + ' ' + Lastname As FullName FROM [users_icon] ORDER BY Firstname, Lastname", New SqlConnection(ConfigurationManager.ConnectionStrings("connKeypad").ConnectionString())) cmd.Connection.Open() Dim ddlValues As SqlDataReader ddlValues = cmd.ExecuteReader() Dim iconUsers As DropDownList iconUsers = CType(e.Row.FindControl("iconUsers"), DropDownList) iconUsers.Style.Add("font-size", "11px") iconUsers.DataSource = ddlValues iconUsers.DataValueField = "id" iconUsers.DataTextField = "FullName" iconUsers.DataBind() Dim ListItem1 = New ListItem("Select Case Owner", "0") iconUsers.Items.Insert("0", ListItem1) iconUsers.AutoPostBack = True If IsDBNull(rowView("CaseOwner")) Then iconUsers.SelectedValue = 0 Else iconUsers.SelectedValue = rowView("CaseOwner") End If AddHandler iconUsers.SelectedIndexChanged, AddressOf iconUsers_SelectedIndexChanged cmd.Connection.Close() cmd.Connection.Dispose() Dim btnDetails As Button = CType(e.Row.FindControl("btnDetails"), Button) btnDetails.PostBackUrl = "~/admin/detail.aspx?uid=" & intUserId Dim LabelAddress As Button = CType(e.Row.FindControl("btnSchedule"), Button) LabelAddress.PostBackUrl = "~/admin/schedule.aspx?uid=" & intUserId End If End Sub Protected Sub iconUsers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Response.Write("Function Called") End Sub

谢谢你的帮助.J.



1> Ian Pugsley..:

有一些类似的问题(请参阅事件处理程序不使用AddHandler触发 并将事件分配给Repeater控件内的自定义控件),但您的特定情况看起来就像是要添加处理程序两次; 一次在标记中,一次在数据绑定上.

我将删除RowDataBound事件中的那个(因为它没有做任何事情,因为当你回发时处理程序将丢失,并且在事件实际触发后添加处理程序).另外,请确保将@Bala提及AutoPostBack.


啊 - 看起来你重新数据绑定每一行的每个下拉列表(甚至是回发),并且在处理事件之前你在Page_Load中调用`gvCases.DataBind()`.因此,当您重新数据绑定DropDownList时,您将清除同时触发的SelectedIndexChanged事件.你有没有理由在Page_Load中显式地使用DataBinding,而是将GridView的数据源设置为某个SQLDataSource并让它自己发生?
推荐阅读
135369一生真爱_890
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有