【问题标题】:GridView Row UpdateGridView 行更新
【发布时间】:2011-02-13 06:24:53
【问题描述】:
if (((LinkButton)GridView1.Rows[0].Cells[1].Controls[0]).Text == "Insert")
        {
            VendorProperties VP = new VendorProperties();
            VP.VendorName = ((TextBox)GridView1.Rows[0].Cells[3].Controls[0]).Text.ToString();
           try
            {
                VP.ABN = Convert.ToInt32(((TextBox)GridView1.Rows[0].Cells[4].Controls[0]).Text.ToString());
            }
            catch 
            {
                lblMessage.Text = "Please Enter a Number for ABN";

            }
                VP.VendorAddress1 = ((TextBox)GridView1.Rows[0].Cells[5].Controls[0]).Text.ToString();
            VP.VendorAddress2 = ((TextBox)GridView1.Rows[0].Cells[6].Controls[0]).Text.ToString();
            VP.VendorAddress3 = ((TextBox)GridView1.Rows[0].Cells[7].Controls[0]).Text.ToString();
            VP.State = ((TextBox)GridView1.Rows[0].Cells[8].Controls[0]).Text.ToString();
            VP.PostCode= ((TextBox)GridView1.Rows[0].Cells[9].Controls[0]).Text.ToString();
            VP.ContractorVendorContactName = ((TextBox)GridView1.Rows[0].Cells[10].Controls[0]).Text.ToString();
            VP.Phone = ((TextBox)GridView1.Rows[0].Cells[11].Controls[0]).Text.ToString();
            DropDownList DR = (DropDownList)GridView1.Rows[0].Cells[12].FindControl("DropDownList1");
            VP.EmailAddressID= Convert.ToInt32(DR.SelectedValue.ToString());
            PinDicDAO.InsertVendor(VP);
        }

以上是我通过 gridview 行更新事件为数据库插入记录的代码。项目“VP.ABN”(在try catch中)是一个整数,用户应该输入一个整数。如果用户输入ABN的字符串,我需要停止将记录插入数据库并打印消息“请输入一个数字ABN”。在此代码中,即使用户输入了一个字符串,它也会显示消息,并且仍然将记录插入到数据库中,因为 ABN 值为 0。我该如何解决这个问题?

【问题讨论】:

    标签: asp.net validation gridview try-catch


    【解决方案1】:

    首先,您不应该以这种方式使用 try catch。
    您想验证可以使用正则表达式的文本框。我对它们不太熟悉,但以下链接应该会有所帮助。 No Validation 如果您需要更多帮助,请联系 msdn source
    如果我正确理解您的问题,我认为这应该可以解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-04
      相关资源
      最近更新 更多