.NET Tutorials, Forums, Interview Questions And Answers
HomeTutorialArticlesForumInterview QuestionCode SnippetsTechnology NewsFun Zone Poll Certification Search
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


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 :01/07/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 <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 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;

 

         

 

        }

    }

}


Featured Articles


Best Practices No 5: - Detecting .NET application memory leaks
Memory leaks in .NET application have always being programmer's nightmare. Memory leaks are biggest problems when it comes to production servers. Productions servers normally need to run with least down time. Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily and send a sorry note to the customer for the downtime. ... Read More
.NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code
One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET application. Only measuring execution time does not clearly give idea of where the performance issue resides. Ok, said and done one of the biggest task is to understand which function, assembly or class has consumed how much memory. In this tutorial we will see how we can find which functions consume how much memory. This article discusses the best practices involved using CLR profiler for studying memory allocation.... Read More
How to improve your LINQ query performance by 5 X times ?
LINQ has been criticized by many early adopters for its performance issues. Well if you are just going to drag and drop using DBML code generator I am sure you will land up in to mess. Try doing this make a simple LINQ to SQL project using DBML and see your SQL profiler, I am sure you will never like to touch DBML code generator again. ... Read More
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    Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend