【发布时间】:2013-05-17 09:54:19
【问题描述】:
我有一个中继器,其结构如下:
<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>
绑定使用:
ListProducts = db.GetDataTable("select * from Products where Id in (" + selectedValues + ")");
rptItems.DataSource = ListProducts;
rptItems.DataBind();
然后额外的东西完成了:
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;
}
}
但是,现在在页面的 onclick 事件中,我正在尝试保存存储的信息 - 这是我到目前为止所做的,但它似乎总是为空,我无法添加.text 等到(TextBox)item.FindControl("tbSelected"); 的末尾
这是我正在尝试点击的循环:
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");
}
}
}
【问题讨论】:
-
尝试“foreach (Control c in rptItemss.Items)”而不是RepeaterItems,然后是((TextBox)c.FindControl("tbSelected")).Text
-
看起来它可以工作,如果 (TextBox)c.FindControl("tbSelected") 为空,我将如何保护(现在总是显示)
-
var text = (c.FindControl("tbSelected")== null ? "Empty" : ((TextBox)c.FindControl("tbSelected")).Text; 使用调试器和看看什么有效