文本框在GridView控件不持有其回传值 [英] TextBox in GridView not holding its value on postback
本文介绍了文本框在GridView控件不持有其回传值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个.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();
}
这篇关于文本框在GridView控件不持有其回传值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文