Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Extract Data from Controls within an ASP:DataList

Posted on 2013-10-24
3
Medium Priority
?
338 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

972 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