【问题标题】:Get Value of ListView Item on selectedIndexChanged on Dropdownlist in ListView获取ListView中Dropdownlist的selectedIndexChanged上ListView项的值
【发布时间】:2014-12-30 17:50:58
【问题描述】:

我在该 ListView 的每一行中都有一个 ListView 和 DropDownList。您可以在该 DropDownlist 中更改指定人员(DropDownList 显示来自 MySql 数据库的数据)。

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

这是我的 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");
    }

我成功获取了行的 DataItemIndex 我更改了 DropDownList 的值,DropDownlist 也在正确触发 onSelectedIndexChanged,但我需要的是标签“customerId”的值,我不知道如何获取这个值。

提前致谢

【问题讨论】:

    标签: c# asp.net listview drop-down-menu


    【解决方案1】:

    您需要使用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 仍为空。
    • @user1814545:改为设置Text 属性。相应地编辑了我的答案。
    • 对不起,那是我的错。使用 text-property,您的解决方案是完美的。非常感谢。
    【解决方案2】:

    尝试直接从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 不会每次都在回发时绑定

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

    if (!IsPostBack)
        {
          //Load ListView
        }
    

    【讨论】:

      【解决方案3】:

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

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

      不是我使用豌豆制造者解决方案的 ListViewDataItem 插入

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多