Solved

Extract Data from Controls within an ASP:DataList

Posted on 2013-10-24
3
309 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

759 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