2011-11-08 27 views
1

我有一箇中繼器綁定來自數據庫的數據。現在點擊它的行,我想綁定另一箇中繼器的詳細信息。 一件事更多兩個不嵌套。 OK讓我用一個例子來解釋一下,我有一個類的中繼器。這個轉發器具有有關學校中所有班級的綁定信息。現在,我有另一個轉發器來獲取特定班級的詳細信息。現在當我點擊班級列表中的某個班級時,我將獲得詳細信息,即綁定第二個轉發器班級ID。如何綁定中繼器點擊另一箇中繼器的一行?

回答

1

好的。那麼你知道用戶點擊的行的特定id。在點擊事件中獲取id並將它傳遞給你的存儲過程,或者你以什麼方式綁定到repeater.check下面..

<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

如果您使用的LinkBut​​ton的CommandArgument屬性將是更容易檢索id –

0

我認爲這是你在找什麼:

一個用戶控件顯示有關類的擴展信息。帶有一個綁定在Class實體集合上的中繼器的Web表單。轉發器的ItemTemplate包含一個用於常規類信息的標題和一個用Visibility設置爲false的UserControl實例。然後,我們根據用戶是否想要查看詳細信息來打開/關閉此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用戶控制

(CSS)

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

(代號BEH IND)

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

WebForm的

(CSS)

<%@ 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(); 
} 
相關問題