Ishan Dragenox
Ishan Dragenox

Reputation: 135

Get Selected Value from DropDownList inside a DataList

I have designed the following code to nest a DropDownList in a DataList with some values the problem. Now how do I get the selected value.

I want to update a Label within the DataList with the selected value of the DropDownList. It should happen each time the user changes the DropDownList selected value

I want something like this:

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

After much reading I found this

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

But how do I assign this value to the label every time the selected item is changed?

The code I have written so far is

ASPX Code:

<%@ 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# Code:

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

Upvotes: 3

Views: 1797

Answers (1)

VDWWD
VDWWD

Reputation: 35554

You can use the NamingContainer of the sender to find the Label.

protected void DDList_SelectedIndexChanged(object sender, EventArgs e)
{
    //cast the sender back to a dropdownlist
    DropDownList ddl = sender as DropDownList;

    //get the namingcontainer from the dropdownlist and cast it as a datalistitem
    DataListItem item = ddl.NamingContainer as DataListItem;

    //now use findcontrol to find the label in the datalistitem
    Label lbl = item.FindControl("selectedData") as Label;

    lbl.Text = ddl.SelectedValue;
}

Upvotes: 2

Related Questions