【问题标题】:How to bind a repeater on click on a row of another repeater?如何在单击另一个中继器的一行时绑定中继器?
【发布时间】:2011-11-08 09:07:25
【问题描述】:

我有一个由来自数据库的数据绑定的转发器。现在点击它的行,我想绑定另一个详细信息的转发器。还有一点,两者都没有嵌套。 好的,让我用一个例子来解释一下,我有一个复读器。这个中继器具有学校所有班级的绑定信息。现在我有另一个中继器来获取特定班级的详细信息。现在当我单击班级列表中的特定班级时,我将获得详细信息,即使用绑定第二个中继器班级编号。

【问题讨论】:

    标签: asp.net repeater


    【解决方案1】:

    好的..所以您知道用户单击的行的特定 id。在单击事件中获取 id 并将其传递给您的存储过程或您绑定到中继器的任何方式。请在下面检查..

    <asp:Repeater ID="repGrd" runat="server">
    <ItemTemplate>      
              <asp:LinkButton ID="lnkbtn" Runat="server" RowID='<%#DataBinder.Eval(Container.DataItem,"ID")%>' OnCommand="clickbutton">Click Here</asp:LinkButton>
    </ItemTemplate>
    </asp:Repeater>
    

    后面的代码是这样的..

     #region On click of row binding repeater
        public void clickbutton(Object sender,CommandEventArgs e)
        {
        try
        {
                //Getting the ID of clicked row
                string RowIDval=((LinkButton)sender).Attributes["RowID"].ToString().Trim();
    
            // Write your code here to bind the repeater as you got the ID
        }
        catch(Exception ex)
        {
    
        }
        }
        #endregion
    

    试试这个。

    【讨论】:

    • 如果您使用 LinkBut​​ton 的 CommandArgument 属性,则检索 id 会更容易
    【解决方案2】:

    我想这就是你要找的:

    显示有关类的扩展信息的用户控件。具有绑定在类实体集合上的转发器的 Web 表单。转发器的 ItemTemplate 包含一个通用类信息的标头和一个我们创建的 UserControl 实例,其中 Visibility 设置为 false。然后,我们根据用户是否想查看详细信息来打开/关闭此 UC。

    一些代码:

    实体

    public class Class
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    public class ClassDetails : Class
    {
        public int NumberOfWeeks
        {
            get
            {
                return this.Id;
            }
        }
    }
    

    ClassDetails 用户控制

    (aspx)

    <hr />
    Class Details:<br />
    Number of Weeks: <asp:Label ID="lblNumWeeks" runat="server" />
    <hr />
    

    (代码隐藏)

    public void Populate(ClassDetails classDetails)
    {
        this.lblNumWeeks.Text = classDetails.NumberOfWeeks.ToString();
    }
    

    网络表单

    (aspx)

    <%@ Register Src="~/ClassDetailsUC.ascx" TagPrefix="SO" TagName="ClassDetailsUC" %>
    <asp:Repeater ID="rptClassList" runat="server">
        <HeaderTemplate>
            <table>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:Label ID="lblClassName" runat="server" />
                    <asp:Button ID="btnShow" runat="server" />
                    <asp:Panel ID="pnlDetails" Visible="false" runat="server">
                        <br />
                        <SO:ClassDetailsUC ID="ucClassDetails" runat="server" />
                    </asp:Panel>
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
    

    (.cs 文件)

    /// <summary>
    /// Page-level collection of Class instances
    /// </summary>
    List<ClassDetails> classes;
    
    /// <summary>
    /// The current class id we are displaying extended information for
    /// </summary>
    private int? ActiveId
    {
        get
        {
            if (this.ViewState["ClassIdDetails"] == null)
            {
                return null;
            }
            else
            {
                return (int?)this.ViewState["ClassIdDetails"];
            }
        }
        set
        {
            this.ViewState["ClassIdDetails"] = value;
        }
    }
    
    protected override void OnInit(EventArgs e)
    {
        this.rptClassList.ItemDataBound += new RepeaterItemEventHandler(rptClassList_ItemDataBound);
        this.rptClassList.ItemCommand += new RepeaterCommandEventHandler(rptClassList_ItemCommand);
        base.OnInit(e);
    }
    
    void rptClassList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //for now we know this is the only button on the repeater. so no need to check CommandType
        //set new ActiveId
        this.ActiveId = Convert.ToInt32(e.CommandArgument);
        //re-bind repeater to refresh data
        this.BindData();
    }
    
    
    /// <summary>
    /// For each Class entity bound, we display some basic info. 
    /// <para>We also check to see if the current Class is the ActiveId. If it is, turn on the detail panel and populate the ClassDetailsUC</para>
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void rptClassList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Class classItem = (Class)e.Item.DataItem;
            ((Label)e.Item.FindControl("lblClassName")).Text = classItem.Name;
            ((Button)e.Item.FindControl("btnShow")).CommandArgument = classItem.Id.ToString();
            if (this.ActiveId.HasValue && this.ActiveId == classItem.Id)
            {
                //we want to display details for this class
                ((Panel)e.Item.FindControl("pnlDetails")).Visible = true;
                //get ClassDetails instance
                ClassDetails details = this.RetrieveDetails(classItem.Id);
                //populate uc
                ((ClassDetailsUC)e.Item.FindControl("ucClassDetails")).Populate(details);
    
            }
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        //quick data retrieval process :)
        classes = new List<ClassDetails> { 
            new ClassDetails() { Id = 1, Name = "Class 1" }, 
            new ClassDetails() { Id = 2, Name = "Class 2" }, 
            new ClassDetails() { Id = 3, Name = "Class 3" } 
        };
    
        if (!this.IsPostBack)
        {
            //only bind on initial load
            this.BindData();
        }
    }
    
    private void BindData()
    {
        this.rptClassList.DataSource = this.classes;
        this.rptClassList.DataBind();
    }
    
    /// <summary>
    /// Quick function to simulate retrieving a single ClassDetails instance
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    private ClassDetails RetrieveDetails(int id)
    {
        return this.classes.Where(c => c.Id == id).Single();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多