继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

取得Repeater控件主键

慕UI0519722
关注TA
已关注
手记 296
粉丝 85
获赞 267

怎样取得Repeater控件主键,Repeater控件没有如Gridview的DataKeyNames属性。想获取Repeater记录的主键只有另想办法了。

下面这个例子,是在每笔记录行放一个Button(当然用LinkButton也可),用户点一点这个铵钮,即可获取这个记录。

.aspx(重点部分已经有注释):

View Code    <!--技术重点是使用OnItemCreated,而不是使用OnItemDataBound-->
                <asp:Repeater ID="Repeater1" runat="server" OnItemCreated="Repeater1_ItemCreated">
                    <HeaderTemplate>
                        <table border="1" cellpadding="5" cellspacing="0" width="100%">
                            <tr>
                                <td>
                                    ID
                                </td>
                                <td>
                                    Name
                                </td>
                                <td>
                                    Email
                                </td>
                                <td>
                                    SELECT
                                </td>
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                                <%# Eval("MemberId") %>
                                <!--由于Repeater控件没有如GridView的DataKeyNames属性,因此放一个HiddenField控件-->
                                <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("MemberId") %>' />
                            </td>
                            <td>
                                <%# Eval("Name") %>
                            </td>
                            <td>
                                <%# Eval("Email") %>
                            </td>
                            <td>
                                <!--只放一个Button,无需写什么,当然你拉一个LinkButton也可以-->
                                <asp:Button ID="Button1" runat="server" Text="Button" />
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>

 

.aspx.cs:

View Code using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;

public partial class Default2 : System.Web.UI.Page
{
    Member objMember = new Member();
    InsusJsUtility objJs = new InsusJsUtility();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Data_Binding();
        }
    }

    private void Data_Binding()
    {
        this.Repeater1.DataSource = objMember.GetMember().Tables[0];
        this.Repeater1.DataBind();
    }

    //ItemCreated事件
    protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
           //判断Button是否存在
            if (e.Item.FindControl("Button1") != null)
            {
               //如果存在,把对象转换为Button。
                Button InsusButton = (Button)e.Item.FindControl("Button1");
                //产生Click事件
                InsusButton.Click += new EventHandler(InsusButton_Click);
            }
        }
    }

    //如何获取主键
    private void InsusButton_Click(object sender, EventArgs e)
    {
        Button button = (Button)sender;
        //判断HiddenField是否存在
        if (button.NamingContainer.FindControl("HiddenField1") != null)
        {
            //存在,把对象转换为HiddenField控件
            HiddenField hf = (HiddenField)button.NamingContainer.FindControl("HiddenField1");
            //取出HiddenField的Value值。
            string id = hf.Value;
           
            //取出之后,就可以在这里处理你的事情了,下面只是简单抛出而已。
            objJs.JsAlert("你选择的记录主键ID是" + id);
        }
    }

}

 

选择结果:

 

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP