Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 29992 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > How to get a Selected Row Data from a DataList ?

How to get a Selected Row Data from a DataList ?

Article posted by Syedshakeer on 7/1/2009 | Views: 4277 | Category: ASP.NET | Level: Beginner red flag


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.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

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.
>> Write Response - Respond to this post and get points
Related Posts

Hope this article helps you to increase your .net internal knowledge.

In this article we are going to learn how to save and retrieve regional languages data from the database. We can use any language data in this example whether Hindi, Bangali, Urdu, French or Chinese.

You must have noticed one link in yahoo.com, msn.com or other popular websites named "Page Options". When you click this link you get a popup displaying different small several color icons. After clicking one of these icons your page theme changes without entire page refresh. Now you are able to see the same page with different look and feel.

Read ConnectionString Information from Application Configuration File.

This article demonstrate how to do CRUD operation using GridView and jQuery seamlessly (without page refresh) and also describes some simple UI effects in ASP.NET GridView control using jQuery. This is the Part 1 of this article.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for 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. | 5/28/2012 11:58:05 AM