如何在点击转发器的一行时获得empid?

时间:2011-12-12 05:15:53

标签: asp.net repeater

我有一个转发器。当我点击它的任何一行时我想得到empid。我怎么能够 ? 我的代码是: -

<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>

3 个答案:

答案 0 :(得分:0)

此处带有复选框控件的示例。使用按钮或任何点击事件的方式相同。将其隐藏为empid。

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)

{
CheckBox box = sender as CheckBox;

RepeaterItem item = box.NamingContainer as RepeaterItem;
HiddenField hf = item.FindControl("HiddenField1") as HiddenField;string id = hf.Value;

}



<ItemTemplate>

<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox1_CheckedChanged" />

<asp:HiddenField ID="HiddenField1" Value='<%# Eval("id") %>' runat="server" />

</ItemTemplate>

答案 1 :(得分:0)

快速撰写

<asp:Repeater ID="Repaddressorbbl" runat="server" OnItemCommand="Repaddressorbbl_ItemCommand">
                    <ItemTemplate>
                        <tr id="gh" style="cursor: pointer" onclick="Select('<%# Eval("EmployeeID")%>');">
                            <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>

尝试这个

答案 2 :(得分:0)

你必须创建手动回发。但为了使其始终有效,您的转发器应始终绑定,即在每次回发期间。

以下代码段可能会为您提供指导。

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);
        }
    }
}

aspx标记看起来像这样:

<%@ 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>

我使用了ObjectDataSource,它返回一个字符串数组的名称,但数据源的选择完全属于你。我在这里仅用于例如缘故。

请注意,我在Page_Load上调用Repeater1.Databind()而不管回发与否。可能有更好的方法来做到这一点。但大多数代码都是here