我知道我以前做過類似的事情,但我不知道爲什麼它現在不能工作。我有一個ListView與一些文本框。當我點擊一個按鈕(linkbutton,無論)時,我想從這些框中讀出文本。ListView字段沒有發佈
<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" />
然後這裏是相關的代碼隱藏的東西:
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就是「終結」。我在這裏做錯了什麼?
不應該是lv_bar在foreach中嗎? – 2010-01-07 18:45:47
是的,對不起,我試圖在粘貼之前概括實際的代碼 - 但這不是問題:) – 2010-01-07 18:47:26