.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to get a Selected Row Data from a DataList and display the data in TextBoxes.

Posted By:Syed Shakeer Hussain       Posted Date: July 01, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  
 

In this Article you can learn how to get a Selected Row Data from a DataList 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 Link button.

 

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 and EditCommand Event should be the same Text.

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

The Complete 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 Pagetitle>

head>

<body>

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

    <div>

         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;

 

         

 

        }

    }

}


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend