Go to DotNetFunda.com
 Online : 1497 |  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 ?

red flag  Posted on: 7/1/2009 6:41:49 AM by Syedshakeer | Views: 1272 | Category: ASP.NET | Level: Beginner


In this Article you can learn how to get a Selected Row Data from a DataList and display the data in textBoxes.



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.

Found interesting? Add this to:

| More



Please Sign In to vote for this post.

 
Latest post(s) from Syedshakeer

Latest Articles
Experience:2 year(s)
Home page:http://www.dotnetfunda.com
Member since:Thursday, February 05, 2009
Level:Starter
Status: [Member]
Biography:Shakeer Hussain has completed his Master of Computer Applications degree from Deccan College of engg and technology of Osmania University.He is a MVM of www.dotnetspider.com.He has good experience in the areas of ASP.NET, C#.NET, VB.NET, SQL SERVER 2000/2005 and Windows Mobile. He has worked in Windows Mobile,Web Applicatin and ERP projects.

Submit Article

About Us | The Team | Advertise | Contact Us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 9/3/2010 4:39:56 AM