Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Extract Data from Controls within an ASP:DataList

Posted on 2013-10-24
3
Medium Priority
?
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

688 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