我创建了一个网格视图,其中包含在后端填充的textbox:
<asp:GridView ID="grvMyTest" runat="server" AutoGenerateColumns="False"
EnableModelValidation="True" Height="30%" TabIndex="9"
AllowSorting="True" Width="100%" Visible="true" AllowPaging="True"
PageSize="20" CssClass="mGrid" PagerStyle-CssClass="pgr">
<Columns>
<asp:TemplateField HeaderText="Jan">
<ItemTemplate>
<asp:TextBox ID="tbjan" runat="server" Text='<%# Eval("mJan") %>'
Width="50px" style="text-align: center"></asp:TextBox>
</ItemTemplate>
<HeaderStyle BackColor="Control" HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>在后端,当用户单击按钮时,我希望检索要在数据库中更新的TextBox的值:
<asp:Button runat="server" Text="Alter values" id="testButton" OnClick="clickedButton" />后端代码:
protected void clickedButton(object sender, System.EventArgs e)
{
foreach (GridViewRow row in grvMyTest.Rows) //Running all lines of grid
{
TextBox value = ((TextBox)(row.Cells[0].FindControl("mJan")));
}
}但是,即使在网格上显示的数据库中给定了该值,该值也始终为null。
当页面加载值时,出现的值是:网格,但是,单击按钮时值为null (clickedButton方法)。
发布于 2019-02-15 08:41:58
一个非常快速和简单的解决方案是向GridView添加一个标签,并将其可见性设置为false。
<asp:TextBox ID="tbjan" runat="server" Text='<%# Eval("mJan") %>'></asp:TextBox>
<asp:Label ID="tbjanLabel" runat="server" Text='<%# Eval("mJan") %>' Visible="false"></asp:Label>然后,您可以在代码后面比较这些值。
TextBox value = (TextBox)(row.FindControl("tbjan"));
Label lbl = (Label)(row.FindControl("tbjanLabel"));
if (lbl.Text == value.Text)
{
//no change
}发布于 2019-02-15 15:59:52
补充VDWWD响应,确保页面没有调用回发并从网格视图中丢失引用。
https://stackoverflow.com/questions/54696491
复制相似问题