OnSelectedIndexChanged不会首先进行选择,但会在后续选择中进行

时间:2012-03-24 21:22:00

标签: asp.net vb.net

我在数据网格中有一个列表框,应该在选择更改时更新,但确实如此,但不是第一次尝试。只有在它回发后,在第一次点击后它才会按预期工作。任何帮助赞赏。

这是带有列表框的数据网格的前端部分。

<asp:TemplateColumn HeaderText="Qty">
                <ItemStyle HorizontalAlign="Center" Wrap="False" CssClass="grid" />
                <HeaderStyle HorizontalAlign="Center" ForeColor="Black" Font-Bold="true" CssClass="grid" width="30" />
                <ItemTemplate>
                    <asp:ListBox ID="lstQty" rows="1" runat="server" AutoPostBack="True" EnableViewState="True" OnSelectedIndexChanged="lstQtyUpdate" />
                </ItemTemplate>
            </asp:TemplateColumn>

这是页面加载部分:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Try
        If Not IsPostBack Then
            LoadCart()
            cartList.Columns(0).Visible = False
            If (cartList.Items.Count = 0) Then
                cartList.Visible = False
                lblEmptyMsg.Visible = True
            Else
                cartList.Visible = True
                lblEmptyMsg.Visible = False
            End If
        End If
    Catch ex As Exception
        Errorlog(ex, "Cart.Page_Load()")
    End Try
End Sub

这是使用onselectedindexchanged:

调用的子
Protected Sub lstQtyUpdate(sender As Object, e As System.EventArgs)
    Dim lb As New ListBox
    lb = CType(sender, ListBox)
    Dim thisID As String = lb.ClientID
    Dim oiQty As Integer = ComFunctions.ConvertToInt(lb.SelectedItem.Value)
    Dim oiID As Integer = 0
    For Each item As DataGridItem In cartList.Items
        lb = CType(item.FindControl("lstQty"), ListBox)
        If (thisID = lb.ClientID) Then
            oiID = ComFunctions.ConvertToInt(item.Cells(0).Text)
            Exit For
        End If
    Next.....

这是datagrid的绑定,可能是罪魁祸首。

Private Sub cartList_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles cartList.ItemDataBound
    Try
        Dim rbd As ImageButton
        Dim lst As ListBox
        Dim id As Integer = 0
        Dim evTitle As String = String.Empty
        Dim evImage As String = String.Empty
        Dim capacity As Integer = 0
        Dim soldseats As Integer = 0
        Dim seatsleft As Integer = 0
        Dim evdate As String = String.Empty
        Dim evtimestart As String = String.Empty
        Dim evtimeend As String = String.Empty
        Dim EditLink As String = String.Empty
        Dim DeletedLink As String = String.Empty
        If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
            id = DataBinder.Eval(e.Item.DataItem, "oi_id")
            evTitle = ComFunctions.ConvertToStr(DataBinder.Eval(e.Item.DataItem, "title"))
            evImage = ComFunctions.ConvertToStr(DataBinder.Eval(e.Item.DataItem, "image"))
            evdate = ComFunctions.ConvertToDate(DataBinder.Eval(e.Item.DataItem, "eventsdatestart"))
            capacity = ComFunctions.ConvertToStr(DataBinder.Eval(e.Item.DataItem, "capacity"))
            seatsleft = (capacity - soldseats)
            evtimestart = ComFunctions.Format_Time((DataBinder.Eval(e.Item.DataItem, "eventsdatestart")))
            evtimeend = ComFunctions.Format_Time((DataBinder.Eval(e.Item.DataItem, "eventsdateend")))
            Dim obj_DATA_Capacity As New DATA_Events()
            soldseats = obj_DATA_Capacity.GetSeatsSold(id)
            e.Item.Cells(0).Text = id
            e.Item.Cells(1).Text = evTitle & "<br />" & evdate & " " & evtimestart & " - " & evtimeend
            e.Item.Cells(2).Text = "<img src=""" & AppSettings("Events_ImagePath") & "/Thumb/" & evImage & """ width=""100"" />"
            e.Item.Cells(3).Text = "$" & ComFunctions.ConvertToDecimal(DataBinder.Eval(e.Item.DataItem, "oi_price"), 2)
            lst = CType(e.Item.FindControl("lstQty"), ListBox)
            If seatsleft > 0 Then
                'lst.Items.Add(0)
                For I = 1 To seatsleft
                    lst.Items.Add(I)
                Next
            End If
            lst.ID = id
            lst.SelectedValue = ComFunctions.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "oi_qty"))
            rbd = CType(e.Item.FindControl("DeleteThis"), ImageButton)
            rbd.CommandArgument = id
        End If

    Catch ex As Exception
        Errorlog(ex, "quickCart.cartList_ItemDataBound()")
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

我不确定为什么它第一次不起作用,顺便说一句,“不起作用”究竟是什么意思?但除此之外,您获取当前DataGridItem中第一个单元格文本的方法很奇怪。

这更直接:

Dim lb = DirectCast(sender, ListBox)
Dim item = DirectCast(lb.NamingContainer, DataGridItem)
Dim oiID = Int32.Parse(item.Cells(0).Text)

也许它有助于让它发挥作用。