从DataList中的DropDownList获取选定值

时间:2017-03-14 19:06:15

标签: c# asp.net

我设计了以下代码,将DropDownList嵌套在DataList中,但问题有些值。现在我如何获得所选的值。

我想使用DataList的所选值更新DropDownList中的标签。每次用户更改DropDownList所选值

时都会发生这种情况

我想要这样的事情:

protected void DDList_SelectedIndexChanged(object sender, EventArgs e)
{
     selectedData.Text = myDropDownList.SelectedValue;
}

经过多次阅读,我发现了这个

(e.Item.FindControl("DDList") as DropDownList).SelectedValue = DataBinder.Eval(e.Item, "Value").ToString();

但是,每次更改所选项目时,如何将此值分配给标签?

到目前为止我写的代码是

ASPX代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList runat="server" ID="DataList1" OnItemDataBound="DataList1_ItemDataBound" DataKeyField="Class">
        <ItemTemplate>
            <table>
                <tr>
                    <td>
                        <label>
                            <%#Eval("ID")%>: 
                        </label>
                    </td>
                    <td>
                        <label>
                            <%#Eval("Name")%>
                        </label>
                    </td>
                    <td>
                        <label>
                            <%#Eval("Class")%>
                        </label>
                    </td>
                    <td>
                        <asp:DropDownList runat="server" ID="DDList" OnSelectedIndexChanged="DDList_SelectedIndexChanged"></asp:DropDownList>
                    </td>
                    <td>
                        <asp:Label runat="server" ID="selectedData" ForeColor="Red"></asp:Label>
                    </td>
                </tr>
            </table>
            <br />
        </ItemTemplate>
    </asp:DataList>
</div>
</form>

C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            PopulateData();
        }
    }

    private void PopulateData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("ID", typeof(int)));
        dt.Columns.Add(new DataColumn("Name", typeof(string)));
        dt.Columns.Add(new DataColumn("Class", typeof(int)));

        DataRow dr1 = dt.NewRow();
        dr1["ID"] = 1;
        dr1["Name"] = "Ishan";
        dr1["Class"] = 3;

        dt.Rows.Add(dr1);

        DataRow dr2 = dt.NewRow();
        dr2["ID"] = 2;
        dr2["Name"] = "Sumit";
        dr2["Class"] = 5;

        dt.Rows.Add(dr2);

        DataRow dr3 = dt.NewRow();
        dr3["ID"] = 3;
        dr3["Name"] = "Manish";
        dr3["Class"] = 4;

        dt.Rows.Add(dr3);

        DataList1.DataSource = dt;
        DataList1.DataBind();
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            var myDropDownList = e.Item.FindControl("DDList") as DropDownList;
            int currentItemID = int.Parse(this.DataList1.DataKeys[e.Item.ItemIndex].ToString());

            myDropDownList.DataSource = PopulateDDList(currentItemID);
            myDropDownList.DataValueField = "ID";
            myDropDownList.DataTextField = "Value";
            myDropDownList.DataBind();
            (e.Item.FindControl("DDList") as DropDownList).SelectedValue = DataBinder.Eval(e.Item, "Value").ToString();
        }
    }

    private DataTable PopulateDDList(int count)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("ID", typeof(int)));
        dt.Columns.Add(new DataColumn("Value", typeof(string)));

        for(int i = 0; i < count; i++)
        {
            DataRow dr = dt.NewRow();
            dr["ID"] = i;
            dr["Value"] = Convert.ToString(i+1);
            dt.Rows.Add(dr);
        }

        return dt;
    }
}

1 个答案:

答案 0 :(得分:2)

您可以使用NamingContainer的{​​{1}}来查找sender

Label