我在templatefield(位于gridview中)中有一个自由文本框,该文本框通过aspx中的Text='<%#Eval("data1") %>'
和BindGrid()sql命令绑定数据以填充freetextbox的gridview templatefield。当我单击一个按钮时,数据丢失。如何在templatefield中保留数据?
如果我将templatefield替换为<asp:BoundField DataField="data1" HeaderText="data1" >
,即使单击按钮后它也保留数据。
aspx代码:
<asp:GridView ID="GridView1" runat="server" EmptyDataText="No Records." AutoGenerateColumns="False" DataKeyNames = "primarykey">
<asp:TemplateField HeaderText="data1">
<ItemTemplate>
<FTB:FreeTextBox ID="FreeTextBox1" EnableToolbars="false" EnableHtmlMode="false" Height="30" Width="100%" ReadOnly="true" runat="server" Text='<%#Eval("data1") %>' ></FTB:FreeTextBox>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
CS代码部分:
protected void Page_Load(object sender, EventArgs e)
{if (!Page.IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["db1"].ToString()))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT [mydata1] AS data1 FROM [dbo].[dbtest1], con);
using (SqlDataAdapter sql = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sql.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
按钮单击:
<asp:Button ID="Save" runat="server" OnClick="SaveClick" Text="Save" />
protected void SaveClick(object sender, EventArgs e)
{
}