如何在点击中继器的一行时获得empid?

发布于 2024-12-20 12:05:02 字数 2584 浏览 0 评论 0原文

我有一个复读机。当我点击它的任何行时我想得到 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睫毛上残留的泪 2024-12-27 12:05:02

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

以下代码片段可能会指导您。

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(),无论是否回发。可能有更好的方法来做到这一点。但大部分代码都在这里

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

晌融 2024-12-27 12:05:02

这里是带有复选框控件的示例。与按钮或任何点击事件使用的方式相同。使其成为 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>

Here the sample with checkbox control. Same way you gonna use with button or any click events. Make it one hidden for 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>
一梦浮鱼 2024-12-27 12:05:02

快速写一下

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

试试这个

a quick write -up

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

try this one

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文