从表单提交的文本框中获取值

时间:2011-07-05 07:51:44

标签: .net asp.net listview textbox

假设我在ItemTemplate中有一个带有TextBox控件的ListView控件:

<asp:ListView ID="aspectsAndRatings" runat="server">
    <LayoutTemplate>
        <ul>
            <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
        </ul>
    </LayoutTemplate>
    <ItemTemplate>
        <li>
            <asp:TextBox ID="rating" runat="server" />
        </li>
    </ItemTemplate>
</asp:ListView>

当发布此表单时(通过单击“提交”按钮),我想提取已输入到文本框中的值。

我当然可以做以下事情:

Request.Form["ctl00$MainContent$aspectsAndRatings$ctrl0$rating"]

但我确信ASP.NET提供了一种更简洁的方法来获取ListView的每一行输入的值。我只是不记得它是什么。

(我确实尝试过listView.Items [n] .FindControl(“rating”),但它返回的文本框的值为空值,而不是用户输入的值。)

1 个答案:

答案 0 :(得分:2)

您应该能够在回发处理程序中使用以下代码获取值。请记住,不要在页面加载事件中重新绑定转发器。

foreach (var item in aspectsAndRatings.Items)
{
    TextBox textBox = item.FindControl("rating") as TextBox;
    if (textBox != null)
    {
        string textValue = textBox.Text;
    }
}
相关问题