c# - Gridview 无法解析输入字符串不正确
问题描述
本质上是在选中复选框时尝试捕获信息,如果是则捕获输入的数量。附上代码。
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="TextboxQuantity" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
这是我的 aspx.cs 代码。
//check to see if a check box is checked
for (int row = 0; row < gv_Input.Rows.Count; row++)
{
CheckBox Cbox = (CheckBox)gv_Input.Rows[row].FindControl("CheckboxSelect");
TextBox Tbox = (TextBox)gv_Input.Rows[row].FindControl("TextboxQuantity");
int quantity = Convert.ToInt32(Tbox.Text);
if (Cbox.Checked)
{
if (Tbox == null)
{
Response.Write("<script>alert('Fill in textbox')</script>");
}
else
{
Response.Write(
"<script>alert('Something was inputted into the textbox')</script>");
}
}
}
给出错误的行是这一行
int quantity = Convert.ToInt32(Tbox.Text);
错误:输入字符串的格式不正确
解决方案
即使文本框留空,测试if (Tbox == null)
也永远不会为真,因为您检查的是对文本框的引用,而不是其内容。我相信你的测试应该是:
if(Tbox == null || string.IsNullOrWhitespace(Tbox.Text) == true) {
推荐阅读
- ios - Xcode 12 GM 陷入无限循环
- angular - Jhipster 和 ngx-translate 停止使用升级到 0.14
- javascript - Discord ping 报告为 NaN
- javascript - 为什么 justify-content 不导致元素之间的空间?
- c - 为什么参数太少的 printf 在 Cygwin 上工作?
- visual-studio - VS 2017 - 旧解决方案项目中缺少 NuGet 包
- javascript - 从孩子设置上下文会导致无限调用循环
- druid - 使用 DataSketches 计算中位数
- javascript - 在段落文本之后放置图标,无论使用 CSS 的行长如何
- r - 将文件路径的数据框复制到 R 中的新文件夹中