循环中继器项目
我有一个转发器,其构建如下:
How Many?
它受到约束:
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事件中,我正在尝试保存存储的信息 – 这是我到目前为止所做的,但它总是似乎都是null,我不能添加.text等到(TextBox)item.FindControl("tbSelected");
inheritance我的循环我正在尝试点击:
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"); } } }
它始终为null,因为没有具有id tbSelected
TextBox
将其更改为:
var tbSelected = (TextBox)item.FindControl("tbox");
要保护您的代码不使用null,请使用关键字:
var tbSelected = item.FindControl("tbox") as TextBox; if (tbSelected != null) { //textbox with id tbox exists tbSelected.Text = "your text"; }
尝试更换
foreach (RepeaterItem item in rptItems.Items)
同
上述就是C#学习教程:循环中继器项目分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
foreach (Control c in rptItems.Items) { if(c.FindControl("tbSelected") != null) { var selectedText = ((TextBox)c.FindControl("tbSelected")).Text; } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1013718.html