0

我有一个 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>
4

2 回答 2

1

你可以if (timeR.Text.Length < 10)改成if (timeR.Text.Length < 10 and timeR.Text.Length > 0)

于 2012-11-28T21:07:44.317 回答
0

I figured out the issue here's the code:

StringBuilder sb = new StringBuilder();


                foreach (GridViewRow row in GridView1.Rows)
                {
                    TextBox tR = row.FindControl("SitUps") as TextBox;
                    if(tR.Text != "")
                    {
                    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();
        }

I cleaned up the code and now it checks if the text is not null and proceeds with the update.

Thank you!

于 2012-11-28T21:39:15.423 回答