文本框在GridView控件不持有其回传值控件、文本框、回传、GridView

2023-09-06 10:45:34 作者:做一个快乐的2B青年

我有一个.NET窗体应用程序一个gridview,并在回发,我没有看到在GridView中的文本框中输入的值。

I have a gridview on a .NET forms application, and on postback, I am not seeing the values entered in the textbox within a gridview.

ASPX:

<asp:GridView ID="gvItems" runat="server" AutoGenerateColumns="false" ShowHeader="false" DataKeyNames="ItemId" EnableViewState="true">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txtItem" runat="server" Text="0" EnableViewState="true" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="ItemId" /> 
     </Columns>
</asp:GridView>

<asp:Button runat="server" ID="btn" Text="Submit" OnClick="btn_OnClick" OnClientClick="javascript:return someClientStuff();" />

code背后:

Code Behind:

protected void btn_OnClick(object sender, System.EventArgs e)
{
    foreach(GridViewRow row in gvItems.Rows)
    {                
        if (row.RowType == DataControlRowType.DataRow)
        {
            var itemId = Convert.ToInt32(gvItems.DataKeys[row.RowIndex].Values[0]);
            var itemValue = ((row.Cells[0].FindControl("txtItem") as TextBox).Text;
        }
}

我看到的itemId填充的每一行,但itemValue始终是空字符串。

I am seeing itemId populated for each row, but itemValue is always empty string.

已经有一段时间,我曾在一个窗体应用程序,任何帮助是AP preciated!

Been a while since I worked on a forms application, any help is appreciated!

推荐答案

我会假设的DataBind GridView控件的不是如果(!的IsPostBack)。这种添加到的Page_Load

I will assume that the DataBind of the GridView is not in if(!IsPostBack). Add this to the Page_Load

if(!IsPostBack)
{
    gvItems.DataSource = soruceOftheGrid
    gvItems.DataBind();
}