在DataGrid(WebControl)中处理DropDownList事件的补充说明 Carlwave

由于DropDownList没有CommandName属性,所以不能用ItemCommand事件,不过你可以这样做:
在DataGrid的模板列中加入的DropDownList控件
<asp:DropDownList runat="server" id="ddl" AutoPostBack="True" OnSelectedIndexChanged="ddl_SelectedIndexChanged" />
然后你在.aspx.vb中加入一个函数
Protected Sub ddlOprStyle_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As EventArgs)'不可以使用private声明,必须用public和protected
        Dim DdList As DropDownList
        DdList 
= CType(sender, DropDownList)
        
Dim i As Int32
        
Dim DdListDg As DropDownList
        
'查询当前选择的ddlist
        For i = 0 To dgMsg.Items.Count - 1
            DdListDg 
= CType(dgMsg.Items(i).FindControl("ddl"), DropDownList)
            
If DdList.UniqueID.CompareTo(DdListDg.UniqueID) = 0 Then
'插入要处理的事件,如绑定等等
              End If
         
Next i
End Sub



上面的代码功能是获得当前响应的DropDownList在DataGrid中的行index值,并处理相关方法。
实现方法是通过循环判定.DataGrid中为每个子控件唯一指定的UniqueID值是否与当前选定的DropDownList的UniqueID相等。

2006-07-07:最近又找到一个更方便的方法,只要通过增加事件就可以了
看上面的代码,在
dim DdListDg as DropDownList
DdListDg=CType(dgMsg.Items(i).FindControl("ddl"),DropDownList)后面
增加事件
AddHandler DdListDg.SelectedIndexChanged, AddressOf Update

然后定义要执行的sub就行
Private Sub Update()
    ' 写要用的代码
End Sub
原文地址:https://www.cnblogs.com/Carlwave/p/329804.html