在ListView中的Dropdownlist选定项更改时获取ListView项目的值

3

我有一个ListView和每行ListView中的DropDownList。您可以更改该DropDownlist中的分配人(DropDownList显示来自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>   

以下是我的代码后端:

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");
    }

我已经成功获取了更改DropDownList值的行的DataItemIndex,也能正确地触发OnSelectedIndexChanged事件,但我需要获取标签“customerId”的值,不知道如何获取。

先行致谢。

3个回答

4
你需要使用FindControl来获取对Label的引用,然后使用它的Text属性:
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>    

嗨,谢谢你的回答,但我几乎以与你完全相同的方式使用了FindControl。字符串customerID仍然为空。 - raven_977
@user1814545:改为设置Text属性即可。我已相应地编辑了我的回答。 - Tim Schmelter
抱歉,那是我的错。使用text属性,你的解决方案是完美的。非常感谢。 - raven_977

1
即使我的解决方案得到了回答,它仍然对我非常有效。
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的解决方案。


1
尝试直接从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
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接