ASP .NET - 如何通过中继迭代? [英] ASP .NET - How to Iterate through a repeater?

查看:91
本文介绍了ASP .NET - 如何通过中继迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做的是通过迭代转发器和读一些控件的值:

What I'm trying to do is iterate through a repeater and read some controls values:

   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());

    }

正如你所看到的,我有一个HiddenValue和一个TextBox。不幸的是这是行不通的,值不正确读取。

As you can see, I have a HiddenValue and a TextBox. Unfortunately this isn't working, the values are not read correctly.

怎么了?

感谢您!

编辑:
这里的形式为整个code:

Here is the entire code of the form:

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());

            }
        }
    }
}

正如你所看到的,中继器的构造层次的约束。而且我想,当事件发生在页面的按钮,读取数据。

As you can see, the repeater is bound at the constructor level. And I'm trying to read the data when an event occur on a button of the page.

任何想法?

推荐答案

有几件事情要检查:


  • 中继势必在Page_Load中双方的intial请求,并在回发。因此,当一个按钮,点击帖子页面,中继器获取与原始数据加载。由用户输入的任何丢失。这是你想要的吗?如果没有,使用IsPostBack属性加载它仅在GET请求。

  • the repeater is bound in Page_Load on both the intial request and on postback. So when a button click posts the page, the repeater gets reloaded with the original data. Anything entered by the user is lost. Is this what you want? If not, use the IsPostBack property to load it on the GET request only.

如果会话[登录]为空,你的中继不会得到加载 - ?我已签此presume

if Session["login"] is null, your repeater will not get loaded - I presume you have checked this?

在您的贴code,你是不是做与您试图从Repeater控件获取值什么。没有显示这一点,因为有额外的code?

in your posted code, you are not doing anything with the values you are trying to get from the repeater controls. Is this because there is additional code not shown?

你用调试器来检查所发现的FindControl的控件,以确保它们的存在,它们是什么,你认为他们是谁?

Have you used the debugger to inspect the the controls found by FindControl to make sure they exist and they are what you think they are?

这篇关于ASP .NET - 如何通过中继迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆