Tôi có một repeater được xây dựng như sau:Looping thông qua các mục lặp lại
<asp:Repeater runat="server" ID="rptItems" OnItemDataBound="rptItems_ItemDataBound">
<ItemTemplate>
<div class="span12 grey-box">
<div class="hero-block3">
<div class="row show-grid">
<div class="span9">
<div class="hero-content-3">
<h2><asp:Literal ID="ltrName" runat="server"></asp:Literal></h2>
<p><asp:Literal ID="ltrDescription" runat="server"></asp:Literal></p>
</div>
</div>
<div class="span3">
<asp:Panel ID="pnlAmount" runat="server">
<div class="tour-btn" id="divAmount" runat="server">
<small>How Many?<br /></small>
<asp:TextBox runat="server" ID="tbox" Width="40"></asp:TextBox>
</div>
</asp:Panel>
</div>
</div>
</div>
</div>
<div class="clear-both"></div>
<br />
</ItemTemplate>
</asp:Repeater>
Nó ràng buộc sử dụng:
ListProducts = db.GetDataTable("select * from Products where Id in (" + selectedValues + ")");
rptItems.DataSource = ListProducts;
rptItems.DataBind();
Và sau đó công cụ bổ sung được thực hiện với:
protected void rptItems_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
DataRowView nRow = null;
switch (e.Item.ItemType)
{
case ListItemType.Item:
case ListItemType.AlternatingItem:
nRow = (DataRowView)e.Item.DataItem;
((Literal)e.Item.FindControl("ltrDescription")).Text = "" + nRow["Description"];
((Literal)e.Item.FindControl("ltrName")).Text = "" + nRow["Name"];
if ("" + nRow["HasAmount"] == "False")
{
((Panel)e.Item.FindControl("pnlAmount")).Visible = false;
}
break;
}
}
Tuy nhiên, bây giờ trên một sự kiện onclick cho trang, tôi đang cố gắng lưu các thông tin được lưu trữ - Đây là những gì tôi đã làm cho đến nay, nhưng nó alw ays tất cả có vẻ là null, và tôi không thể thêm một .text vv để kết thúc (TextBox)item.FindControl("tbSelected");
Heres vòng lặp của tôi đang cố gắng trên nhấp chuột:
protected void doStageThree(object sender, EventArgs e)
{
foreach (RepeaterItem item in rptItems.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
var tbSelected = (TextBox)item.FindControl("tbSelected");
var lblDescription = (Literal)item.FindControl("ltrDescription");
var lblName = (Literal)item.FindControl("ltrName");
}
}
}
Hãy thử "foreach (Control c trong rptItemss.Items)" thay vì RepeaterItems, sau đó ((TextBox) c.FindControl ("tbSelected")) Văn bản –
Điều đó có vẻ như nó có thể hoạt động, làm thế nào tôi sẽ bảo vệ nếu (TextBox) c.FindControl ("tbSelected") là null (bây giờ là luôn hiển thị) – TMB87
var text = (c.FindControl ("tbSelected") == null? "Empty": ((TextBox) c.FindControl ("tbSelected")). Chơi xung quanh với trình gỡ lỗi và xem những gì hoạt động –