2013-04-23 77 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

相关问题