我要做的是迭代转发器并读取一些控件值:
foreach (RepeaterItem iter in TablePanier.Items)
{
string guid = ((HiddenField)iter.FindControl("guid")).Value.ToString();
// nombre exemplaires du livre
int nbExemplaires = int.Parse(((System.Web.UI.WebControls.TextBox)iter.FindControl("txtNbExemplaires")).Text.ToString());
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我有一个HiddenValue和一个TextBox.不幸的是,这不起作用,值无法正确读取.
怎么了?
谢谢!
编辑: 这是表格的完整代码:
public partial class Panier : System.Web.UI.Page
{
Bussiness.Manager _manager = new Bussiness.Manager("MSSQLSERVER");
IEnumerable<Bussiness.iPanier> _paniers;
CurrencyConvertor _currencyConvertor = new CurrencyConvertor();
Bussiness.iCommande _commande;
int idPanier;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["login"] != null)
{
Security security = new Security();
_paniers = _manager.chargerPannierUtilisateur(Session["login"].ToString());
foreach (Bussiness.iPanier p in _paniers)
{
idPanier = p.id;
TablePanier.DataSource = p.Livres;
TablePanier.DataBind();
}
}
else
{
Response.Redirect("~/Accueil.aspx");
}
}
protected void btnCommande_Click(object sender, EventArgs e)
{
foreach (RepeaterItem iter in TablePanier.Items)
{
// id livre courant
if (iter.ItemType == ListItemType.Item || iter.ItemType == ListItemType.AlternatingItem)
{
string guid = ((HiddenField)iter.FindControl("guid")).Value.ToString();
int nbExemplaires = int.Parse(((System.Web.UI.WebControls.TextBox)iter.FindControl("txtNbExemplaires")).Text.ToString());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,转发器在构造函数级别绑定.我正在尝试在页面按钮上发生事件时读取数据.
任何的想法 ?
取决于您拥有控件的位置.
请注意,转发器有一个HeaderItemTemplate,ItemTemplate和AlternateItemTemplate&FooterItemTemplate.
因此,当您遍历转发器的Item集合时,您需要在访问控件对象之前了解要查找的模板.
通常,您需要编写如下代码:
foreach(RepeaterItem item in TablePanier.Items){
if(item.ItemType == ListItemType.HeaderItem){
// do something with the header
}
else if(item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem){
// do something with the item
var guid_control = item.FindControl("guid") as HiddenField;
var nbExemplaires = item.FindControl("txtNbExemplaires") as TextBox;
}
else if(item.ItemType == ListItemType.FooterItem){
// do something with the footer
}
Run Code Online (Sandbox Code Playgroud)
HTH.}
| 归档时间: |
|
| 查看次数: |
11790 次 |
| 最近记录: |