<%--OnClick="Btn_Submit_Click"--%>
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jayraj ChhayaPosted Jan 11, 2024, 6:27 AM
To solve the problem and implement validation for the first, second, and third rows of the GridView, you can follow these steps:
Btn_Submit_Click).FindControlmethod to access the TextBox controls within each row and retrieve their values for validation.Here's an example of how you can implement the server-side validation for the first, second, and third rows of the GridView:
Make sure to replace the
Btn_Submit_Clickevent handler with the actual event handler name used in your code.sumit sankpalPosted Jan 11, 2024, 6:26 AM
if (e.Row.RowType == DataControlRowType.DataRow)
after Checking this condition it is not entering inside it.
it is going outside.
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex < 3)
{
// Apply validation to the first three rows
TextBox txtStream = (TextBox)e.Row.FindControl("txtStream");
TextBox txtInstituteName = (TextBox)e.Row.FindControl("txtInstituteName");
TextBox txtGrade = (TextBox)e.Row.FindControl("txtGrade");
TextBox txtYearOfPassing = (TextBox)e.Row.FindControl("txtYearOfPassing");
// Add your validation logic here
RequiredFieldValidator rtxtStream = FindControl("rtxtStream") as RequiredFieldValidator;
RequiredFieldValidator rtxtInstituteName = FindControl("rtxtInstituteName") as RequiredFieldValidator;
RequiredFieldValidator rtxtGrade = FindControl("rtxtGrade") as RequiredFieldValidator;
RequiredFieldValidator rtxtYearOfPassing = FindControl("rtxtYearOfPassing") as RequiredFieldValidator;
// Set the Visible property to false for the RequiredFieldValidators in the last row
if (rtxtStream != null)
rtxtStream.Visible = true;
if (rtxtInstituteName != null)
rtxtInstituteName.Visible = true;
if (rtxtGrade != null)
rtxtGrade.Visible = true;
if (rtxtYearOfPassing != null)
rtxtYearOfPassing.Visible = true;
}
if(e.Row.RowIndex == 3)
{
TextBox txtStream = (TextBox)e.Row.FindControl("txtStream");
TextBox txtInstituteName = (TextBox)e.Row.FindControl("txtInstituteName");
TextBox txtGrade = (TextBox)e.Row.FindControl("txtGrade");
TextBox txtYearOfPassing = (TextBox)e.Row.FindControl("txtYearOfPassing");
// validation logic
RequiredFieldValidator rtxtStream = FindControl("rtxtStream") as RequiredFieldValidator;
RequiredFieldValidator rtxtInstituteName = FindControl("rtxtInstituteName") as RequiredFieldValidator;
RequiredFieldValidator rtxtGrade = FindControl("rtxtGrade") as RequiredFieldValidator;
RequiredFieldValidator rtxtYearOfPassing = FindControl("rtxtYearOfPassing") as RequiredFieldValidator;
// Set the Visible property to false for the RequiredFieldValidators in the last row
if (rtxtStream != null)
rtxtStream.Visible = false;
if (rtxtInstituteName != null)
rtxtInstituteName.Visible = false;
if (rtxtGrade != null)
rtxtGrade.Visible = false;
if (rtxtYearOfPassing != null)
rtxtYearOfPassing.Visible = false;
}
}
Naimish MakwanaPosted Jan 11, 2024, 5:31 AM
To validate only the first three rows of a GridView on the server side, you can use the
RowDataBoundevent. This event is triggered for each row when it’s bound to data. Inside this event, you can check the row index and apply validation only if the row index is less than 3 (since row indices start from 0). Here’s an example:In the above code,
FindControlis used to access the TextBox controls in the GridView1. You can replace the comment// Add your validation logic herewith your actual validation logic. Please note that this is a basic example and you might need to adjust it according to your specific requirements.Remember to wire up this event handler to the
OnRowDataBoundevent of your GridView in the ASPX markup:Thanks