TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
kieky bouvier
NA
58
83.5k
How to get selected value from ListView?
Aug 10 2011 9:27 AM
Hi, i really need your help..
Can u help me to get selected value from listview? So that i can read one record..
thanks before..
Reply
Answers (
4
)
0
Satyapriya Nayak
0
39.3k
13.3m
Aug 11 2011 2:54 PM
Hi kieky,
If this post helps you mark it as answer
Thanks
0
Satyapriya Nayak
0
39.3k
13.3m
Aug 10 2011 2:28 PM
Attachment:
listview.rar
Hi kieky,
Use the below code and run the attachments.
Default.aspx code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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>Listview</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="sid"
OnSelectedIndexChanging="ListView1_SelectedIndexChanging">
<LayoutTemplate>
<table style="border: solid 2px red;" cellspacing="0" cellpadding="3" rules="all">
<tr style="background-color: red; color: Yellow;">
<th>Select</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Student Address</th>
<th>Student Marks</th>
</tr>
<tbody>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="lnkSelect" Text="Select" CommandName="Select" runat="server" />
</td>
<td><%#Eval("sid")%></td>
<td><%#Eval("sname")%></td>
<td><%#Eval("saddress")%></td>
<td><%#Eval("smarks")%></td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr style="background-color: Yellow; color: Green;">
<td>
<asp:LinkButton ID="lnkSelect" Text="Select" CommandName="Select" runat="server"
ForeColor="Blue" />
</td>
<td><%#Eval("sid")%></td>
<td><%#Eval("sname")%></td>
<td><%#Eval("saddress")%></td>
<td><%#Eval("smarks")%></td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Default.aspx.vb code
Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
Dim con As New SqlConnection(strConnString)
Dim sqlda As SqlDataAdapter
Dim com As SqlCommand
Dim ds As DataSet
Dim str As String
Dim str1 As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
binddata()
End Sub
Sub binddata()
con.Open()
str = "select * from student"
com = New SqlCommand(str, con)
sqlda = New SqlDataAdapter(com)
ds = New DataSet()
sqlda.Fill(ds, "student")
ListView1.DataSource = ds
ListView1.DataMember = "student"
ListView1.DataBind()
con.Close()
End Sub
Protected Sub ListView1_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewSelectEventArgs) Handles ListView1.SelectedIndexChanging
ListView1.SelectedIndex = e.NewSelectedIndex
str1 = ListView1.SelectedDataKey.Value
Label1.Text = "<b>You have selected Student ID:</b>" & str1
binddata()
End Sub
End Class
Thanks
If this post helps you mark it as answer
0
Satyapriya Nayak
0
39.3k
13.3m
Aug 10 2011 2:08 PM
Attachment:
select listview data.rar
Hi kieky,
Use the below code and run the attachments.
Default.aspx code
<%@ 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>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="sid"
OnSelectedIndexChanging="ListView1_SelectedIndexChanging">
<LayoutTemplate>
<table style="border: solid 2px red;" cellspacing="0" cellpadding="3" rules="all">
<tr style="background-color: red; color: Yellow;">
<th>Select</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Student Address</th>
<th>Student Marks</th>
</tr>
<tbody>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="lnkSelect" Text="Select" CommandName="Select" runat="server" />
</td>
<td><%#Eval("sid")%></td>
<td><%#Eval("sname")%></td>
<td><%#Eval("saddress")%></td>
<td><%#Eval("smarks")%></td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr style="background-color: Yellow; color: Green;">
<td>
<asp:LinkButton ID="lnkSelect" Text="Select" CommandName="Select" runat="server"
ForeColor="Blue" />
</td>
<td><%#Eval("sid")%></td>
<td><%#Eval("sname")%></td>
<td><%#Eval("saddress")%></td>
<td><%#Eval("smarks")%></td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Default.aspx.cs code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
DataSet ds = new DataSet();
SqlCommand com;
SqlConnection con;
SqlDataAdapter sqlda;
string str;
string str1;
protected void Page_Load(object sender, EventArgs e)
{
binddata();
}
void binddata()
{
con = new SqlConnection(connStr);
con.Open();
str = "select * from student";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "student");
ListView1.DataSource = ds;
ListView1.DataMember = "student";
ListView1.DataBind();
con.Close();
}
protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
ListView1.SelectedIndex =Convert.ToInt32(e.NewSelectedIndex);
str1 = ListView1.SelectedDataKey.Value.ToString();
Label1.Text = "<b>You have selected Student ID:</b>" + str1;
binddata();
}
}
Thanks
If this post helps you mark it as answer
0
Vilas Gite
525
2.5k
543.5k
Aug 10 2011 10:06 AM
Hi Kieky...
Refer '
Our recommended articles
"
---------------------------------
Embed video file
How to get selected value from ListView?