Solved

Extract Data from Controls within an ASP:DataList

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

623 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