asp:gridview文本框旧值回发

时间:2016-05-30 12:42:06

标签: c# asp.net gridview postback

<asp:GridView Width="300px" RowStyle-Height="20px" HeaderStyle-Height="25px" DataKeyNames="Id"  ID="bankAccounts_List" CssClass="table" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="Sorting Handle" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate>
<span class="handle"><img src="../Images/sortingHandler.jpg" height="10" /></span>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Account Number">                                              
<ItemTemplate>
<asp:TextBox ID="grd_txtbankAccountNumber" Width="150" onblur="return OnBlurGridTextbox(this);" BorderStyle="None" BackColor="White" style="text-align:left;" runat="server" Text='<%# Eval("AccountNo") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Priority" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate>
<asp:TextBox ID="grd_txtPriority" Width="100" BorderStyle="None" BackColor="White" runat="server" style="text-align:center"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate>
<asp:LinkButton runat="server" ID="grd_linkbutton" OnClientClick="return EditGridButton(this)">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id" >
<HeaderStyle CssClass="hiddencol" />
<ItemStyle CssClass="hiddencol" />
<ItemTemplate>
<asp:Label ID="grd_lblId" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
</ItemTemplate> 
</asp:TemplateField>
</Columns>                                       
</asp:GridView>  

<asp:Button ID="btnSaveBankInformation" runat="server" Text="<%$Resources:Strings,save%>"
            OnClick="btnSaveBankInformation_Click" />

protected void btnSaveBankInformation_Click(object sender, EventArgs e)
{
TextBox txtBankAccountNumber = bankAccounts_List.Rows[i].Cells[GetColumnIndexByName(bankAccounts_List.Rows[i], "Account Number")].FindControl("grd_txtbankAccountNumber") as TextBox;
}

我在任何文本框中更改了什么,在保存按钮上我总是得到旧的绑定文本框值而不是我刚刚输入的新值。 请帮助我。是的,我没有使用!IsPostBack

再次在PostBack上绑定GridView

1 个答案:

答案 0 :(得分:0)

if (!IsPostBack) //Bind grid only when IsPostBack is false
{
    grd.DataSource = //data source ;
    grd.DataBind(); // bind data 
}