I have a repeater. i want to get empid when i click on its any row.? how can i ? My code is:-
<table id="table1" class="yui" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>
<a href='#' title="Click Header to Sort">Emp name #</a>
</th>
<th>
<a href='#' title="Click Header to Sort">emp sal</a>
</th>
<th>
<a href='#' title="Click Header to Sort">emp address</a>
</th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="Repaddressorbbl" runat="server" OnItemCommand="Repaddressorbbl_ItemCommand">
<ItemTemplate>
<tr id="gh" style="cursor: pointer" onclick="Select(this);">
<td style="text-align: center;">
<%#Eval("empname")%>
</td>
<td style="text-align: center;">
<%# Eval("empsal")%>
</td>
<td style="text-align: center;">
<%# Eval("empadd")%>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
<tfoot>
<tr id="pagerOne1">
<td colspan="4">
<img src="_assets/img/first.png" class="first" />
<img src="_assets/img/prev.png" class="prev" />
<input type="text" class="pagedisplay" />
<img src="_assets/img/next.png" class="next" />
<img src="_assets/img/last.png" class="last" />
<select class="pagesize">
<option selected="selected" value="100">100</option>
<option value="200">200</option>
<option value="400">400</option>
</select>
</td>
</tr>
</tfoot>
</table>
YOu have to create manual postback. But for this to work always, your repeater should bind always, i.e. during each postback.
The following code snippets may guide you.
using System;
namespace PostbackRef.Web
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
string arg = Request["__EVENTARGUMENT"];
if (string.IsNullOrEmpty(arg) == false)
{
if (arg.StartsWith("row"))
{
string v = arg.Substring(arg.IndexOf("#") + 1);
txtSelected.Text = v;
}
}
}
//Have to bind repeater always
Repeater1.DataBind();
}
protected string getPostbackReference(string Name)
{
return ClientScript.GetPostBackEventReference(this, "rowEvent#" + Name);
}
}
}
The aspx markup looks like this:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PostbackRef.Web._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></title>
<style type="text/css">
p
{
font-size: 12pt;
}
p:hover
{
background-color: Gray;
font-weight: bold;
color: White;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">
<ItemTemplate>
<p onclick="<%# getPostbackReference((string)Container.DataItem) %>">
<%# Container.DataItem %></p>
</ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetNames"
TypeName="PostbackRef.Web.MyDs"></asp:ObjectDataSource>
<asp:HiddenField ID="hfValue" runat="server" />
</div>
<div>
Clicked :
<asp:TextBox ID="txtSelected" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
I've used an ObjectDataSource which returns a string array of names, but the choice of datasource is entirely yours. Here I've used just for e.g. sake.
Note that I am calling Repeater1.Databind() on Page_Load irrespective of postback or not. There are probably better ways to do this. But most of the code came exactly here