ListView的领域没有得到公布 [英] ListView fields not getting posted

查看:130
本文介绍了ListView的领域没有得到公布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我以前做过这样的事情,但我不知道为什么它不工作了。我有一些文本框一个ListView。我想读课文出那些箱子当我点击一个按钮(LinkBut​​ton的,等等)。

I know I've done something like this before, but I have no idea why it isn't working now. I have a ListView with some textboxes. I want to read the text out of those boxes when I click a button (linkbutton, whatever).

        <asp:ListView runat="server" ID="lv_bar" EnableViewState="true">
            <LayoutTemplate>
                <table>
                    <tr>
                        <th>Foo</th>
                    </tr>
                    <tr runat="server" id="itemPlaceholder"></tr>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr>
                    <td><asp:LinkButton ID="lb_delete" CausesValidation="false" runat="server" Text="Del" /></td>
                    <td><asp:TextBox id="txt_foo" runat="server" /></td>
                </tr>
            </ItemTemplate>
        </asp:ListView>
        <asp:LinkButton ID="lb_add" CausesValidation="false" runat="server" Text="Add" />

然后这里的相关code-背后的东西:

And then here's the relevant code-behind stuff:

protected void Page_Load(object sender, EventArgs e)
{
    lb_chapter_add.Click += lb_chapter_add_Click;

    if (!IsPostBack)
    {
            lv_chapters.DataSource = new List<Foo>() { new Foo() { Name = "harbl"} };
            lv_chapters.DataBind();
        }
    }

void lb_add_Click(object sender, EventArgs e)
{
    foreach (ListViewDataItem item in lv_bar.Items)
    {
        var txt_foo = (TextBox)item.FindControl("txt_foo");
        Response.Write("foo: " + txt_foo.Text);
    }
    Response.Write("<br />the end");
    Response.End();
}

但我所看到的,当我输入一些文本txt_foo并单击lb_add就是终结。我在做什么错在这里?

But what I see when I enter some text into txt_foo and click lb_add is just "the end". What am I doing wrong here?

推荐答案

您正在使用一个非持久化对象作为数据源的问题吧。

The problem it that you are using a a non persistent object as DataSource.

由于点击按钮,生成一个回传和lv_chapters不包含任何项目。坐落在该行断点,其中的foreach是,你会看到在lv_chapters.Items空,或者说,它的Count属性返回0。

Due to clicking the button, you generate a postback and lv_chapters does not contain any items. Set a breakpoint in the line where the foreach is and you will see that lv_chapters.Items in null, or that it's Count property returns 0.

这篇关于ListView的领域没有得到公布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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