簡體   English   中英

在Gridview中更改TextBox數據(asp net c#)

[英]Change TextBox data in Gridview (asp net c #)

我創建了一個gridview,其中包含填充在后端的文本框:

<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。

當頁面加載時,將顯示以下值: Grid But,單擊按鈕時該值為null(clickedButton方法)。

一種非常快速簡單的解決方案是將一個Label添加到GridView並將其Visiblity設置為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
 }

作為對VDWWD響應的補充,請確保頁面未調用回發並從gridview中丟失了引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM