获取ListView下拉列表中selectedIndexChanged的ListView项的值

本文关键字:ListView 下拉列表 获取 selectedIndexChanged | 更新日期: 2023-09-27 17:54:21

我有一个ListView和下拉列表在每一行的ListView。你可以在下拉列表中更改指定的人员(下拉列表显示来自MySql数据库的数据)。

当更改人员时,我需要做一些数据库操作。为此,我需要知道在哪个ListView行上更改了人员,并且我需要知道条目的值。

这是我的aspx文件:

<asp:ListView ID="manageAssigneesListView" runat="server" DataKeyNames="" OnItemDataBound="OnDataBound">            
                <ItemTemplate>
                    <div class="summaryRow">
                        <asp:Label ID="customerId" runat="server"><%# Eval("customerId") %>&nbsp;</asp:Label>    
                        <div style="width:200px; margin: 0px 5px; float: left;"><%# Eval("lastName") %>, <%# Eval("firstName") %></div>
                        <div style="width:120px; margin: 0px 2px; float: left;">Nr.</div>
                        <div style="width:200px; margin-left: 50px; float: right;">
                            <asp:DropDownList runat="server" ID="selectAssignee" Width="196" AppendDataBoundItems="true" OnSelectedIndexChanged="selectAssignee_OnSelectedIndexChanged" AutoPostBack="true">
                            </asp:DropDownList>
                        </div>
                        <div class="clear"></div>
                    </div>
                </ItemTemplate>
            </asp:ListView>   

下面是我的codebehind:

protected void Page_Load(object sender, EventArgs e)
    {
        Helper.doAuth(Session, Response);
        if (!IsPostBack)
        {
            // load all customers without assignee
            MySqlCommand cmd = new MySqlCommand();
            MySqlConnection con = new MySqlConnection();
            con.ConnectionString = Helper.CONNECTION_STRING;
            cmd.CommandText = "SELECT customerId, lastName, firstName, assignee FROM customer WHERE assignee IS NULL ORDER BY customerId DESC";
            cmd.Connection = con;
            con.Open();
            MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            manageAssigneesListView.DataSource = ds;
            manageAssigneesListView.DataBind();
            con.Close();
        }
    }
    protected void OnDataBound(object sender, ListViewItemEventArgs e)
    {
        DropDownList selectAssignee = (e.Item.FindControl("selectAssignee") as DropDownList);
        MySqlCommand cmd = new MySqlCommand();
        MySqlConnection con = new MySqlConnection();
        con.ConnectionString = Helper.CONNECTION_STRING;
        cmd.CommandText = "SELECT * FROM user where role = " + Helper.ROLE_SALESPERSON;
        cmd.Connection = con;
        con.Open();
        MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        selectAssignee.DataSource = ds;
        selectAssignee.DataTextField = "emailAdress";
        selectAssignee.DataBind();
        con.Close();    
    }

    protected void selectAssignee_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList dropDownList = (DropDownList)sender;
        ListViewItem listView = (ListViewItem)dropDownList.NamingContainer;
        var test = listView.DataItem;
        string test2 = listView.FindControl("customerId").ToString();
        int rowIndex = (int)listView.DataItemIndex;
        Label lblMessage = (Label)listView.FindControl("customerId");
    }

我成功地获得了行DataItemIndex,我改变了下拉列表的值,下拉列表也正确地onSelectedIndexChanged,但我需要的是标签"customerId"的值,我不知道如何获得这个值。

Thanks in advance

获取ListView下拉列表中selectedIndexChanged的ListView项的值

您需要使用FindControl来获取对Label的引用,然后使用它的文本属性:

ListViewItem item = (ListViewItem)dropDownList.NamingContainer;
Label lbltCustomerID = (Label) item.FindControl("customerId");
string customerID = lblCustomerID.Text;

顺便说一下,ListViewItem.DataItem在回发时总是null

编辑:

应该设置标签的Text,而不是

<asp:Label ID="customerId" runat="server">
    <%# Eval("customerId") %>&nbsp;
</asp:Label>    

<asp:Label ID="customerId" runat="server"
   Text='<%# Eval("customerId") %>' >
</asp:Label>    

尝试直接从ListView中获取标签的值,如

        DropDownList dropDownList = (DropDownList)sender;
        ListViewItem listView = (ListViewItem)dropDownList.NamingContainer;
        var test = listView.DataItem;
        string test2 = listView.FindControl("customerId").ToString();
        int rowIndex = (int)listView.DataItemIndex;
        Label label = (Label)manageAssigneesListView.Items[rowIndex ].Controls[0];

 Label label = (Label)manageAssigneesListView.Items[rowIndex].FindControl("customerId");

并确保ListView不会在每次回发时都绑定

如果ListView加载在PageLoad上不要忘记添加它:

if (!IsPostBack)
    {
      //Load ListView
    }

即使它回答了,我的解决方案对我来说也很有效:

protected void myDdl_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList dropDownList = (DropDownList)sender;
        ListViewDataItem listView = (ListViewDataItem)dropDownList.NamingContainer;
        int rowIndex = listView.DataItemIndex;
        //Some logic
    }

不是我用ListViewDataItem代替Peasmaker的解决方案