I created a gridview containing textbox that are populated in the backend:
<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>
In the backend I would like that when the user clicks the button I would like to retrieve the value of the TextBox to update in the database:
<asp:Button runat="server" Text="Alter values" id="testButton" OnClick="clickedButton" />
Back-end code:
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")));
}
}
But the value is always null even having given in the database that appear on the grid.
When the page loads the values appear: Grid But, the value is null when the button as clicked (clickedButton method).
A very quick and simple solution would be to add a Label to the GridView and set it's Visiblity to 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>
Then you can compare those values in code behind
TextBox value = (TextBox)(row.FindControl("tbjan"));
Label lbl = (Label)(row.FindControl("tbjanLabel"));
if (lbl.Text == value.Text)
{
//no change
}
作为对VDWWD响应的补充,请确保页面未调用回发并从gridview中丢失了引用。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.