Due to postback of Button you will not get the TextBox value.
if you want to test it change Label to TextBox for lblLastName
Here is the Sample:
HTML:
<form id="form1" runat="server">
<div>
<asp:ListView ID="lvDemo" runat="server">
<LayoutTemplate>
<table border="0" cellpadding="1">
<tr style="background-color: #E5E5FE">
<th align="left">
EmployeeId
</th>
<th align="left">
LastName
</th>
<th align="left">
Country
</th>
<th align="left">
</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<asp:Label ID="lblCustomerID" runat="server" Text='<%# Eval("EmployeeID") %>'></asp:Label>
</td>
<td id="Td2" runat="server">
<asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
</td>
<td id="Td3" runat="server">
<asp:LinkButton ID="lnkSelect" runat="server" OnClick="GetName" Text="Select" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
</div>
</form>
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetEmployees();
}
}
protected void GetName(object sender, EventArgs e)
{
LinkButton btn = sender as LinkButton;
ListViewDataItem item = (ListViewDataItem)(sender as Control).NamingContainer;
Label lblStatus = (Label)item.FindControl("lblLastName");
string message = lblStatus.Text;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(message);
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
}
private void GetEmployees()
{
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
string sqlStatment = "SELECT EmployeeID ,LastName,Country FROM Employees ";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
this.lvDemo.DataSource = ds;
this.lvDemo.DataBind();
}
}
}
}
Download NorthWind DataBase from this link:
http://northwinddatabase.codeplex.com/