开发者

Getting values from text-boxes, on form submit

开发者 https://www.devze.com 2023-03-17 13:34 出处:网络
Say I have a ListView control with a TextBox control in the ItemTemplate: <asp:ListView ID=\"aspectsAndRatings\" runat=\"server\">

Say I have a ListView control with a TextBox control in the ItemTemplate:

<asp:ListView ID="aspectsAndRatings" runat="server">
    <LayoutTemplate>
        <ul>
            <asp:PlaceHolder ID="itemPlaceholder" runat="ser开发者_如何学Pythonver" />
        </ul>
    </LayoutTemplate>
    <ItemTemplate>
        <li>
            <asp:TextBox ID="rating" runat="server" />
        </li>
    </ItemTemplate>
</asp:ListView>

When this form gets posted (by clicking the Submit button) I want to extract the values that have been entered into the text-boxes.

I could, of course, just do the following:

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

But I'm sure ASP.NET provides a cleaner way to get the values entered, for each row of the ListView. I just can't remember what it is.

(I did try listView.Items[n].FindControl("rating"), but it returns the textbox with an empty value, rather than the value the user entered.)


You should be able to get values using the code below in a postback handler. Just rememeber not to re-bind your repeater at the page load event.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号