我有一个 GridViewTextBox
和一个Label
内部。我的问题是,当我更新 GridView 并且它TextBox
为空时,它会删除标签中的数据(这是有道理的)。我的问题是,是否可以在TextBox
不丢失任何数据的情况下保持空白和更新?
C#:
Private void Update()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
TextBox timeR = GridView1.Rows[i].FindControl("SitUps") as TextBox;
if (timeR.Text.Length < 10)
{
foreach (GridViewRow row in GridView1.Rows)
{
sb.Append("UPDATE bleaTest SET SitUps = '");
sb.Append((row.FindControl("SitUps") as TextBox).Text);
sb.Append("'");
sb.Append(" WHERE id = ");
sb.Append(Convert.ToInt32((row.FindControl("ID") as Label).Text));
sb.Append(" ");
}
}
else
{
timeR.Text = "Number is too high!";
foreach (GridViewRow row in GridView1.Rows)
{
sb.Append("UPDATE bleaTest SET SitUps = '");
sb.Append((row.FindControl("SitUps") as TextBox).Text);
sb.Append("'");
sb.Append(" WHERE id = ");
sb.Append(Convert.ToInt32((row.FindControl("ID") as Label).Text));
sb.Append(" ");
}
}
}
string connectiongString = "Data Source=WSCJTCSQ1;Initial Catalog=TestDB;Persist Security Info=True;User ID=v2soft;Password=passwordv2soft";
SqlConnection myConnection = new SqlConnection(connectiongString);
SqlCommand myCommand = new SqlCommand(sb.ToString(), myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
BindData();
}
aspx:
<asp:TemplateField HeaderText="Sit Ups">
<ItemTemplate>
<asp:TextBox ID="SitUps" runat="server" type="number" Text='<%# Eval("SitUps") %>' EnableViewState="True"></asp:TextBox></div>
</ItemTemplate>
</asp:TemplateField>