views:

40

answers:

1

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" />

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

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?

+1  A: 

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

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.

citronas
Okay, I did notice that behavior. So what should I use as DataSource then?
mgroves
One possibility that pops into my mind is ObjectDataSource. Asp.net will automatically care for the "persistance" of the datasource.Another solution would be to bind the ListView on every postback. But keep in mind that this restrict you in so far, that you can't have any controls inside that listview that generate a postback (like an imagebutton) due to you bind the list on every Page_Load and the Event_Handler for the imagebutton will never be executed because you destroy the fired event if you rebind the datasource before the eventhandler is called.
citronas
ObjectDataSource makes sense, but I keep getting "The type specified in the TypeName property of ObjectDataSource could not be found"
mgroves
Have a look at the MSDN http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.aspxThis articles describes it better than I could sum it up.
citronas
Yeah, I think it's because I didn't put it in App_Code. I'll mark you as accepted, you've been very helpful, thanks.
mgroves