Go to DotNetFunda.com
 Online : 5302 |  Welcome, Guest!   Login
 
Home > Articles > ASP.NET > How to get a Selected Row Data from a DataList ?

Submit Article | Articles Home | Search Articles |

How to get a Selected Row Data from a DataList ?

 Posted on: 7/1/2009 6:41:49 AM by Syedshakeer | Views: 849 | Category: ASP.NET | Level: Beginner | Print Article
In this Article you can learn how to get a Selected Row Data from a DataList and display the data in textBoxes.

.NET Training Videos!
Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

Introduction

In this Article you can learn how to get a Selected Row Data from a DatraList and display the data in textBoxes.
First Bind the Data to the DataList using SqlDataSource and drag and drop three textboxes on the Page.you can see the coding at the bottom in the page. 


 

How to Create a ‘Select’ Link button on the DataList ?
In DataList for creating a Select link button on DataList you have write a code Explicitly.In .aspx source code write the code for creating a Linkbutton.



Select the DataList Events and double click on ‘EditCommand’ Event and write the below code.

protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)

  {

     if (e.CommandName == "Edit")

     {

         Label lbname = (Label)e.Item.FindControl("nameLabel");

         Label lbmarks = (Label)e.Item.FindControl("marksLabel");

         Label lbid = (Label)e.Item.FindControl("idLabel");

 

            TextBox1.Text = lbname.Text;

            TextBox2.Text = lbmarks.Text;

            TextBox3.Text = lbid.Text;

 

    }

}

CommandName:- Gets/Sets the command name which is passed to the command event handler.To create a command button you need to add a command name to the button. You add a command name to the button with the CommandName property .

In the above code I use e.CommandName=="Edit".The CommandName I had used in <asp:LinkButton CommandName="Edit"> and EditCommand Event should be the same Text.

Below is the OutPut Image when the DataList Row Is Selected .

The Complete Source Code is as Follows:-

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        &nbsp;</div>

        <asp:DataList ID="DataList1" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"

            BorderWidth="1px" CellPadding="2" DataSourceID="SqlDataSource1" ForeColor="Black"

            GridLines="Vertical" Style="z-index: 100;  left: 356px; position: absolute; top: 12px "

            Width="280px" CellSpacing="4" BorderStyle="Double" OnEditCommand="DataList1_EditCommand" >

            <FooterStyle BackColor="Tan" />

            <SelectedItemStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />

           

            <ItemTemplate>

           

<asp:LinkButton ID="lnkselect" runat ="server" Text ="Select" CommandName ="Edit" ></asp:LinkButton>

           

  name: <asp:Label  ID="nameLabel" runat="server" Text='<%# Eval("name") %>'></asp:Label><br />

              

  marks:<asp:Label ID="marksLabel" runat="server" Text='<%# Eval("marks") %>'></asp:Label><br />

 

 id:<asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>'></asp:Label><br />

                <br />

            </ItemTemplate>

                       

                       

            <AlternatingItemStyle BackColor="PaleGoldenrod" />

            <HeaderStyle BackColor="Tan" Font-Bold="True" />

        </asp:DataList>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:shakeerConnectionString %>"

            SelectCommand="SELECT [name], [marks], [id] FROM [emp]"></asp:SqlDataSource>

        <asp:TextBox ID="TextBox1" runat="server" Style="z-index: 101; left: 463px; position: absolute;

            top: 450px"></asp:TextBox>

        <asp:TextBox ID="TextBox2" runat="server" Style="z-index: 102; left: 463px; position: absolute;

            top: 480px"></asp:TextBox>

        <asp:TextBox ID="TextBox3" runat="server" Style="z-index: 103; left: 463px; position: absolute;

            top: 509px"></asp:TextBox>

        <asp:Label ID="Label1" runat="server" Style="z-index: 107; left: 389px; position: absolute;

            top: 450px" Text="Name:"></asp:Label>

        <asp:Label ID="Label2" runat="server" Style="z-index: 105; left: 389px; position: absolute;

            top: 480px" Text="Marks:"></asp:Label>

        <asp:Label ID="Label3" runat="server" Style="z-index: 106; left: 389px; position: absolute;

            top: 508px" Text="ID:"></asp:Label>

    </form>

</body>

</html>

In .aspx.cs

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

      

      

 

    }

 

    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)

    {

        if (e.CommandName =="Edit")

        {

            Label lbname = (Label)e.Item.FindControl("nameLabel");

            Label lbmarks = (Label)e.Item.FindControl("marksLabel");

            Label lbid = (Label)e.Item.FindControl("idLabel");

            TextBox1.Text = lbname.Text;

            TextBox2.Text = lbmarks.Text;

            TextBox3.Text = lbid.Text;

 

         

 

        }

    }

}

 


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:1 year(s)
Home page:http://www.dotnetfunda.com
Member since:Thursday, February 05, 2009
Level:Bronze
Status: [Member]
Biography:Hi to All ..
I am Working as Software Developer on .NET and MVM of www.dotnetspider.com
 Latest post(s) from Syedshakeer

   ◘ How to get the value of a Hidding column in a Gridview using C# posted on 9/8/2009 6:05:36 PM
   ◘ How to calculate total at the BackEnd using Trigger? posted on 8/25/2009 6:14:14 PM
   ◘ How to display records as First-Next-Previous-Last in a Textboxes using Windows Application? posted on 8/25/2009 6:01:05 PM
   ◘ How to Start Mobile Application on Windows? posted on 8/18/2009 1:00:01 AM
   ◘ Paging for First, Next, Previous and Last in gridview posted on 8/16/2009 12:47:43 PM


Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)