Solved

Extract Data from Controls within an ASP:DataList

Posted on 2013-10-24
3
312 Views
Last Modified: 2013-11-04
Hi

I have a couple of controls that are contained within an ASP:DataList and I am having trouble extracting the data contained within them.

I need to extract the Label Text and also the value selected in a Drop Down List

<asp:DataList ID="dlItems" runat="server" DataKeyField="ItemID" OnItemDataBound="dlItems_ItemDataBound" OnItemCommand="DataList1_ItemCommand" OnEditCommand="AddToCart">
                        <ItemTemplate>


                            <div class="ProductDiv">
                                <asp:Label ID="ProdID" commandname="ProdID" runat="server" Text=' <%# Container.DataItem("ItemID")%>' /><br />
                                <asp:Label ID="ProdName" runat="server" Text='  <%# Container.DataItem("ProductTitle")%>' /><br />

                                <%# Container.DataItem("ProductDescription")%><br />

                                <asp:DropDownList runat="server" ID="QTYDropDown" OnDataBinding="QTYDropDown_DataBinding" OnSelectedIndexChanged="QTYDropDown_SelectedIndexChanged" CommandName="QTYDropDown" /><br />
                                <asp:Button ID="btnAddToCart" runat="server" Text="Add To Cart" CommandName="Edit" CommandArgument='<%# Container.DataItem("ItemID")%>' />

                                <asp:Label Text="text" runat="server" ID="dropdownvalue" />

                            </div>
                        </ItemTemplate>
                    </asp:DataList>

Open in new window


Im trying to extract the ProdName and also the value of the DropDown QTYDropDown but both state they are not declared

Can anyone help?

Many Thanks
0
Comment
Question by:TPLLimited
  • 2
3 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39600227
When do you need to extract this data?  What have you tried so far?
0
 

Accepted Solution

by:
TPLLimited earned 0 total points
ID: 39611852
Hi,

For this scenario, if you want to get the dropdownlist selected value in dlItems_ItemDataBound event, you could try to use the below codes:

DropDownList ddl = ((DropDownList)dlItems.Items[i].FindControl("QTYDropDown"));

Open in new window


Besides, you should also add the following codes :

<asp:Button ID="btnAddToCart" runat="server" Text="Add To Cart" CommandName="Edit" CommandArgument="<%#((DataListItem) Container).ItemIndex %>"  />

Open in new window


For more information, you could refer to the following example:

.aspx file:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DatalistDropdownlist.aspx.cs" Inherits="WebApplication2013723.DatalistDropdownlist" %>

<!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 ID="dlItems" runat="server" DataKeyField="Id" OnItemCommand="dlItems_ItemCommand" >
                        <ItemTemplate>
                            <div class="ProductDiv">
                                <asp:Label ID="ProdID" commandname="ProdID" runat="server" Text=' <%# Eval("Id")%>' /><br />
                                <asp:Label ID="ProdName" runat="server" Text='  <%# Eval("address")%>' /><br />
                                <asp:Label id="ProdDesc" runat="server" text=' <%# Eval("Recalculate")%>' /><br />
                                <asp:DropDownList runat="server" ID="QTYDropDown"  CommandName="QTYDropDown"  AutoPostBack="True">
                                    <asp:ListItem>a</asp:ListItem>
                                    <asp:ListItem>b</asp:ListItem>
                                    <asp:ListItem>c</asp:ListItem>
                                    <asp:ListItem>d</asp:ListItem>
                                    <asp:ListItem>e</asp:ListItem>
                                </asp:DropDownList><br />
                                <asp:Button ID="btnAddToCart" runat="server" Text="Add To Cart" CommandName="Edit"  CommandArgument="<%#((DataListItem) Container).ItemIndex %>"  />
                                <asp:Label Text="text" runat="server" ID="dropdownvalue" />
                            </div>
                        </ItemTemplate>
                    </asp:DataList>
    </div>

    </form>
</body>
</html>

Open in new window


.aspx.cs file:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2013723
{
    public partial class DatalistDropdownlist : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string connectionString1 = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\Progamming Wuxi\WebForms\WebApplication2013723\App_Data\Database13725.mdf;Integrated Security=True";

                SqlConnection myConnection = new SqlConnection(connectionString1);

                SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Test", myConnection);

                DataSet ds = new DataSet();

                ad.Fill(ds);

                dlItems.DataSource = ds;

                dlItems.DataBind();

            }
        }

        protected void dlItems_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                 int i = Convert.ToInt32(e.CommandArgument.ToString());
                 DropDownList ddl = ((DropDownList)dlItems.Items[i].FindControl("QTYDropDown"));
                 Response.Write(ddl.SelectedValue);    //test whether could find the value;
                 
            }
        }

       }
}

Open in new window

0
 

Author Closing Comment

by:TPLLimited
ID: 39621076
Got it to work using the above
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now