2011-11-08 66 views
1

如果我有以下ListView,我如何将SelectedIndexChanged事件侦听器附加到DropDownList,以便我可以对各个对象执行命令?想象一下,我有一个新用户列表,我想通过从DropDownList中选择组来添加他们到一个用户组。访问控件内部ASP.NET视图控件(事件处理)

<asp:ListView ID="NewUsers" runat="server" DataSourceID="NewUsersSDS" DataKeyNames="ID"> 
    <LayoutTemplate> 
     <asp:Table ID="groupPlaceholder" runat="server"><asp:TableRow></asp:TableRow></asp:Table> 
    </LayoutTemplate> 
    <GroupTemplate> 
     <asp:TableCell ID="itemPlaceholder" runat="server"></asp:TableCell> 
    </GroupTemplate> 
    <ItemTemplate> 
     <asp:Table ID="NewUsersTable" runat="server" Width="32%" CssClass="inlineTable"> 
      <asp:TableRow> 
       <asp:TableCell Width="100px"><%# Eval("FullName").ToString.Trim()%></asp:TableCell> 
       <asp:TableCell> 
        <asp:HiddenField ID="RowIndex" runat="server" Value="<%# Container.DisplayIndex %>" /> 
        <asp:DropDownList ID="UserGroupSelect" runat="server" DataSourceID="UserGroupSelectSDS" DataValueField="ID" DataTextField="UserGroup" 
         OnSelectedIndexChanged="UserGroupSelect_SelectedIndexChanged" AutoPostBack="True"> 
        </asp:DropDownList> 
       </asp:TableCell> 
      </asp:TableRow> 
     </asp:Table> 
    </ItemTemplate> 
</asp:ListView> 

我一直在访问__View控件内部控件的问题。我在几个地方看过,你可以通过NewUsers.FindControl([ControlID as String])访问它们,但这似乎不适合我。我想这就是所谓的动态控制?不太确定,感觉有点失落。

与往常一样,您的帮助非常感谢。 ;)

附加信息/代码

'Now working code, thanks to James :) 
Protected Sub ItemBind(ByVal sender As Object, ByVal e As ListViewItemEventArgs) Handles NewUsers.ItemDataBound 
    Dim lv As ListView = DirectCast(sender, ListView) 
    If e.Item.ItemType = ListViewItemType.DataItem Then 
     lv.DataKeys(e.Item.DataItemIndex).Value.ToString() 'get the datakey 
    End If 
End Sub 
    Protected Sub UserGroupSelect_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) 
    Dim RowIndex As Integer = CInt(DirectCast(DirectCast(sender, DropDownList).Parent.FindControl("RowIndex"), HiddenField).Value) 
    Dim pk As Integer = CInt(NewUsers.DataKeys(RowIndex)("ID")) 
    Try 
     MessageBox("Update key " + pk.ToString, "Update Key") 'Custom js "alert" box function 
    Catch ex As Exception 
     MessageBox("Something went wrong, is the update key empty?") 
    End Try 
End Sub 

回答

2

要访问一个ListView(或任何数据绑定控件控件),您需要在项目/行使用FindControl

ListViewItem item = ListView1.Items[0]; 
if (item != null) 
{ 
    DropDownList ddl = item.FindControl("DropDownList1") as DropDownList; 
    if (ddl != null) 
    { 
     string value = ddl.SelectedValue; 
    } 
} 

至于附加SelectedIndexChanged事件,你可以这样做:

<ItemTemplate> 
    <asp:DropDownList ID="DropDownList1" runat="server" 
     AutoPostBack="true" 
     OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> 
    </asp:DropDownList> 
    <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Container.DisplayIndex %>' /> 
</ItemTemplate> 

代码隐藏:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    int rowIndex = Convert.ToInt32(((HiddenField)((DropDownList)sender).Parent.FindControl("HiddenField1")).Value); 

    ListViewItem item = ListView1.Items[rowIndex]; 
    if (item != null) 
    { 
     //your logic here 
    }   
} 

要检索datakey:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    int rowIndex = Convert.ToInt32(((HiddenField)((DropDownList)sender).Parent.FindControl("HiddenField1")).Value); 

    int pk = (int)ListView1.DataKeys[rowIndex]["PrimaryKey"];  
} 
+0

嗯,那么,这是否代码顶块去了?在ListView的'ItemDataBound'事件里面?因为'DropDownBox'控件当然没有'ItemCommand'属性。我仍然无法正常工作。在我尝试了更多的想法之后,我会稍微发布一些代码。 – Chiramisu

+0

是的,'ItemDataBound'对它来说是个好地方。显然,你不需要获得'ListViewItem'的部分,因为你已经拥有它了。 –

+0

对不起,您需要添加'AutoPostBack =“true”' –