如何使用javascript在asp.net的gridview中按钮单击上应用文本框空白验证?

时间:2012-05-11 07:12:07

标签: c# javascript asp.net vb.net validation

如何在javascript中的gridview内点按钮上应用文本框空白验证?我有 一个gridview,每行包含2个文本框和一个保存按钮。我想在相应的保存按钮点击上验证文本框。

我已应用逻辑但问题是它只适用于硬编码的textBox ID。如何修改此代码以使其适用于所有gridview行?

function gvValidate() {

var grid = document.getElementById('<%= GridViewCTInformation.ClientID %>');
 if(grid!=null) 
  {
   var Inputs = grid.getElementsByTagName("input"); 
    for(i = 0; i < Inputs.length; i++) 
     {
      if(Inputs[i].type == 'text' ) 
       {
           if (Inputs[i].id == 'ctl00_contentPlaceHolderSubScreen_GridViewCTInformation_ctl02_TextBoxCTTermCode') 
             {
                 if (Inputs[i].value == "") {
                     alert("Enter values,blank is not allowed");
                     return false;
                 }

             }
             else if (Inputs[i].id == 'ctl00_contentPlaceHolderSubScreen_GridViewCTInformation_ctl02_TextBoxCTTermDesc') {
                 if (Inputs[i].value == "") {
                     alert("Enter values,blank is not allowed");
                     return false;
                 }
             }

      }
     }
     return true;
 }

}

 Protected Sub GridViewTaxInformation_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewTaxInformation.RowDataBound
    Try
        If e.Row.RowType = DataControlRowType.DataRow Then

            Dim btnSave As Button = DirectCast(e.Row.FindControl("ButtonSave"), Button)
            btnSave.Attributes.Add("onclick", "return gvValidate()")
        End If
    Catch ex As Exception
        Common.WriteLog(ex.Message)
        Common.WriteLog((ex.StackTrace))
        Response.Redirect("..\Errors.aspx", False)
    End Try
End Sub

4 个答案:

答案 0 :(得分:2)

最后我得到了我的问题的解决方案..我刚刚将gridview的行索引传递给了javascript函数。

这是代码

 function gvValidate(rowIndex) {

var grid = document.getElementById('<%= GridViewCTInformation.ClientID %>');
 if(grid!=null) {
     var Inputs = grid.rows[rowIndex + 1].getElementsByTagName("input"); 
    for(i = 0; i < Inputs.length; i++) 
     {
      if(Inputs[i].type == 'text' ) 
       {
                  if (Inputs[i].value == "") {
                     alert("Enter values,blank is not allowed");
                     return false;
                 }

      }
     }
     return true;
 }

}

Protected Sub GridViewCTInformation_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewCTInformation.RowDataBound
    Try
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim btnSave As Button = DirectCast(e.Row.FindControl("ButtonCTInfoSave"), Button)
            btnSave.Attributes.Add("onclick", "return gvValidate(" + e.Row.RowIndex.ToString() + ")")
        End If
    Catch ex As Exception
        Common.WriteLog(ex.Message)
        Common.WriteLog((ex.StackTrace))
        Response.Redirect("..\Errors.aspx", False)
    End Try
End Sub

答案 1 :(得分:1)

不要检查身份证明。只需检查空白值。

 if(Inputs[i].type == 'text' ) 
 {

             if (Inputs[i].value == "") {
                 alert("Enter values,blank is not allowed");
                 return false;
             }

  }

答案 2 :(得分:0)

  <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="the name of your textbox to be validated" ForeColor="Red"></asp:RequiredFieldValidator>

尝试它可能有助于您使用验证控件来验证任何输入

答案 3 :(得分:0)

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
    $("[id*=GridView1] [id*=lnkUpdate]").click(function () {
        //Find the GridView Row using the LinkButton reference.
        var row = $(this).closest("tr");

        //Find the TextBox control.
        var txtName = row.find("[id*=txtName]");

        //Find the DropDownList control.
        var ddlCountries = row.find("[id*=ddlCountries]");

        var message = "";

        //Validate the TextBox control.
        if ($.trim(txtName.val()) == "") {
            message += "Please enter Name.\n";
        }

        //Validate the DropDownList control.
        if (ddlCountries.val() == "") {
            message += "Please select Country.";
        }

        //Display error message.
        if (message != "") {
            alert(message);
            return false;
        }
        return true;
    });
});
</script>

您可以尝试使用上面的代码进行网格视图验证。