2013-04-23 73 views
0

我有這些複選框上的中繼器:如何將一個ID分配給Repeater中的複選框?

<asp:Repeater id="repeaterCategories" runat="server"> 
    <ItemTemplate> 
     ... 

     <asp:CheckBox ID="chbCategoria" Text="My Label" runat="server" /> 

     ... 
    </ItemTemplate> 
</asp:Repeater> 

每一個複選框,必須從數據庫中獲取頁面ID一致(每repeaterCategories項目都有其唯一的ID,這樣一個)。

我該如何設置它?所以,在回發中,我檢查了哪些CheckBox控件是Checked,並且我得到了這些ID。

回答

1

你可以嘗試添加像這樣

protected void repeaterCategories_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
     CheckBox chk = e.Item.FindControl("chbCategoria") as CheckBox ; 
     chk.Attributes.Add("PageID", DataBinder.Eval(e.Item.DataItem, "DB_FIELD").ToString()); 
    } 
} 
+0

看起來不錯!爲什麼'if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)'? – markzzz 2013-04-23 14:34:28

+0

在ItemTemplate或AlternatingItemTemplate中找到控件 – 2013-04-23 14:45:49

+0

嗯,但我只有'ItemTemplate',所以它只會迭代:) – markzzz 2013-04-23 14:47:21

0

用戶控制網頁:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %> 
<asp:CheckBox id ="MyCheckBox" runat="server"/> 

背後代碼:

using System; 

public partial class WebUserControl : System.Web.UI.UserControl 
{ 
    private string _myProperty; 
    public string MyProperty 
    { 
     get { return this._myProperty; } 
     set { this._myProperty = value; } 
    } 

    public bool IsChecked 
    { 
     get 
     { 
      return this.MyCheckBox.Checked; 
     } 
    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
} 

在你的中繼頁:

<%@ Register Src="~/WebUserControl.ascx" TagPrefix="uc1" TagName="WebUserControl" %> 

中繼內部:

<asp:Repeater id="repeaterCategories" runat="server"> 
    <ItemTemplate> 
     ... 

     <uc1:WebUserControl runat="server" ID="WebUserControl" MyProperty="My_ID_Value" /> 

     ... 
    </ItemTemplate> 
</asp:Repeater> 

您可以在Web用戶控件上添加任意數量的屬性。

+0

嚴正自定義屬性不是真的!試着用'checkBox.Attributes.Add(「category-id」,((ArchiePagina)e.Item.DataItem).UniqueID);'但它將屬性添加到跨度,而不是複選框:O – markzzz 2013-04-23 13:50:51

+0

我沒有明白你在說什麼:O – markzzz 2013-04-23 13:54:11

相關問題