我在GridView中有TextBox
和Label
。我的問題是,當我更新GridView並且TextBox
爲空時,它會刪除標籤中的數據(這很有意義)。我的問題是,是否有可能保持TextBox
爲空並更新而不丟失任何數據?TextBox刪除GridView數據爲空
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>
如果在更新過程中文本框爲空,您究竟想要更新什麼?請稍微描述一下。 – ORION
嗨jcalabris,如果'TextBox'是空的我希望它什麼也不做,而是將一個空白數據放入表中。讓我知道這是否合理。 – vadim
gridview是否應該只有一行,或者它可以有多行?這背後的基本思想是隻更新自上次更新以來已更改的行。是否有可能該行可以更改並設置爲空值?在那種情況下,我猜你會需要更新數據庫嗎? – ORION