0

I have a GridView in Which I have four TextBoxes in the Template Field. I have one Button below the GridView.

How to validate the TextBoxes in the GridView, When the Button Clicked?

4 Answers 4

1

use RequiredFieldValidator and set ValidationGroup="gridview", check below example

   <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>
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Akhtar, Its working. But when we use Required Field Validator, the textbox alignment in the GridView is not looking good. How to solve this?
Set Display="Static" to required FieldValidator.
Hi Akhtar, Everything is perfectly working. But I have one doubt. Suppose if we require validation only for three textboxes and we dont want to validate the last textbox. In this case, how do we display all the textboxes alignment perfectly?
1

You can use the 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>

1 Comment

Hi Peyram, I am new to JQuery. Anyway thank u for ur valuable contribution. I upvoted ur answer.
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>

And then call the JavaScript function through link button like this:

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

Comments

0

i have 7 textbox

ok i have worked on my JS function and it worked for me.hope it will work for everyone else.in my code i have taken a variable

success which is working as a flag and i am checking it twice and returning true at the end so that if one of the textbox is not empty and other not it will not return true. sorry for poor editing

 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;

    }

onclientclick

<asp:Button ID="btn_Update" Style="background-color: #B2DE94; width: 40px"  CausesValidation="false" runat="server"  OnClientClick="return fnCheck(this);" Text="Update" CommandName="Update" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.