如何在点击另一个转发器的行时绑定转发器?

时间:2011-11-08 09:07:26

标签: asp.net repeater

我有来自数据库的数据的转发器绑定。现在点击它的行我想绑定另一个转发器的详细信息。还有一件事没有嵌套。 好吧让我用一个例子来解释一下,我有一个类的转发器。这个转发器有关于学校所有课程的绑定信息。现在我有另一个转发器来获取特定类的详细信息。现在,当我点击类列表中的特定类时,我将获得详细信息,即使用第二个转发器绑定班级ID。

2 个答案:

答案 0 :(得分:1)

好的..所以你知道用户点击的行的特定id。在click事件中获取id并将其传递给你的存储过程或者你绑定到repeater的任何方式。检查下面的.. < / p>

<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

试试这个。

答案 1 :(得分:0)

我认为这就是你要找的东西:

显示有关类的扩展信息的UserControl。带有转发器的Web表单,它绑定在Class实体的集合上。转发器的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用户控件

(VB)

<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

(VB)

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