在ASP.Net中的GridView中对TextBox的JavaScript验证

时间:2011-06-06 06:35:01

标签: c# javascript asp.net validation user-controls

我有一个GridView,其中我在模板字段中有四个TextBox。我在GridView下面有一个按钮。

如何在单击按钮的情况下验证GridView中的TextBox?

4 个答案:

答案 0 :(得分:1)

使用RequiredFieldValidator并设置ValidationGroup="gridview",请查看以下示例

   <asp:TemplateField HeaderText="">
          <ItemTemplate>
              <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
               <asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="TextBox3" ValidationGroup="gridview" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
          </ItemTemplate>
            </asp:TemplateField>
          <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <asp:Button ID="Button2" runat="server" Text="Button" ValidationGroup="gridview" CausesValidation="true" />
                </ItemTemplate>
            </asp:TemplateField>

答案 1 :(得分:1)

您可以使用JQuery Validation Plugin

<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate  /lib/jquery.delegate.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>
<script type="text/javascript">
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});;
</script>

<script>
$(document).ready(function(){
$("#myform").validate({
rules: {
field: "required"
}
});
});

  

 <body>
 <form id="myform">
  <label for="field">Required: </label>
  <input class="left" id="field" name="field" />
  <br/>
  <input type="submit" value="Validate!" />
 </form>
 </body>

答案 2 :(得分:1)

<script type="text/javascript">
    function ValidateGridview() {
        titlename = document.getElementById('<%=((TextBox)grd_party_influenc.FooterRow.FindControl("txt_f_title")).ClientID%>');
        if (titlename.value ==0) {
            alert("Please Insert The Title ....");
            titlename.focus();
            return false;
        }
       // return true;
    }

</script>

然后通过链接按钮调用JavaScript函数:

<asp:LinkButton ID="lnk_btn_insert" runat="server" CommandName="Insert" OnClientClick="ValidateGridview()">Insert</asp:LinkButton>

答案 3 :(得分:0)

我有7个文本框

  
    

好吧我已经处理了我的JS功能,它对我有用。我会这样做     为其他人工作。在我的代码中,我采取了一个变量

  
     

mix.scripts([])作为一个标志,我正在检查它两次   最后返回success,以便其中一个文本框不为空   而不是它不会返回真实。   抱歉编辑不当

true

<强>的OnClientClick

 function fnCheck(val) {
        var success = true;
        var v = val.id.split('_')[1];
        var merter = document.getElementById('GridSubMeter_' + v + '_txtMeterIdn').value.trim();
        var Billper = document.getElementById('GridSubMeter_' + v + '_txBillPer').value.trim()
        var Endkwh = document.getElementById('GridSubMeter_' + v + '_txEndKwh').value.trim();
        var startkwh = document.getElementById('GridSubMeter_' + v + '_txStartKwh').value.trim();
        var ReadEndDate = document.getElementById('GridSubMeter_' + v + '_txReadEndDate').value.trim();
        var ReadStartDate = document.getElementById('GridSubMeter_' + v + '_txReadStartDate').value.trim();
        var CTFACT = document.getElementById('GridSubMeter_' + v + '_txCTFact').value.trim();
        debugger;
        if (merter != '') {

        }
        else {
            alert("Meter Identifier is Required Field");
            success = false;
    }

    if (Billper != '') {

    }
    else {
        alert("Bill Period is Required Field");
        success = false;
    }

    if (Endkwh != '') {

    }
    else {
        alert("EndKwh is Required Field");
        success = false;
    }
    if (startkwh != '') {

    }
    else {
        alert("StartKwh is Required Field");
        success = false;
    }
    if (ReadEndDate != '') {

    }
    else {
        alert("Read EndDate is Required Field");
        success = false;
    }

    if (ReadStartDate != '') {

    }
    else {
        alert("Read StartDate is Required Field");
        success = false;
    }
    if (CTFACT != '') {

    }
    else
    { alert("CT Factor is Required Field");
    success = false;
}

return success;

    }