3

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

我已经应用了逻辑,但问题是它只适用于硬编码的文本框 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

4 回答 4

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
于 2012-05-11T15:16:08.610 回答
1

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

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

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

  }
于 2012-05-11T07:14:53.983 回答
0
  <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="the name of your textbox to be validated" ForeColor="Red"></asp:RequiredFieldValidator>

试试看可能对你有帮助,你可以使用验证控件来验证任何输入

于 2012-05-11T07:39:54.510 回答
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>

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

于 2019-03-06T10:26:05.850 回答