如何在单击另一行中继器时绑定中继器?

发布于 2024-12-14 19:29:40 字数 197 浏览 1 评论 0原文

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

I have a repeater bind by the data coming from database. Now on click of its row i want to bind another repeater of detailed information.One thing more both are not nested.
OK Let me explain with an example, I have a repeater of classes. This repeater has binding information about all classes in school.Now I have another Repeater for detailed information of a particular class.Now when i click on a particular class in list of classes, then i will get the detailed information i.e. bind the second repeater using the class id.

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

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

发布评论

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

评论(2

沧笙踏歌 2024-12-21 19:29:40

好吧..所以你知道用户单击的行的特定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

试试这个。

Ok..So you know the particular id of row which the user clicks.In the click event get the id and pass it to your stored procedure or what ever way you are binding to the repeater.check this below..

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

and the code behind goes like this..

 #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

Try this out.

短叹 2024-12-21 19:29:40

我认为这就是您正在寻找的:

显示有关类的扩展信息的用户控件。具有绑定到类实体集合的转发器的 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();
}

WebForm

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

I think this is what you are looking for:

A UserControl that displays extended information about a Class. A web form with a repeater that binds on a collection of Class entities. The ItemTemplate of the repeater consists of a header for general class info and an instance of the UserControl we created with Visibility set to false. We then turn this UC on/off depending on whether the user wants to see details.

Some code:

Entities

public class Class
{
    public int Id { get; set; }
    public string Name { get; set; }
}

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

The ClassDetails User Control

(aspx)

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

(code-behind)

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

The WebForm

(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 file)

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