• 取得Repeater控件主键


    怎样取得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);
            }
        }

    }

    选择结果:

  • 相关阅读:
    《三极管应用分析精粹》终审完成,很快就要印刷了!
    关于SPAPI注册,SP-API注册,SPAPI申请,SP-API申请,开发人员资料注册,amazon亚马逊开发人员资料申请注册,amazon亚马逊销售合作伙伴 API申请注册,SP-API申请注册,amazon亚马逊Selling Partner API申请注册详细指导
    日照的那片海
    Cesium地下模式应用示例
    nginx-1.12.2解决跨域问题nginx.conf设置参考记录
    产品功能被像素级抄袭了。我们拿什么来保护原创的产品设计?
    网线的特征阻抗是多少?协议转换器上连接2m线,其非平衡阻抗是多少欧姆?
    ArrayList、LinkedList、HashSet、HashMap、Iterator
    java基础(枚举、包)
    微服务架构、ELK、ETL
  • 原文地址:https://www.cnblogs.com/insus/p/2036884.html
Copyright © 2020-2023  润新知